blob: 8e513a573fe9e50979bb98a1e8da83dba8f082cc [file] [log] [blame]
Thomas Gleixnerf85d2082019-06-04 10:10:45 +02001// SPDX-License-Identifier: GPL-2.0-only
Aleksa Sarai49b786e2015-06-09 21:32:10 +10002/*
3 * Process number limiting controller for cgroups.
4 *
5 * Used to allow a cgroup hierarchy to stop any new processes from fork()ing
6 * after a certain limit is reached.
7 *
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.
12 *
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.
20 *
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).
24 *
25 * pids.current tracks all child cgroup hierarchies, so parent/pids.current is
26 * a superset of parent/child/pids.current.
27 *
28 * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com>
Aleksa Sarai49b786e2015-06-09 21:32:10 +100029 */
30
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>
36
37#define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
38#define PIDS_MAX_STR "max"
39
40struct pids_cgroup {
41 struct cgroup_subsys_state css;
42
43 /*
44 * Use 64-bit types so that we can safely represent "max" as
45 * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
46 */
47 atomic64_t counter;
48 int64_t limit;
Kenny Yu135b8b32016-06-21 14:04:36 -040049
50 /* Handle for "pids.events" */
51 struct cgroup_file events_file;
52
53 /* Number of times fork failed because limit was hit. */
54 atomic64_t events_limit;
Aleksa Sarai49b786e2015-06-09 21:32:10 +100055};
56
57static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css)
58{
59 return container_of(css, struct pids_cgroup, css);
60}
61
62static struct pids_cgroup *parent_pids(struct pids_cgroup *pids)
63{
64 return css_pids(pids->css.parent);
65}
66
67static struct cgroup_subsys_state *
68pids_css_alloc(struct cgroup_subsys_state *parent)
69{
70 struct pids_cgroup *pids;
71
72 pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL);
73 if (!pids)
74 return ERR_PTR(-ENOMEM);
75
76 pids->limit = PIDS_MAX;
77 atomic64_set(&pids->counter, 0);
Kenny Yu135b8b32016-06-21 14:04:36 -040078 atomic64_set(&pids->events_limit, 0);
Aleksa Sarai49b786e2015-06-09 21:32:10 +100079 return &pids->css;
80}
81
82static void pids_css_free(struct cgroup_subsys_state *css)
83{
84 kfree(css_pids(css));
85}
86
87/**
88 * pids_cancel - uncharge the local pid count
89 * @pids: the pid cgroup state
90 * @num: the number of pids to cancel
91 *
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.
94 */
95static void pids_cancel(struct pids_cgroup *pids, int num)
96{
97 /*
98 * A negative count (or overflow for that matter) is invalid,
99 * and indicates a bug in the `pids` controller proper.
100 */
101 WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter));
102}
103
104/**
105 * pids_uncharge - hierarchically uncharge the pid count
106 * @pids: the pid cgroup state
107 * @num: the number of pids to uncharge
108 */
109static void pids_uncharge(struct pids_cgroup *pids, int num)
110{
111 struct pids_cgroup *p;
112
Tejun Heo67cde9c2015-12-03 10:18:21 -0500113 for (p = pids; parent_pids(p); p = parent_pids(p))
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000114 pids_cancel(p, num);
115}
116
117/**
118 * pids_charge - hierarchically charge the pid count
119 * @pids: the pid cgroup state
120 * @num: the number of pids to charge
121 *
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.
125 */
126static void pids_charge(struct pids_cgroup *pids, int num)
127{
128 struct pids_cgroup *p;
129
Tejun Heo67cde9c2015-12-03 10:18:21 -0500130 for (p = pids; parent_pids(p); p = parent_pids(p))
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000131 atomic64_add(num, &p->counter);
132}
133
134/**
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
138 *
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
Rami Rosenfccd3af2015-12-13 22:13:08 +0200141 * succeeded, otherwise -EAGAIN.
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000142 */
143static int pids_try_charge(struct pids_cgroup *pids, int num)
144{
145 struct pids_cgroup *p, *q;
146
Tejun Heo67cde9c2015-12-03 10:18:21 -0500147 for (p = pids; parent_pids(p); p = parent_pids(p)) {
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000148 int64_t new = atomic64_add_return(num, &p->counter);
149
150 /*
151 * Since new is capped to the maximum number of pid_t, if
152 * p->limit is %PIDS_MAX then we know that this test will never
153 * fail.
154 */
155 if (new > p->limit)
156 goto revert;
157 }
158
159 return 0;
160
161revert:
162 for (q = pids; q != p; q = parent_pids(q))
163 pids_cancel(q, num);
164 pids_cancel(p, num);
165
166 return -EAGAIN;
167}
168
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500169static int pids_can_attach(struct cgroup_taskset *tset)
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000170{
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000171 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500172 struct cgroup_subsys_state *dst_css;
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000173
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500174 cgroup_taskset_for_each(task, dst_css, tset) {
175 struct pids_cgroup *pids = css_pids(dst_css);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000176 struct cgroup_subsys_state *old_css;
177 struct pids_cgroup *old_pids;
178
179 /*
Aleksa Saraice523992015-08-25 12:50:44 +1000180 * No need to pin @old_css between here and cancel_attach()
181 * because cgroup core protects it from being freed before
182 * the migration completes or fails.
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000183 */
Aleksa Saraice523992015-08-25 12:50:44 +1000184 old_css = task_css(task, pids_cgrp_id);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000185 old_pids = css_pids(old_css);
186
187 pids_charge(pids, 1);
188 pids_uncharge(old_pids, 1);
189 }
190
191 return 0;
192}
193
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500194static void pids_cancel_attach(struct cgroup_taskset *tset)
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000195{
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000196 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500197 struct cgroup_subsys_state *dst_css;
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000198
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500199 cgroup_taskset_for_each(task, dst_css, tset) {
200 struct pids_cgroup *pids = css_pids(dst_css);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000201 struct cgroup_subsys_state *old_css;
202 struct pids_cgroup *old_pids;
203
204 old_css = task_css(task, pids_cgrp_id);
205 old_pids = css_pids(old_css);
206
207 pids_charge(old_pids, 1);
208 pids_uncharge(pids, 1);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000209 }
210}
211
Oleg Nesterovafbcb362015-11-27 19:57:22 +0100212/*
213 * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
Ingo Molnar780de9d2017-02-02 11:50:56 +0100214 * on cgroup_threadgroup_change_begin() held by the copy_process().
Oleg Nesterovafbcb362015-11-27 19:57:22 +0100215 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500216static int pids_can_fork(struct task_struct *task)
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000217{
218 struct cgroup_subsys_state *css;
219 struct pids_cgroup *pids;
Kenny Yu135b8b32016-06-21 14:04:36 -0400220 int err;
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000221
Oleg Nesterovafbcb362015-11-27 19:57:22 +0100222 css = task_css_check(current, pids_cgrp_id, true);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000223 pids = css_pids(css);
Kenny Yu135b8b32016-06-21 14:04:36 -0400224 err = pids_try_charge(pids, 1);
225 if (err) {
226 /* Only log the first time events_limit is incremented. */
227 if (atomic64_inc_return(&pids->events_limit) == 1) {
228 pr_info("cgroup: fork rejected by pids controller in ");
Tejun Heo1d18c272017-03-01 15:39:07 -0500229 pr_cont_cgroup_path(css->cgroup);
Kenny Yu135b8b32016-06-21 14:04:36 -0400230 pr_cont("\n");
231 }
232 cgroup_file_notify(&pids->events_file);
233 }
234 return err;
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000235}
236
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500237static void pids_cancel_fork(struct task_struct *task)
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000238{
Oleg Nesterovafbcb362015-11-27 19:57:22 +0100239 struct cgroup_subsys_state *css;
240 struct pids_cgroup *pids;
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000241
Oleg Nesterovafbcb362015-11-27 19:57:22 +0100242 css = task_css_check(current, pids_cgrp_id, true);
243 pids = css_pids(css);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000244 pids_uncharge(pids, 1);
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000245}
246
Oleg Nesterov51bee5a2019-01-28 17:00:13 +0100247static void pids_release(struct task_struct *task)
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000248{
Tejun Heo2e91fa72015-10-15 16:41:53 -0400249 struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id));
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000250
251 pids_uncharge(pids, 1);
252}
253
254static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf,
255 size_t nbytes, loff_t off)
256{
257 struct cgroup_subsys_state *css = of_css(of);
258 struct pids_cgroup *pids = css_pids(css);
259 int64_t limit;
260 int err;
261
262 buf = strstrip(buf);
263 if (!strcmp(buf, PIDS_MAX_STR)) {
264 limit = PIDS_MAX;
265 goto set_limit;
266 }
267
268 err = kstrtoll(buf, 0, &limit);
269 if (err)
270 return err;
271
272 if (limit < 0 || limit >= PIDS_MAX)
273 return -EINVAL;
274
275set_limit:
276 /*
277 * Limit updates don't need to be mutex'd, since it isn't
278 * critical that any racing fork()s follow the new limit.
279 */
280 pids->limit = limit;
281 return nbytes;
282}
283
284static int pids_max_show(struct seq_file *sf, void *v)
285{
286 struct cgroup_subsys_state *css = seq_css(sf);
287 struct pids_cgroup *pids = css_pids(css);
288 int64_t limit = pids->limit;
289
290 if (limit >= PIDS_MAX)
291 seq_printf(sf, "%s\n", PIDS_MAX_STR);
292 else
293 seq_printf(sf, "%lld\n", limit);
294
295 return 0;
296}
297
298static s64 pids_current_read(struct cgroup_subsys_state *css,
299 struct cftype *cft)
300{
301 struct pids_cgroup *pids = css_pids(css);
302
303 return atomic64_read(&pids->counter);
304}
305
Kenny Yu135b8b32016-06-21 14:04:36 -0400306static int pids_events_show(struct seq_file *sf, void *v)
307{
308 struct pids_cgroup *pids = css_pids(seq_css(sf));
309
Kenny Yu9f6870d2016-06-21 11:55:35 -0700310 seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pids->events_limit));
Kenny Yu135b8b32016-06-21 14:04:36 -0400311 return 0;
312}
313
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000314static struct cftype pids_files[] = {
315 {
316 .name = "max",
317 .write = pids_max_write,
318 .seq_show = pids_max_show,
319 .flags = CFTYPE_NOT_ON_ROOT,
320 },
321 {
322 .name = "current",
323 .read_s64 = pids_current_read,
Tejun Heo67cde9c2015-12-03 10:18:21 -0500324 .flags = CFTYPE_NOT_ON_ROOT,
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000325 },
Kenny Yu135b8b32016-06-21 14:04:36 -0400326 {
327 .name = "events",
328 .seq_show = pids_events_show,
329 .file_offset = offsetof(struct pids_cgroup, events_file),
330 .flags = CFTYPE_NOT_ON_ROOT,
331 },
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000332 { } /* terminate */
333};
334
335struct cgroup_subsys pids_cgrp_subsys = {
336 .css_alloc = pids_css_alloc,
337 .css_free = pids_css_free,
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000338 .can_attach = pids_can_attach,
339 .cancel_attach = pids_cancel_attach,
340 .can_fork = pids_can_fork,
341 .cancel_fork = pids_cancel_fork,
Oleg Nesterov51bee5a2019-01-28 17:00:13 +0100342 .release = pids_release,
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000343 .legacy_cftypes = pids_files,
344 .dfl_cftypes = pids_files,
Tejun Heo8cfd8142017-07-21 11:14:51 -0400345 .threaded = true,
Aleksa Sarai49b786e2015-06-09 21:32:10 +1000346};