diff --git a/internal/server/cgroup/abstraction.go b/internal/server/cgroup/abstraction.go index 8573ca6f08..6ffcc9e664 100644 --- a/internal/server/cgroup/abstraction.go +++ b/internal/server/cgroup/abstraction.go @@ -153,7 +153,7 @@ func (cg *CGroup) GetProcessesUsage() (int64, error) { // SetMemorySwapLimit sets the hard limit for swap. func (cg *CGroup) SetMemorySwapLimit(limit int64) error { - if !cgControllers["memory"] { + if !cgControllers["memory.swap"] { return ErrControllerMissing } @@ -314,7 +314,7 @@ func (cg *CGroup) SetMemorySwappiness(limit int64) error { // GetMemorySwapLimit returns the hard limit on swap usage. func (cg *CGroup) GetMemorySwapLimit() (int64, error) { - if !cgControllers["memory"] { + if !cgControllers["memory.swap"] { return -1, ErrControllerMissing } @@ -337,7 +337,7 @@ func (cg *CGroup) GetMemorySwapLimit() (int64, error) { // GetMemorySwapUsage return current usage of swap. func (cg *CGroup) GetMemorySwapUsage() (int64, error) { - if !cgControllers["memory"] { + if !cgControllers["memory.swap"] { return -1, ErrControllerMissing } diff --git a/internal/server/cgroup/init.go b/internal/server/cgroup/init.go index 718ef17960..1d410c0e99 100644 --- a/internal/server/cgroup/init.go +++ b/internal/server/cgroup/init.go @@ -11,6 +11,7 @@ import ( "github.com/lxc/incus/v7/internal/server/db/cluster" "github.com/lxc/incus/v7/internal/server/db/warningtype" "github.com/lxc/incus/v7/shared/logger" + "github.com/lxc/incus/v7/shared/util" ) var cgControllers = map[string]bool{} @@ -37,6 +38,9 @@ const ( // Memory resource control. Memory + // MemorySwap resource control. + MemorySwap + // Pids resource control. Pids ) @@ -54,6 +58,8 @@ func Supports(resource Resource) bool { return cgControllers["io"] case Memory: return cgControllers["memory"] + case MemorySwap: + return cgControllers["memory.swap"] case Pids: return cgControllers["pids"] } @@ -98,6 +104,11 @@ func Warnings() []cluster.Warning { TypeCode: warningtype.MissingCGroupMemoryController, LastMessage: "memory limits will be ignored", }) + } else if !Supports(MemorySwap) { + warnings = append(warnings, cluster.Warning{ + TypeCode: warningtype.MissingCGroupMemorySwapAccounting, + LastMessage: "swap limits will be ignored", + }) } if !Supports(Pids) { @@ -156,5 +167,15 @@ func Init() { } _ = controllers.Close() + + // Check for swap accounting support (can be disabled through kernel arguments). + if cgControllers["memory"] { + for _, path := range []string{filepath.Join(cgPath, fields[2]), filepath.Join(cgPath, "init.scope")} { + if util.PathExists(filepath.Join(path, "memory.swap.current")) { + cgControllers["memory.swap"] = true + break + } + } + } } }