From d57f5bb28623f71202bd9acdcd0eb2f471bbee6e Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 20 May 2020 11:49:51 -0700 Subject: [PATCH] cgroupv1: don't ignore MemorySwap if Memory==-1 Commit 18ebc51b3cc3 "Reset Swap when memory is set to unlimited (-1)" added handling of the case when a user updates the container limits to set memory to unlimited (-1) but do not set any other limits. Apparently, in this case, if swap limit was previously set, kernel fails to set memory.limit_in_bytes to -1 if memory.memsw.limit_in_bytes is not set to -1. What the above commit fails to handle correctly is the request when Memory is set to -1 and MemorySwap is set to some specific limit N (where N > 0). In this case, the value of N is silently discarded and MemorySwap is set to -1 instead. This is wrong thing to do, as the limit set, even if incorrectly, should not be ignored. Fix this by only assigning MemorySwap == -1 in case it was not explicitly set. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs/memory.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 39809ef0..95087690 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -74,9 +74,9 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) { } func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error { - // If the memory update is set to -1 we should also - // set swap to -1, it means unlimited memory. - if cgroup.Resources.Memory == -1 { + // If the memory update is set to -1 and the swap is not explicitly + // set, we should also set swap to -1, it means unlimited memory. + if cgroup.Resources.Memory == -1 && cgroup.Resources.MemorySwap == 0 { // Only set swap if it's enabled in kernel if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) { cgroup.Resources.MemorySwap = -1