From fe898e7862f945fa3632580139602c627dcb9be0 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Sat, 25 Feb 2017 10:58:18 -0800 Subject: [PATCH] Fix kmem accouting when use with cgroupsPath Fixes: #1347 Fixes: #1083 The root cause of #1083 is because we're joining an existed cgroup whose kmem accouting is not initialized, and it has child cgroup or tasks in it. Fix it by checking if the cgroup is first time created, and we should enable kmem accouting if the cgroup is craeted by libcontainer with or without kmem limit configed. Otherwise we'll get issue like #1347 Signed-off-by: Qiang Huang --- libcontainer/cgroups/fs/memory.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 8d286832..b7f9b1f5 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -33,14 +33,18 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) { path, err := d.path("memory") if err != nil && !cgroups.IsNotFound(err) { return err + } else if path == "" { + return nil } if memoryAssigned(d.config) { - if path != "" { + if _, err := os.Stat(path); os.IsNotExist(err) { if err := os.MkdirAll(path, 0755); err != nil { return err } - } - if d.config.KernelMemory != 0 { + // Only enable kernel memory accouting when this cgroup + // is created by libcontainer, otherwise we might get + // error when people use `cgroupsPath` to join an existed + // cgroup whose kernel memory is not initialized. if err := EnableKernelMemoryAccounting(path); err != nil { return err }