diff --git a/libcontainer/cgroups/fs2/memory.go b/libcontainer/cgroups/fs2/memory.go index 29656597423..df8336ba0f8 100644 --- a/libcontainer/cgroups/fs2/memory.go +++ b/libcontainer/cgroups/fs2/memory.go @@ -57,7 +57,10 @@ func setMemory(dirPath string, r *configs.Resources) error { // never write empty string to `memory.swap.max`, it means set to 0. if swapStr != "" { if err := cgroups.WriteFile(dirPath, "memory.swap.max", swapStr); err != nil { - return err + // If swap is not enabled, silently ignore setting to max or disabling it. + if !(errors.Is(err, os.ErrNotExist) && (swapStr == "max" || swapStr == "0")) { + return err + } } } diff --git a/tests/integration/cgroups.bats b/tests/integration/cgroups.bats index a9c2ea4856f..d5ca1325dce 100644 --- a/tests/integration/cgroups.bats +++ b/tests/integration/cgroups.bats @@ -272,7 +272,6 @@ convert_hugetlb_size() { "memory.low": "524288", "memory.high": "5242880", "memory.max": "20484096", - "memory.swap.max": "20971520", "pids.max": "99", "cpu.max": "10000 100000", "cpu.weight": "42" @@ -289,7 +288,6 @@ convert_hugetlb_size() { echo "$output" | grep -q '^memory.low:524288$' echo "$output" | grep -q '^memory.high:5242880$' echo "$output" | grep -q '^memory.max:20484096$' - echo "$output" | grep -q '^memory.swap.max:20971520$' echo "$output" | grep -q '^pids.max:99$' echo "$output" | grep -q '^cpu.max:10000 100000$' @@ -297,12 +295,34 @@ convert_hugetlb_size() { check_systemd_value "MemoryLow" 524288 check_systemd_value "MemoryHigh" 5242880 check_systemd_value "MemoryMax" 20484096 - check_systemd_value "MemorySwapMax" 20971520 check_systemd_value "TasksMax" 99 check_cpu_quota 10000 100000 "100ms" check_cpu_weight 42 } +@test "runc run (cgroup v2 resources.unified swap)" { + requires root cgroups_v2 cgroups_swap + + set_cgroups_path + update_config ' .linux.resources.unified |= { + "memory.max": "20484096", + "memory.swap.max": "20971520" + }' + + runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified + [ "$status" -eq 0 ] + + runc exec test_cgroups_unified sh -c 'cd /sys/fs/cgroup && grep . *.max' + [ "$status" -eq 0 ] + echo "$output" + + echo "$output" | grep -q '^memory.max:20484096$' + echo "$output" | grep -q '^memory.swap.max:20971520$' + + check_systemd_value "MemoryMax" 20484096 + check_systemd_value "MemorySwapMax" 20971520 +} + @test "runc run (cgroup v2 resources.unified override)" { requires root cgroups_v2 diff --git a/tests/integration/helpers.bash b/tests/integration/helpers.bash index e1d2a80af84..8124abbd75c 100755 --- a/tests/integration/helpers.bash +++ b/tests/integration/helpers.bash @@ -121,7 +121,7 @@ function init_cgroup_paths() { CGROUP_BASE_PATH=/sys/fs/cgroup # Find any cgroup.freeze files... - if [ -n "$(find "$CGROUP_BASE_PATH" -type f -name "cgroup.freeze" -print -quit)" ]; then + if [ -n "$(find "$CGROUP_BASE_PATH" -maxdepth 2 -type f -name "cgroup.freeze" -print -quit)" ]; then CGROUP_SUBSYSTEMS+=" freezer" fi else @@ -451,8 +451,14 @@ function requires() { ;; cgroups_swap) init_cgroup_paths - if [ -v CGROUP_V1 ] && [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then - skip_me=1 + if [ -v CGROUP_V1 ]; then + if [ ! -e "${CGROUP_MEMORY_BASE_PATH}/memory.memsw.limit_in_bytes" ]; then + skip_me=1 + fi + elif [ -v CGROUP_V2 ]; then + if [ -z "$(find "$CGROUP_BASE_PATH" -maxdepth 2 -type f -name memory.swap.max -print -quit)" ]; then + skip_me=1 + fi fi ;; cgroups_cpu_idle) @@ -460,7 +466,7 @@ function requires() { init_cgroup_paths [ -v CGROUP_V1 ] && p="$CGROUP_CPU_BASE_PATH" [ -v CGROUP_V2 ] && p="$CGROUP_BASE_PATH" - if [ -z "$(find "$p" -name cpu.idle -print -quit)" ]; then + if [ -z "$(find "$p" -maxdepth 2 -type f -name cpu.idle -print -quit)" ]; then skip_me=1 fi ;; @@ -476,7 +482,7 @@ function requires() { p="$CGROUP_BASE_PATH" f="cpu.max.burst" fi - if [ -z "$(find "$p" -name "$f" -print -quit)" ]; then + if [ -z "$(find "$p" -maxdepth 2 -type f -name "$f" -print -quit)" ]; then skip_me=1 fi ;; diff --git a/tests/integration/update.bats b/tests/integration/update.bats index ed57efca9fb..54774a877b2 100644 --- a/tests/integration/update.bats +++ b/tests/integration/update.bats @@ -38,10 +38,6 @@ function setup() { MEM_SWAP="memory.memsw.limit_in_bytes" SD_MEM_SWAP="unsupported" SYSTEM_MEM=$(cat "${CGROUP_MEMORY_BASE_PATH}/${MEM_LIMIT}") - HAVE_SWAP="no" - if [ -f "${CGROUP_MEMORY_BASE_PATH}/${MEM_SWAP}" ]; then - HAVE_SWAP="yes" - fi else MEM_LIMIT="memory.max" SD_MEM_LIMIT="MemoryMax" @@ -50,9 +46,11 @@ function setup() { MEM_SWAP="memory.swap.max" SD_MEM_SWAP="MemorySwapMax" SYSTEM_MEM="max" - HAVE_SWAP="yes" fi + unset HAVE_SWAP + get_cgroup_value $MEM_SWAP >/dev/null && HAVE_SWAP=yes + SD_UNLIMITED="infinity" SD_VERSION=$(systemctl --version | awk '{print $2; exit}') if [ "$SD_VERSION" -lt 227 ]; then @@ -94,8 +92,8 @@ function setup() { check_cgroup_value "$MEM_RESERVE" 33554432 check_systemd_value "$SD_MEM_RESERVE" 33554432 - # Run swap memory tests if swap is available - if [ "$HAVE_SWAP" = "yes" ]; then + # Run swap memory tests if swap is available. + if [ -v HAVE_SWAP ]; then # try to remove memory swap limit runc update test_update --memory-swap -1 [ "$status" -eq 0 ] @@ -127,7 +125,7 @@ function setup() { check_systemd_value "$SD_MEM_LIMIT" "$SD_UNLIMITED" # check swap memory limited is gone - if [ "$HAVE_SWAP" = "yes" ]; then + if [ -v HAVE_SWAP ]; then check_cgroup_value "$MEM_SWAP" "$SYSTEM_MEM" check_systemd_value "$SD_MEM_SWAP" "$SD_UNLIMITED" fi @@ -221,7 +219,7 @@ EOF check_cgroup_value "pids.max" 20 check_systemd_value "TasksMax" 20 - if [ "$HAVE_SWAP" = "yes" ]; then + if [ -v HAVE_SWAP ]; then # Test case for https://github.com/opencontainers/runc/pull/592, # checking libcontainer/cgroups/fs/memory.go:setMemoryAndSwap.