Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgroup-resources,cgroupv2: allow setting memory.swap.max to 0 #1241

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/libcrun/cgroup-resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,10 @@ write_memory_swap (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_res
return 0;

swap = memory->swap;
if (cgroup2 && memory->swap != -1)
// Cgroupv2 apply limit must check if swap > 0, since `0` and `-1` are special case
// 0: This means process will not be able to use any swap space.
// -1: This means that the process can use as much swap as it needs.
if (cgroup2 && memory->swap > 0)
{
if (! memory->limit_present)
return crun_make_error (err, 0, "cannot set swap limit without the memory limit");
Expand Down
24 changes: 24 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ def test_resources_cpu_weight():
run_crun_command(["delete", "-f", cid])
return 0

def test_resources_cgroupv2_swap_0():
if not is_cgroup_v2_unified() or is_rootless():
return 77

conf = base_config()
add_all_namespaces(conf, cgroupns=True)
conf['process']['args'] = ['/init', 'pause']

conf['linux']['resources'] = {}
conf['linux']['resources']['memory'] = {
"swap": 0
}
cid = None
try:
_, cid = run_and_get_output(conf, command='run', detach=True)
out = run_crun_command(["exec", cid, "/init", "cat", "/sys/fs/cgroup/memory.swap.max"])
if "0" not in out:
return -1
finally:
if cid is not None:
run_crun_command(["delete", "-f", cid])
return 0

def test_resources_cpu_quota_minus_one():
if is_cgroup_v2_unified() or is_rootless():
return 77
Expand Down Expand Up @@ -303,6 +326,7 @@ def test_resources_exec_cgroup():


all_tests = {
"resources-v2-swap-disabled": test_resources_cgroupv2_swap_0,
"resources-pid-limit" : test_resources_pid_limit,
"resources-pid-limit-userns" : test_resources_pid_limit_userns,
"resources-unified" : test_resources_unified,
Expand Down