blob: 1a078b5d281ae5e22e8ab88a944eb39611cec427 [file] [log] [blame]
Aleksa Sarai917d8e22015-06-13 03:21:58 +10001 Process Number Controller
2 =========================
3
4Abstract
5--------
6
7The process number controller is used to allow a cgroup hierarchy to stop any
8new tasks from being fork()'d or clone()'d after a certain limit is reached.
9
10Since it is trivial to hit the task limit without hitting any kmemcg limits in
11place, PIDs are a fundamental resource. As such, PID exhaustion must be
12preventable in the scope of a cgroup hierarchy by allowing resource limiting of
13the number of tasks in a cgroup.
14
15Usage
16-----
17
18In order to use the `pids` controller, set the maximum number of tasks in
19pids.max (this is not available in the root cgroup for obvious reasons). The
20number of processes currently in the cgroup is given by pids.current.
21
22Organisational operations are not blocked by cgroup policies, so it is possible
23to have pids.current > pids.max. This can be done by either setting the limit to
24be smaller than pids.current, or attaching enough processes to the cgroup such
25that pids.current > pids.max. However, it is not possible to violate a cgroup
26policy through fork() or clone(). fork() and clone() will return -EAGAIN if the
27creation of a new process would cause a cgroup policy to be violated.
28
29To set a cgroup to have no limit, set pids.max to "max". This is the default for
30all new cgroups (N.B. that PID limits are hierarchical, so the most stringent
31limit in the hierarchy is followed).
32
33pids.current tracks all child cgroup hierarchies, so parent/pids.current is a
34superset of parent/child/pids.current.
35
36Example
37-------
38
39First, we mount the pids controller:
40# mkdir -p /sys/fs/cgroup/pids
41# mount -t cgroup -o pids none /sys/fs/cgroup/pids
42
43Then we create a hierarchy, set limits and attach processes to it:
44# mkdir -p /sys/fs/cgroup/pids/parent/child
45# echo 2 > /sys/fs/cgroup/pids/parent/pids.max
46# echo $$ > /sys/fs/cgroup/pids/parent/cgroup.procs
47# cat /sys/fs/cgroup/pids/parent/pids.current
482
49#
50
51It should be noted that attempts to overcome the set limit (2 in this case) will
52fail:
53
54# cat /sys/fs/cgroup/pids/parent/pids.current
552
56# ( /bin/echo "Here's some processes for you." | cat )
57sh: fork: Resource temporary unavailable
58#
59
60Even if we migrate to a child cgroup (which doesn't have a set limit), we will
61not be able to overcome the most stringent limit in the hierarchy (in this case,
62parent's):
63
64# echo $$ > /sys/fs/cgroup/pids/parent/child/cgroup.procs
65# cat /sys/fs/cgroup/pids/parent/pids.current
662
67# cat /sys/fs/cgroup/pids/parent/child/pids.current
682
69# cat /sys/fs/cgroup/pids/parent/child/pids.max
70max
71# ( /bin/echo "Here's some processes for you." | cat )
72sh: fork: Resource temporary unavailable
73#
74
75We can set a limit that is smaller than pids.current, which will stop any new
76processes from being forked at all (note that the shell itself counts towards
77pids.current):
78
79# echo 1 > /sys/fs/cgroup/pids/parent/pids.max
80# /bin/echo "We can't even spawn a single process now."
81sh: fork: Resource temporary unavailable
82# echo 0 > /sys/fs/cgroup/pids/parent/pids.max
83# /bin/echo "We can't even spawn a single process now."
84sh: fork: Resource temporary unavailable
85#