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