mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
52390d6804
This is somewhat radical approach to deal with kernel memory. Per-cgroup kernel memory limiting was always problematic. A few examples: - older kernels had bugs and were even oopsing sometimes (best example is RHEL7 kernel); - kernel is unable to reclaim the kernel memory so once the limit is hit a cgroup is toasted; - some kernel memory allocations don't allow failing. In addition to that, - users don't have a clue about how to set kernel memory limits (as the concept is much more complicated than e.g. [user] memory); - different kernels might have different kernel memory usage, which is sort of unexpected; - cgroup v2 do not have a [dedicated] kmem limit knob, and thus runc silently ignores kernel memory limits for v2; - kernel v5.4 made cgroup v1 kmem.limit obsoleted (see https://github.com/torvalds/linux/commit/0158115f702b). In view of all this, and as the runtime-spec lists memory.kernel and memory.kernelTCP as OPTIONAL, let's ignore kernel memory limits (for cgroup v1, same as we're already doing for v2). This should result in less bugs and better user experience. The only bad side effect from it might be that stat can show kernel memory usage as 0 (since the accounting is not enabled). [v2: add a warning in specconv that limits are ignored] Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
234 lines
6.9 KiB
Bash
234 lines
6.9 KiB
Bash
#!/usr/bin/env bats
|
|
|
|
load helpers
|
|
|
|
function teardown() {
|
|
teardown_bundle
|
|
}
|
|
|
|
function setup() {
|
|
setup_busybox
|
|
}
|
|
|
|
@test "runc create (no limits + no cgrouppath + no permission) succeeds" {
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions
|
|
[ "$status" -eq 0 ]
|
|
}
|
|
|
|
@test "runc create (rootless + no limits + cgrouppath + no permission) fails with permission error" {
|
|
requires rootless
|
|
requires rootless_no_cgroup
|
|
# systemd controls the permission, so error does not happen
|
|
requires no_systemd
|
|
|
|
set_cgroups_path
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"applying cgroup configuration"*"permission denied"* ]]
|
|
}
|
|
|
|
@test "runc create (rootless + limits + no cgrouppath + no permission) fails with informative error" {
|
|
requires rootless
|
|
requires rootless_no_cgroup
|
|
# systemd controls the permission, so error does not happen
|
|
requires no_systemd
|
|
|
|
set_resources_limit
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"rootless needs no limits + no cgrouppath when no permission is granted for cgroups"* ]] ||
|
|
[[ "$output" == *"cannot set pids limit: container could not join or create cgroup"* ]]
|
|
}
|
|
|
|
@test "runc create (limits + cgrouppath + permission on the cgroup dir) succeeds" {
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
set_cgroups_path
|
|
set_resources_limit
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions
|
|
[ "$status" -eq 0 ]
|
|
if [ "$CGROUP_UNIFIED" != "no" ]; then
|
|
if [ -n "${RUNC_USE_SYSTEMD}" ]; then
|
|
if [ "$(id -u)" = "0" ]; then
|
|
check_cgroup_value "cgroup.controllers" "$(cat /sys/fs/cgroup/machine.slice/cgroup.controllers)"
|
|
else
|
|
# shellcheck disable=SC2046
|
|
check_cgroup_value "cgroup.controllers" "$(cat /sys/fs/cgroup/user.slice/user-$(id -u).slice/cgroup.controllers)"
|
|
fi
|
|
else
|
|
check_cgroup_value "cgroup.controllers" "$(cat /sys/fs/cgroup/cgroup.controllers)"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
@test "runc exec (limits + cgrouppath + permission on the cgroup dir) succeeds" {
|
|
[[ "$ROOTLESS" -ne 0 ]] && requires rootless_cgroup
|
|
|
|
set_cgroups_path
|
|
set_resources_limit
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_permissions
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc exec test_cgroups_permissions echo "cgroups_exec"
|
|
[ "$status" -eq 0 ]
|
|
[[ ${lines[0]} == *"cgroups_exec"* ]]
|
|
}
|
|
|
|
@test "runc exec (cgroup v2 + init process in non-root cgroup) succeeds" {
|
|
requires root cgroups_v2
|
|
|
|
set_cgroups_path
|
|
set_cgroup_mount_writable
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_group
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc exec test_cgroups_group cat /sys/fs/cgroup/cgroup.controllers
|
|
[ "$status" -eq 0 ]
|
|
[[ ${lines[0]} == *"memory"* ]]
|
|
|
|
runc exec test_cgroups_group cat /proc/self/cgroup
|
|
[ "$status" -eq 0 ]
|
|
[[ ${lines[0]} == "0::/" ]]
|
|
|
|
runc exec test_cgroups_group mkdir /sys/fs/cgroup/foo
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc exec test_cgroups_group sh -c "echo 1 > /sys/fs/cgroup/foo/cgroup.procs"
|
|
[ "$status" -eq 0 ]
|
|
|
|
# the init process is now in "/foo", but an exec process can still join "/"
|
|
# because we haven't enabled any domain controller.
|
|
runc exec test_cgroups_group cat /proc/self/cgroup
|
|
[ "$status" -eq 0 ]
|
|
[[ ${lines[0]} == "0::/" ]]
|
|
|
|
# turn on a domain controller (memory)
|
|
runc exec test_cgroups_group sh -euxc 'echo $$ > /sys/fs/cgroup/foo/cgroup.procs; echo +memory > /sys/fs/cgroup/cgroup.subtree_control'
|
|
[ "$status" -eq 0 ]
|
|
|
|
# an exec process can no longer join "/" after turning on a domain controller.
|
|
# falls back to "/foo".
|
|
runc exec test_cgroups_group cat /proc/self/cgroup
|
|
[ "$status" -eq 0 ]
|
|
[[ ${lines[0]} == "0::/foo" ]]
|
|
|
|
# teardown: remove "/foo"
|
|
# shellcheck disable=SC2016
|
|
runc exec test_cgroups_group sh -uxc 'echo -memory > /sys/fs/cgroup/cgroup.subtree_control; for f in $(cat /sys/fs/cgroup/foo/cgroup.procs); do echo $f > /sys/fs/cgroup/cgroup.procs; done; rmdir /sys/fs/cgroup/foo'
|
|
runc exec test_cgroups_group test ! -d /sys/fs/cgroup/foo
|
|
[ "$status" -eq 0 ]
|
|
#
|
|
}
|
|
|
|
@test "runc run (cgroup v1 + unified resources should fail)" {
|
|
requires root cgroups_v1
|
|
|
|
set_cgroups_path
|
|
set_resources_limit
|
|
update_config '.linux.resources.unified |= {"memory.min": "131072"}'
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
|
|
[ "$status" -ne 0 ]
|
|
[[ "$output" == *'invalid configuration'* ]]
|
|
}
|
|
|
|
@test "runc run (blkio weight)" {
|
|
requires root cgroups_v2
|
|
|
|
set_cgroups_path "$BUSYBOX_BUNDLE"
|
|
update_config '.linux.resources.blockIO |= {"weight": 750}' "${BUSYBOX_BUNDLE}"
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc exec test_cgroups_unified sh -c 'cat /sys/fs/cgroup/io.bfq.weight'
|
|
[ "$status" -eq 0 ]
|
|
[ "$output" = 'default 750' ]
|
|
}
|
|
|
|
@test "runc run (cgroup v2 resources.unified only)" {
|
|
requires root cgroups_v2
|
|
|
|
set_cgroups_path
|
|
update_config ' .linux.resources.unified |= {
|
|
"memory.min": "131072",
|
|
"memory.low": "524288",
|
|
"memory.high": "5242880",
|
|
"memory.max": "10485760",
|
|
"memory.swap.max": "20971520",
|
|
"pids.max": "99",
|
|
"cpu.max": "10000 100000",
|
|
"cpu.weight": "42"
|
|
}'
|
|
|
|
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 . *.min *.max *.low *.high'
|
|
[ "$status" -eq 0 ]
|
|
echo "$output"
|
|
|
|
echo "$output" | grep -q '^memory.min:131072$'
|
|
echo "$output" | grep -q '^memory.low:524288$'
|
|
echo "$output" | grep -q '^memory.high:5242880$'
|
|
echo "$output" | grep -q '^memory.max:10485760$'
|
|
echo "$output" | grep -q '^memory.swap.max:20971520$'
|
|
echo "$output" | grep -q '^pids.max:99$'
|
|
echo "$output" | grep -q '^cpu.max:10000 100000$'
|
|
|
|
check_systemd_value "MemoryMin" 131072
|
|
check_systemd_value "MemoryLow" 524288
|
|
check_systemd_value "MemoryHigh" 5242880
|
|
check_systemd_value "MemoryMax" 10485760
|
|
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 override)" {
|
|
requires root cgroups_v2
|
|
|
|
set_cgroups_path
|
|
# CPU shares of 3333 corresponds to CPU weight of 128.
|
|
update_config ' .linux.resources.memory |= {"limit": 33554432}
|
|
| .linux.resources.memorySwap |= {"limit": 33554432}
|
|
| .linux.resources.cpu |= {
|
|
"shares": 3333,
|
|
"quota": 40000,
|
|
"period": 100000
|
|
}
|
|
| .linux.resources.unified |= {
|
|
"memory.min": "131072",
|
|
"memory.max": "10485760",
|
|
"pids.max": "42",
|
|
"cpu.max": "5000 50000",
|
|
"cpu.weight": "42"
|
|
}'
|
|
|
|
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
|
|
[ "$status" -eq 0 ]
|
|
|
|
runc exec test_cgroups_unified cat /sys/fs/cgroup/memory.min
|
|
[ "$status" -eq 0 ]
|
|
[ "$output" = '131072' ]
|
|
|
|
runc exec test_cgroups_unified cat /sys/fs/cgroup/memory.max
|
|
[ "$status" -eq 0 ]
|
|
[ "$output" = '10485760' ]
|
|
|
|
runc exec test_cgroups_unified cat /sys/fs/cgroup/pids.max
|
|
[ "$status" -eq 0 ]
|
|
[ "$output" = '42' ]
|
|
check_systemd_value "TasksMax" 42
|
|
|
|
check_cpu_quota 5000 50000 "100ms"
|
|
|
|
check_cpu_weight 42
|
|
}
|