From 7ef6ece774facb3319c906114f15da1f22e0272c Mon Sep 17 00:00:00 2001 From: Jakub Guzik Date: Thu, 3 Jun 2021 22:54:10 +0200 Subject: [PATCH] Fix setting swaplimit=true without checking Signed-off-by: Jakub Guzik --- pkg/sysinfo/cgroup2_linux.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/sysinfo/cgroup2_linux.go b/pkg/sysinfo/cgroup2_linux.go index 1f9fbf4438..432356b498 100644 --- a/pkg/sysinfo/cgroup2_linux.go +++ b/pkg/sysinfo/cgroup2_linux.go @@ -2,11 +2,13 @@ package sysinfo // import "github.com/docker/docker/pkg/sysinfo" import ( "io/ioutil" + "os" "path" "strings" cgroupsV2 "github.com/containerd/cgroups/v2" "github.com/containerd/containerd/sys" + "github.com/opencontainers/runc/libcontainer/cgroups" "github.com/sirupsen/logrus" ) @@ -66,6 +68,24 @@ func newV2(quiet bool, opts *opts) *SysInfo { return sysInfo } +func getSwapLimitV2() bool { + groups, err := cgroups.ParseCgroupFile("/proc/self/cgroup") + if err != nil { + return false + } + + g := groups[""] + if g == "" { + return false + } + + cGroupPath := path.Join("/sys/fs/cgroup", g, "memory.swap.max") + if _, err = os.Stat(cGroupPath); os.IsNotExist(err) { + return false + } + return true +} + func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}, _ string) []string { var warnings []string if _, ok := controllers["memory"]; !ok { @@ -74,7 +94,7 @@ func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}, _ s } info.MemoryLimit = true - info.SwapLimit = true + info.SwapLimit = getSwapLimitV2() info.MemoryReservation = true info.OomKillDisable = false info.MemorySwappiness = false