diff --git a/libcontainer/cgroups/fs2/memory.go b/libcontainer/cgroups/fs2/memory.go index 23eccbec..f1db0786 100644 --- a/libcontainer/cgroups/fs2/memory.go +++ b/libcontainer/cgroups/fs2/memory.go @@ -15,22 +15,38 @@ import ( "github.com/pkg/errors" ) +// numToStr converts an int64 value to a string for writing to a +// cgroupv2 files with .min, .max, .low, or .high suffix. +// Negative values are converted to "max" for cgroupv1 compatibility +// (which used to write -1 to remove the limit). +func numToStr(value int64) (ret string) { + if value > 0 { + ret = strconv.FormatInt(value, 10) + } else if value < 0 { + ret = "max" + } else { + ret = "" + } + + return ret +} + func setMemory(dirPath string, cgroup *configs.Cgroup) error { - if cgroup.Resources.MemorySwap != 0 { - if err := fscommon.WriteFile(dirPath, "memory.swap.max", strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil { + if val := numToStr(cgroup.Resources.MemorySwap); val != "" { + if err := fscommon.WriteFile(dirPath, "memory.swap.max", val); err != nil { return err } } - if cgroup.Resources.Memory != 0 { - if err := fscommon.WriteFile(dirPath, "memory.max", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { + if val := numToStr(cgroup.Resources.Memory); val != "" { + if err := fscommon.WriteFile(dirPath, "memory.max", val); err != nil { return err } } // cgroup.Resources.KernelMemory is ignored - if cgroup.Resources.MemoryReservation != 0 { - if err := fscommon.WriteFile(dirPath, "memory.low", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil { + if val := numToStr(cgroup.Resources.MemoryReservation); val != "" { + if err := fscommon.WriteFile(dirPath, "memory.low", val); err != nil { return err } } diff --git a/libcontainer/cgroups/fs2/pids.go b/libcontainer/cgroups/fs2/pids.go index db2d7ac9..99d912cf 100644 --- a/libcontainer/cgroups/fs2/pids.go +++ b/libcontainer/cgroups/fs2/pids.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strconv" "strings" "github.com/opencontainers/runc/libcontainer/cgroups" @@ -17,15 +16,8 @@ import ( ) func setPids(dirPath string, cgroup *configs.Cgroup) error { - if cgroup.Resources.PidsLimit != 0 { - // "max" is the fallback value. - limit := "max" - - if cgroup.Resources.PidsLimit > 0 { - limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10) - } - - if err := fscommon.WriteFile(dirPath, "pids.max", limit); err != nil { + if val := numToStr(cgroup.Resources.PidsLimit); val != "" { + if err := fscommon.WriteFile(dirPath, "pids.max", val); err != nil { return err } }