Skip to content

Commit 1f737ee

Browse files
authored
Merge pull request #2426 from kolyshkin/mem-swap-unlim
Fix some cases of swap setting
2 parents 7673bee + 3c6e8ac commit 1f737ee

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

libcontainer/cgroups/fs/memory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
7474
}
7575

7676
func setMemoryAndSwap(path string, cgroup *configs.Cgroup) error {
77-
// If the memory update is set to -1 we should also
78-
// set swap to -1, it means unlimited memory.
79-
if cgroup.Resources.Memory == -1 {
77+
// If the memory update is set to -1 and the swap is not explicitly
78+
// set, we should also set swap to -1, it means unlimited memory.
79+
if cgroup.Resources.Memory == -1 && cgroup.Resources.MemorySwap == 0 {
8080
// Only set swap if it's enabled in kernel
8181
if cgroups.PathExists(filepath.Join(path, cgroupMemorySwapLimit)) {
8282
cgroup.Resources.MemorySwap = -1

libcontainer/cgroups/systemd/v2.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func genV2ResourcesProperties(c *configs.Cgroup) ([]systemdDbus.Property, error)
5757
properties = append(properties,
5858
newProp("MemoryLow", uint64(c.Resources.MemoryReservation)))
5959
}
60-
// swap is set
61-
if c.Resources.MemorySwap != 0 {
62-
swap, err := cgroups.ConvertMemorySwapToCgroupV2Value(c.Resources.MemorySwap, c.Resources.Memory)
63-
if err != nil {
64-
return nil, err
65-
}
60+
61+
swap, err := cgroups.ConvertMemorySwapToCgroupV2Value(c.Resources.MemorySwap, c.Resources.Memory)
62+
if err != nil {
63+
return nil, err
64+
}
65+
if swap != 0 {
6666
properties = append(properties,
6767
newProp("MemorySwapMax", uint64(swap)))
6868
}

libcontainer/cgroups/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,12 @@ func ConvertCPUQuotaCPUPeriodToCgroupV2Value(quota int64, period uint64) string
612612
// for use by cgroup v2 drivers. A conversion is needed since Resources.MemorySwap
613613
// is defined as memory+swap combined, while in cgroup v2 swap is a separate value.
614614
func ConvertMemorySwapToCgroupV2Value(memorySwap, memory int64) (int64, error) {
615+
// for compatibility with cgroup1 controller, set swap to unlimited in
616+
// case the memory is set to unlimited, and swap is not explicitly set,
617+
// treating the request as "set both memory and swap to unlimited".
618+
if memory == -1 && memorySwap == 0 {
619+
return -1, nil
620+
}
615621
if memorySwap == -1 || memorySwap == 0 {
616622
// -1 is "max", 0 is "unset", so treat as is
617623
return memorySwap, nil

tests/integration/update.bats

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ EOF
8080
SD_MEM_SWAP="MemorySwapMax"
8181
SYSTEM_MEM="max"
8282
SYSTEM_MEM_SWAP="max"
83-
# checking swap is currently disabled for v2
84-
#CGROUP_MEMORY=$CGROUP_PATH
83+
CGROUP_MEMORY=$CGROUP_PATH
8584
;;
8685
esac
8786
SD_UNLIMITED="infinity"
@@ -135,10 +134,19 @@ EOF
135134
check_systemd_value "$SD_MEM_SWAP" $SD_UNLIMITED
136135

137136
# update memory swap
138-
runc update test_update --memory-swap 96468992
139-
[ "$status" -eq 0 ]
140-
check_cgroup_value "$MEM_SWAP" 96468992
141-
check_systemd_value "$SD_MEM_SWAP" 96468992
137+
if [ "$CGROUP_UNIFIED" = "yes" ]; then
138+
# for cgroupv2, memory and swap can only be set together
139+
runc update test_update --memory 52428800 --memory-swap 96468992
140+
[ "$status" -eq 0 ]
141+
# for cgroupv2, swap is a separate limit (it does not include mem)
142+
check_cgroup_value "$MEM_SWAP" $((96468992-52428800))
143+
check_systemd_value "$SD_MEM_SWAP" $((96468992-52428800))
144+
else
145+
runc update test_update --memory-swap 96468992
146+
[ "$status" -eq 0 ]
147+
check_cgroup_value "$MEM_SWAP" 96468992
148+
check_systemd_value "$SD_MEM_SWAP" 96468992
149+
fi
142150
fi
143151

144152
# try to remove memory limit

0 commit comments

Comments
 (0)