-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support hardware watchdog timers
Only enabled when activated by config, disabled on shutdown/reboot Fixes #8284 Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com>
- Loading branch information
Showing
8 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
internal/app/machined/pkg/controllers/runtime/watchdog_timer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"go.uber.org/zap" | ||
"golang.org/x/sys/unix" | ||
"sigs.k8s.io/kustomize/kyaml/errors" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
) | ||
|
||
// WatchdogTimerController watches v1alpha1.Config, creates/updates/deletes kernel module specs. | ||
type WatchdogTimerController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Name() string { | ||
return "runtime.WatchdogTimerController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: optional.Some(config.V1Alpha1ID), | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Outputs() []controller.Output { | ||
return nil | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo,cyclop | ||
func (ctrl *WatchdogTimerController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
ticker := time.NewTicker(3 * time.Second) | ||
defer ticker.Stop() | ||
|
||
var wd *os.File | ||
|
||
wdClose := func() { | ||
logger.Info("Closing hardware watchdog", zap.String("filename", wd.Name())) | ||
// Magic close: make sure old watchdog won't trip after we close it | ||
if _, err := wd.WriteString("V"); err != nil { | ||
logger.Error("Failed to send magic close to watchdog", zap.String("filename", wd.Name())) | ||
} | ||
|
||
if err := wd.Close(); err != nil { | ||
logger.Error("Failed to close watchdog", zap.String("filename", wd.Name())) | ||
} | ||
|
||
wd = nil | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
if wd != nil { | ||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), unix.WDIOC_KEEPALIVE, 0); err != 0 { | ||
return fmt.Errorf("failed to feed watchdog: %w", err) | ||
} | ||
} | ||
|
||
continue | ||
case <-r.EventCh(): | ||
} | ||
|
||
r.ResetRestartBackoff() | ||
|
||
cfg, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.V1Alpha1ID) | ||
if err != nil { | ||
if !state.IsNotFoundError(err) { | ||
return fmt.Errorf("error getting watchdog config: %w", err) | ||
} | ||
} | ||
|
||
if cfg != nil && cfg.Config().Machine() != nil { | ||
dev := cfg.Config().Machine().Kernel().WatchdogDevice() | ||
timeout := cfg.Config().Machine().Kernel().WatchdogTimeout() | ||
|
||
if dev == "" || timeout == 0 { | ||
continue | ||
} | ||
|
||
// Close the watchdog if requested to use new one | ||
if wd != nil && wd.Name() != dev { | ||
wdClose() | ||
} | ||
|
||
if wd == nil { | ||
wd, err = os.OpenFile(dev, syscall.O_RDWR, 0o600) | ||
if err != nil { | ||
return errors.Errorf("failed to open watchdog device: %s", err) | ||
} | ||
|
||
logger.Info("Opened hardware watchdog", zap.String("filename", dev)) | ||
// TODO: support reboot/shutdown watchdogs | ||
defer wdClose() | ||
} | ||
|
||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), uintptr(unix.WDIOC_SETTIMEOUT), uintptr(unsafe.Pointer(&timeout))); err != 0 { | ||
return fmt.Errorf("failed to set watchdog timeout: %w", err) | ||
} | ||
|
||
// 3 pings per timeout should suffice in any case | ||
ticker = time.NewTicker(time.Duration(timeout/3) * time.Second) | ||
|
||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), uintptr(unix.WDIOC_KEEPALIVE), uintptr(0)); err != 0 { | ||
return fmt.Errorf("failed to feed watchdog: %w", err) | ||
} | ||
|
||
logger.Info("Set hardware watchdog timeout", zap.Int("timeout", timeout), zap.Int("feed_interval", timeout/3)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters