Skip to content

Commit 7ec2582

Browse files
committed
runc run: treat pids.limit=0 as unlimited
It has been pointed out that runc incorrectly ignores pids.limit=0 set in the runtime spec. This happens because runtime-spec says "default is unlimited" and also allows for Pids to not be set at all, thus distinguishing unset (Resources.Pids == nil) from unlimited (Resources.Pids.Limit <= 0). Internally, runc also distinguishes unset from unlimited, but since we do not use a pointer, we treat 0 as unset and -1 as unlimited. Add a conversion code to libcontainer/specconv. Add a test case to check that starting a container with pids.limit=0 results in no pids limit (the test fails before the fix when systemd cgroup manager is used, as systemd apparently uses parent's TasksMax). NOTE that runc update still treats 0 as "unset". Finally, fix/update the documentation here and there. Should fix issue 4014. Reported-by: Peter Hunt <[email protected]> Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 1fc83f0 commit 7ec2582

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

libcontainer/configs/cgroup_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type Resources struct {
9090
// cgroup SCHED_IDLE
9191
CPUIdle *int64 `json:"cpu_idle,omitempty"`
9292

93-
// Process limit; set <= `0' to disable limit.
93+
// Maximum number of tasks; 0 for unset, -1 for max/unlimited.
9494
PidsLimit int64 `json:"pids_limit"`
9595

9696
// Specifies per cgroup weight, range is from 10 to 1000.

libcontainer/specconv/spec_linux.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,14 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
775775
c.Resources.CpusetMems = r.CPU.Mems
776776
c.Resources.CPUIdle = r.CPU.Idle
777777
}
778+
// Convert pids limit from the runtime-spec value (where any value <= 0 means "unlimited")
779+
// to internal runc value (where -1 is "unlimited", and 0 is "unset").
778780
if r.Pids != nil {
779-
c.Resources.PidsLimit = r.Pids.Limit
781+
if r.Pids.Limit > 0 {
782+
c.Resources.PidsLimit = r.Pids.Limit
783+
} else {
784+
c.Resources.PidsLimit = -1
785+
}
780786
}
781787
if r.BlockIO != nil {
782788
if r.BlockIO.Weight != nil {

man/runc-update.8.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ stdin. If this option is used, all other options are ignored.
8585
(i.e. use unlimited swap).
8686

8787
**--pids-limit** _num_
88-
: Set the maximum number of processes allowed in the container.
88+
: Set the maximum number of tasks. Use **-1** for unlimited.
8989

9090
**--l3-cache-schema** _value_
9191
: Set the value for Intel RDT/CAT L3 cache schema.

tests/integration/cgroups.bats

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ convert_hugetlb_size() {
263263
done
264264
}
265265

266+
# https://github.com/opencontainers/runc/issues/4014.
267+
@test "runc run (pids.limit=0 means unlimited)" {
268+
[ $EUID -ne 0 ] && requires rootless_cgroup
269+
270+
set_cgroups_path
271+
update_config '.linux.resources.pids.limit |= 0'
272+
273+
runc run -d --console-socket "$CONSOLE_SOCKET" test_pids
274+
[ "$status" -eq 0 ]
275+
check_cgroup_value "pids.max" "max"
276+
# systemd < v227 shows UINT64_MAX instead of "infinity".
277+
check_systemd_value "TasksMax" "infinity" "18446744073709551615"
278+
}
279+
266280
@test "runc run (cgroup v2 resources.unified only)" {
267281
requires root cgroups_v2
268282

update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ other options are ignored.
122122
},
123123
cli.StringFlag{
124124
Name: "memory-swap",
125-
Usage: "Total memory usage (memory + swap); set '-1' to enable unlimited swap",
125+
Usage: "Total memory usage (memory + swap); use '-1' to enable unlimited swap",
126126
},
127127
cli.IntFlag{
128128
Name: "pids-limit",
129-
Usage: "Maximum number of pids allowed in the container",
129+
Usage: "Maximum number of tasks; use '-1' for unlimited",
130130
},
131131
cli.StringFlag{
132132
Name: "l3-cache-schema",

0 commit comments

Comments
 (0)