From c8a459f728ae14dbcfe878e97e6e6daada39d8fb Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Fri, 26 Feb 2016 16:02:10 +0800 Subject: [PATCH] Fix problem when update memory and swap memory Currently, if we start a container with: `docker run -ti --name foo --memory 300M --memory-swap 500M busybox sh` Then we want to update it with: `docker update --memory 600M --memory-swap 800M foo` It'll get error because we can't set memory to 600M with the 500M limit of swap memory. Signed-off-by: Qiang Huang --- libcontainer/cgroups/fs/memory.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index e3fd327e91f..ac3d849a7d2 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -66,13 +66,20 @@ func (s *MemoryGroup) SetKernelMemory(path string, cgroup *configs.Cgroup) error } func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error { - if cgroup.Resources.Memory != 0 { - if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { + // When memory and swap memory are both set, we need to release the limits first, + // it's to avoid problem when we update the configs, the new memory limit could + // be larger than the existed swap memory limit but smaller than the new swap + // memory limit, we should allow such update usage. + if cgroup.Resources.Memory != 0 && cgroup.Resources.MemorySwap != 0 { + if err := writeFile(path, "memory.memsw.limit_in_bytes", "-1"); err != nil { + return err + } + if err := writeFile(path, "memory.limit_in_bytes", "-1"); err != nil { return err } } - if cgroup.Resources.MemoryReservation != 0 { - if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil { + if cgroup.Resources.Memory != 0 { + if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil { return err } } @@ -81,6 +88,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error { return err } } + if cgroup.Resources.MemoryReservation != 0 { + if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil { + return err + } + } if cgroup.Resources.OomKillDisable { if err := writeFile(path, "memory.oom_control", "1"); err != nil { return err