1 // SPDX-License-Identifier: GPL-2.0-only
3 * Process number limiting controller for cgroups.
5 * Used to allow a cgroup hierarchy to stop any new processes from fork()ing
6 * after a certain limit is reached.
8 * Since it is trivial to hit the task limit without hitting any kmemcg limits
9 * in place, PIDs are a fundamental resource. As such, PID exhaustion must be
10 * preventable in the scope of a cgroup hierarchy by allowing resource limiting
11 * of the number of tasks in a cgroup.
13 * In order to use the `pids` controller, set the maximum number of tasks in
14 * pids.max (this is not available in the root cgroup for obvious reasons). The
15 * number of processes currently in the cgroup is given by pids.current.
16 * Organisational operations are not blocked by cgroup policies, so it is
17 * possible to have pids.current > pids.max. However, it is not possible to
18 * violate a cgroup policy through fork(). fork() will return -EAGAIN if forking
19 * would cause a cgroup policy to be violated.
21 * To set a cgroup to have no limit, set pids.max to "max". This is the default
22 * for all new cgroups (N.B. that PID limits are hierarchical, so the most
23 * stringent limit in the hierarchy is followed).
25 * pids.current tracks all child cgroup hierarchies, so parent/pids.current is
26 * a superset of parent/child/pids.current.
28 * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com>
31 #include <linux/kernel.h>
32 #include <linux/threads.h>
33 #include <linux/atomic.h>
34 #include <linux/cgroup.h>
35 #include <linux/slab.h>
37 #define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
38 #define PIDS_MAX_STR "max"
41 struct cgroup_subsys_state css
;
44 * Use 64-bit types so that we can safely represent "max" as
45 * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
50 /* Handle for "pids.events" */
51 struct cgroup_file events_file
;
53 /* Number of times fork failed because limit was hit. */
54 atomic64_t events_limit
;
57 static struct pids_cgroup
*css_pids(struct cgroup_subsys_state
*css
)
59 return container_of(css
, struct pids_cgroup
, css
);
62 static struct pids_cgroup
*parent_pids(struct pids_cgroup
*pids
)
64 return css_pids(pids
->css
.parent
);
67 static struct cgroup_subsys_state
*
68 pids_css_alloc(struct cgroup_subsys_state
*parent
)
70 struct pids_cgroup
*pids
;
72 pids
= kzalloc(sizeof(struct pids_cgroup
), GFP_KERNEL
);
74 return ERR_PTR(-ENOMEM
);
76 atomic64_set(&pids
->counter
, 0);
77 atomic64_set(&pids
->limit
, PIDS_MAX
);
78 atomic64_set(&pids
->events_limit
, 0);
82 static void pids_css_free(struct cgroup_subsys_state
*css
)
88 * pids_cancel - uncharge the local pid count
89 * @pids: the pid cgroup state
90 * @num: the number of pids to cancel
92 * This function will WARN if the pid count goes under 0, because such a case is
93 * a bug in the pids controller proper.
95 static void pids_cancel(struct pids_cgroup
*pids
, int num
)
98 * A negative count (or overflow for that matter) is invalid,
99 * and indicates a bug in the `pids` controller proper.
101 WARN_ON_ONCE(atomic64_add_negative(-num
, &pids
->counter
));
105 * pids_uncharge - hierarchically uncharge the pid count
106 * @pids: the pid cgroup state
107 * @num: the number of pids to uncharge
109 static void pids_uncharge(struct pids_cgroup
*pids
, int num
)
111 struct pids_cgroup
*p
;
113 for (p
= pids
; parent_pids(p
); p
= parent_pids(p
))
118 * pids_charge - hierarchically charge the pid count
119 * @pids: the pid cgroup state
120 * @num: the number of pids to charge
122 * This function does *not* follow the pid limit set. It cannot fail and the new
123 * pid count may exceed the limit. This is only used for reverting failed
124 * attaches, where there is no other way out than violating the limit.
126 static void pids_charge(struct pids_cgroup
*pids
, int num
)
128 struct pids_cgroup
*p
;
130 for (p
= pids
; parent_pids(p
); p
= parent_pids(p
))
131 atomic64_add(num
, &p
->counter
);
135 * pids_try_charge - hierarchically try to charge the pid count
136 * @pids: the pid cgroup state
137 * @num: the number of pids to charge
139 * This function follows the set limit. It will fail if the charge would cause
140 * the new value to exceed the hierarchical limit. Returns 0 if the charge
141 * succeeded, otherwise -EAGAIN.
143 static int pids_try_charge(struct pids_cgroup
*pids
, int num
)
145 struct pids_cgroup
*p
, *q
;
147 for (p
= pids
; parent_pids(p
); p
= parent_pids(p
)) {
148 int64_t new = atomic64_add_return(num
, &p
->counter
);
149 int64_t limit
= atomic64_read(&p
->limit
);
152 * Since new is capped to the maximum number of pid_t, if
153 * p->limit is %PIDS_MAX then we know that this test will never
163 for (q
= pids
; q
!= p
; q
= parent_pids(q
))
170 static int pids_can_attach(struct cgroup_taskset
*tset
)
172 struct task_struct
*task
;
173 struct cgroup_subsys_state
*dst_css
;
175 cgroup_taskset_for_each(task
, dst_css
, tset
) {
176 struct pids_cgroup
*pids
= css_pids(dst_css
);
177 struct cgroup_subsys_state
*old_css
;
178 struct pids_cgroup
*old_pids
;
181 * No need to pin @old_css between here and cancel_attach()
182 * because cgroup core protects it from being freed before
183 * the migration completes or fails.
185 old_css
= task_css(task
, pids_cgrp_id
);
186 old_pids
= css_pids(old_css
);
188 pids_charge(pids
, 1);
189 pids_uncharge(old_pids
, 1);
195 static void pids_cancel_attach(struct cgroup_taskset
*tset
)
197 struct task_struct
*task
;
198 struct cgroup_subsys_state
*dst_css
;
200 cgroup_taskset_for_each(task
, dst_css
, tset
) {
201 struct pids_cgroup
*pids
= css_pids(dst_css
);
202 struct cgroup_subsys_state
*old_css
;
203 struct pids_cgroup
*old_pids
;
205 old_css
= task_css(task
, pids_cgrp_id
);
206 old_pids
= css_pids(old_css
);
208 pids_charge(old_pids
, 1);
209 pids_uncharge(pids
, 1);
214 * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
215 * on cgroup_threadgroup_change_begin() held by the copy_process().
217 static int pids_can_fork(struct task_struct
*task
)
219 struct cgroup_subsys_state
*css
;
220 struct pids_cgroup
*pids
;
223 css
= task_css_check(current
, pids_cgrp_id
, true);
224 pids
= css_pids(css
);
225 err
= pids_try_charge(pids
, 1);
227 /* Only log the first time events_limit is incremented. */
228 if (atomic64_inc_return(&pids
->events_limit
) == 1) {
229 pr_info("cgroup: fork rejected by pids controller in ");
230 pr_cont_cgroup_path(css
->cgroup
);
233 cgroup_file_notify(&pids
->events_file
);
238 static void pids_cancel_fork(struct task_struct
*task
)
240 struct cgroup_subsys_state
*css
;
241 struct pids_cgroup
*pids
;
243 css
= task_css_check(current
, pids_cgrp_id
, true);
244 pids
= css_pids(css
);
245 pids_uncharge(pids
, 1);
248 static void pids_release(struct task_struct
*task
)
250 struct pids_cgroup
*pids
= css_pids(task_css(task
, pids_cgrp_id
));
252 pids_uncharge(pids
, 1);
255 static ssize_t
pids_max_write(struct kernfs_open_file
*of
, char *buf
,
256 size_t nbytes
, loff_t off
)
258 struct cgroup_subsys_state
*css
= of_css(of
);
259 struct pids_cgroup
*pids
= css_pids(css
);
264 if (!strcmp(buf
, PIDS_MAX_STR
)) {
269 err
= kstrtoll(buf
, 0, &limit
);
273 if (limit
< 0 || limit
>= PIDS_MAX
)
278 * Limit updates don't need to be mutex'd, since it isn't
279 * critical that any racing fork()s follow the new limit.
281 atomic64_set(&pids
->limit
, limit
);
285 static int pids_max_show(struct seq_file
*sf
, void *v
)
287 struct cgroup_subsys_state
*css
= seq_css(sf
);
288 struct pids_cgroup
*pids
= css_pids(css
);
289 int64_t limit
= atomic64_read(&pids
->limit
);
291 if (limit
>= PIDS_MAX
)
292 seq_printf(sf
, "%s\n", PIDS_MAX_STR
);
294 seq_printf(sf
, "%lld\n", limit
);
299 static s64
pids_current_read(struct cgroup_subsys_state
*css
,
302 struct pids_cgroup
*pids
= css_pids(css
);
304 return atomic64_read(&pids
->counter
);
307 static int pids_events_show(struct seq_file
*sf
, void *v
)
309 struct pids_cgroup
*pids
= css_pids(seq_css(sf
));
311 seq_printf(sf
, "max %lld\n", (s64
)atomic64_read(&pids
->events_limit
));
315 static struct cftype pids_files
[] = {
318 .write
= pids_max_write
,
319 .seq_show
= pids_max_show
,
320 .flags
= CFTYPE_NOT_ON_ROOT
,
324 .read_s64
= pids_current_read
,
325 .flags
= CFTYPE_NOT_ON_ROOT
,
329 .seq_show
= pids_events_show
,
330 .file_offset
= offsetof(struct pids_cgroup
, events_file
),
331 .flags
= CFTYPE_NOT_ON_ROOT
,
336 struct cgroup_subsys pids_cgrp_subsys
= {
337 .css_alloc
= pids_css_alloc
,
338 .css_free
= pids_css_free
,
339 .can_attach
= pids_can_attach
,
340 .cancel_attach
= pids_cancel_attach
,
341 .can_fork
= pids_can_fork
,
342 .cancel_fork
= pids_cancel_fork
,
343 .release
= pids_release
,
344 .legacy_cftypes
= pids_files
,
345 .dfl_cftypes
= pids_files
,