Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Process number limiting controller for cgroups. |
| 3 | * |
| 4 | * Used to allow a cgroup hierarchy to stop any new processes from fork()ing |
| 5 | * after a certain limit is reached. |
| 6 | * |
| 7 | * Since it is trivial to hit the task limit without hitting any kmemcg limits |
| 8 | * in place, PIDs are a fundamental resource. As such, PID exhaustion must be |
| 9 | * preventable in the scope of a cgroup hierarchy by allowing resource limiting |
| 10 | * of the number of tasks in a cgroup. |
| 11 | * |
| 12 | * In order to use the `pids` controller, set the maximum number of tasks in |
| 13 | * pids.max (this is not available in the root cgroup for obvious reasons). The |
| 14 | * number of processes currently in the cgroup is given by pids.current. |
| 15 | * Organisational operations are not blocked by cgroup policies, so it is |
| 16 | * possible to have pids.current > pids.max. However, it is not possible to |
| 17 | * violate a cgroup policy through fork(). fork() will return -EAGAIN if forking |
| 18 | * would cause a cgroup policy to be violated. |
| 19 | * |
| 20 | * To set a cgroup to have no limit, set pids.max to "max". This is the default |
| 21 | * for all new cgroups (N.B. that PID limits are hierarchical, so the most |
| 22 | * stringent limit in the hierarchy is followed). |
| 23 | * |
| 24 | * pids.current tracks all child cgroup hierarchies, so parent/pids.current is |
| 25 | * a superset of parent/child/pids.current. |
| 26 | * |
| 27 | * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com> |
| 28 | * |
| 29 | * This file is subject to the terms and conditions of version 2 of the GNU |
| 30 | * General Public License. See the file COPYING in the main directory of the |
| 31 | * Linux distribution for more details. |
| 32 | */ |
| 33 | |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/threads.h> |
| 36 | #include <linux/atomic.h> |
| 37 | #include <linux/cgroup.h> |
| 38 | #include <linux/slab.h> |
| 39 | |
| 40 | #define PIDS_MAX (PID_MAX_LIMIT + 1ULL) |
| 41 | #define PIDS_MAX_STR "max" |
| 42 | |
| 43 | struct pids_cgroup { |
| 44 | struct cgroup_subsys_state css; |
| 45 | |
| 46 | /* |
| 47 | * Use 64-bit types so that we can safely represent "max" as |
| 48 | * %PIDS_MAX = (%PID_MAX_LIMIT + 1). |
| 49 | */ |
| 50 | atomic64_t counter; |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 51 | atomic64_t limit; |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 52 | |
| 53 | /* Handle for "pids.events" */ |
| 54 | struct cgroup_file events_file; |
| 55 | |
| 56 | /* Number of times fork failed because limit was hit. */ |
| 57 | atomic64_t events_limit; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css) |
| 61 | { |
| 62 | return container_of(css, struct pids_cgroup, css); |
| 63 | } |
| 64 | |
| 65 | static struct pids_cgroup *parent_pids(struct pids_cgroup *pids) |
| 66 | { |
| 67 | return css_pids(pids->css.parent); |
| 68 | } |
| 69 | |
| 70 | static struct cgroup_subsys_state * |
| 71 | pids_css_alloc(struct cgroup_subsys_state *parent) |
| 72 | { |
| 73 | struct pids_cgroup *pids; |
| 74 | |
| 75 | pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL); |
| 76 | if (!pids) |
| 77 | return ERR_PTR(-ENOMEM); |
| 78 | |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 79 | atomic64_set(&pids->counter, 0); |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 80 | atomic64_set(&pids->limit, PIDS_MAX); |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 81 | atomic64_set(&pids->events_limit, 0); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 82 | return &pids->css; |
| 83 | } |
| 84 | |
| 85 | static void pids_css_free(struct cgroup_subsys_state *css) |
| 86 | { |
| 87 | kfree(css_pids(css)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * pids_cancel - uncharge the local pid count |
| 92 | * @pids: the pid cgroup state |
| 93 | * @num: the number of pids to cancel |
| 94 | * |
| 95 | * This function will WARN if the pid count goes under 0, because such a case is |
| 96 | * a bug in the pids controller proper. |
| 97 | */ |
| 98 | static void pids_cancel(struct pids_cgroup *pids, int num) |
| 99 | { |
| 100 | /* |
| 101 | * A negative count (or overflow for that matter) is invalid, |
| 102 | * and indicates a bug in the `pids` controller proper. |
| 103 | */ |
| 104 | WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * pids_uncharge - hierarchically uncharge the pid count |
| 109 | * @pids: the pid cgroup state |
| 110 | * @num: the number of pids to uncharge |
| 111 | */ |
| 112 | static void pids_uncharge(struct pids_cgroup *pids, int num) |
| 113 | { |
| 114 | struct pids_cgroup *p; |
| 115 | |
Tejun Heo | 67cde9c | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 116 | for (p = pids; parent_pids(p); p = parent_pids(p)) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 117 | pids_cancel(p, num); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * pids_charge - hierarchically charge the pid count |
| 122 | * @pids: the pid cgroup state |
| 123 | * @num: the number of pids to charge |
| 124 | * |
| 125 | * This function does *not* follow the pid limit set. It cannot fail and the new |
| 126 | * pid count may exceed the limit. This is only used for reverting failed |
| 127 | * attaches, where there is no other way out than violating the limit. |
| 128 | */ |
| 129 | static void pids_charge(struct pids_cgroup *pids, int num) |
| 130 | { |
| 131 | struct pids_cgroup *p; |
| 132 | |
Tejun Heo | 67cde9c | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 133 | for (p = pids; parent_pids(p); p = parent_pids(p)) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 134 | atomic64_add(num, &p->counter); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * pids_try_charge - hierarchically try to charge the pid count |
| 139 | * @pids: the pid cgroup state |
| 140 | * @num: the number of pids to charge |
| 141 | * |
| 142 | * This function follows the set limit. It will fail if the charge would cause |
| 143 | * the new value to exceed the hierarchical limit. Returns 0 if the charge |
Rami Rosen | fccd3af | 2015-12-13 22:13:08 +0200 | [diff] [blame] | 144 | * succeeded, otherwise -EAGAIN. |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 145 | */ |
| 146 | static int pids_try_charge(struct pids_cgroup *pids, int num) |
| 147 | { |
| 148 | struct pids_cgroup *p, *q; |
| 149 | |
Tejun Heo | 67cde9c | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 150 | for (p = pids; parent_pids(p); p = parent_pids(p)) { |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 151 | int64_t new = atomic64_add_return(num, &p->counter); |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 152 | int64_t limit = atomic64_read(&p->limit); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Since new is capped to the maximum number of pid_t, if |
| 156 | * p->limit is %PIDS_MAX then we know that this test will never |
| 157 | * fail. |
| 158 | */ |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 159 | if (new > limit) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 160 | goto revert; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | |
| 165 | revert: |
| 166 | for (q = pids; q != p; q = parent_pids(q)) |
| 167 | pids_cancel(q, num); |
| 168 | pids_cancel(p, num); |
| 169 | |
| 170 | return -EAGAIN; |
| 171 | } |
| 172 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 173 | static int pids_can_attach(struct cgroup_taskset *tset) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 174 | { |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 175 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 176 | struct cgroup_subsys_state *dst_css; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 177 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 178 | cgroup_taskset_for_each(task, dst_css, tset) { |
| 179 | struct pids_cgroup *pids = css_pids(dst_css); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 180 | struct cgroup_subsys_state *old_css; |
| 181 | struct pids_cgroup *old_pids; |
| 182 | |
| 183 | /* |
Aleksa Sarai | ce52399 | 2015-08-25 12:50:44 +1000 | [diff] [blame] | 184 | * No need to pin @old_css between here and cancel_attach() |
| 185 | * because cgroup core protects it from being freed before |
| 186 | * the migration completes or fails. |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 187 | */ |
Aleksa Sarai | ce52399 | 2015-08-25 12:50:44 +1000 | [diff] [blame] | 188 | old_css = task_css(task, pids_cgrp_id); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 189 | old_pids = css_pids(old_css); |
| 190 | |
| 191 | pids_charge(pids, 1); |
| 192 | pids_uncharge(old_pids, 1); |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 198 | static void pids_cancel_attach(struct cgroup_taskset *tset) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 199 | { |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 200 | struct task_struct *task; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 201 | struct cgroup_subsys_state *dst_css; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 202 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 203 | cgroup_taskset_for_each(task, dst_css, tset) { |
| 204 | struct pids_cgroup *pids = css_pids(dst_css); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 205 | struct cgroup_subsys_state *old_css; |
| 206 | struct pids_cgroup *old_pids; |
| 207 | |
| 208 | old_css = task_css(task, pids_cgrp_id); |
| 209 | old_pids = css_pids(old_css); |
| 210 | |
| 211 | pids_charge(old_pids, 1); |
| 212 | pids_uncharge(pids, 1); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Oleg Nesterov | afbcb36 | 2015-11-27 19:57:22 +0100 | [diff] [blame] | 216 | /* |
| 217 | * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies |
| 218 | * on threadgroup_change_begin() held by the copy_process(). |
| 219 | */ |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 220 | static int pids_can_fork(struct task_struct *task) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 221 | { |
| 222 | struct cgroup_subsys_state *css; |
| 223 | struct pids_cgroup *pids; |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 224 | int err; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 225 | |
Oleg Nesterov | afbcb36 | 2015-11-27 19:57:22 +0100 | [diff] [blame] | 226 | css = task_css_check(current, pids_cgrp_id, true); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 227 | pids = css_pids(css); |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 228 | err = pids_try_charge(pids, 1); |
| 229 | if (err) { |
| 230 | /* Only log the first time events_limit is incremented. */ |
| 231 | if (atomic64_inc_return(&pids->events_limit) == 1) { |
| 232 | pr_info("cgroup: fork rejected by pids controller in "); |
Tejun Heo | 228514b | 2017-03-01 15:39:07 -0500 | [diff] [blame] | 233 | pr_cont_cgroup_path(css->cgroup); |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 234 | pr_cont("\n"); |
| 235 | } |
| 236 | cgroup_file_notify(&pids->events_file); |
| 237 | } |
| 238 | return err; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 239 | } |
| 240 | |
Oleg Nesterov | b53202e | 2015-12-03 10:24:08 -0500 | [diff] [blame] | 241 | static void pids_cancel_fork(struct task_struct *task) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 242 | { |
Oleg Nesterov | afbcb36 | 2015-11-27 19:57:22 +0100 | [diff] [blame] | 243 | struct cgroup_subsys_state *css; |
| 244 | struct pids_cgroup *pids; |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 245 | |
Oleg Nesterov | afbcb36 | 2015-11-27 19:57:22 +0100 | [diff] [blame] | 246 | css = task_css_check(current, pids_cgrp_id, true); |
| 247 | pids = css_pids(css); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 248 | pids_uncharge(pids, 1); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 249 | } |
| 250 | |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 251 | static void pids_free(struct task_struct *task) |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 252 | { |
Tejun Heo | 2e91fa7 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 253 | struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id)); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 254 | |
| 255 | pids_uncharge(pids, 1); |
| 256 | } |
| 257 | |
| 258 | static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf, |
| 259 | size_t nbytes, loff_t off) |
| 260 | { |
| 261 | struct cgroup_subsys_state *css = of_css(of); |
| 262 | struct pids_cgroup *pids = css_pids(css); |
| 263 | int64_t limit; |
| 264 | int err; |
| 265 | |
| 266 | buf = strstrip(buf); |
| 267 | if (!strcmp(buf, PIDS_MAX_STR)) { |
| 268 | limit = PIDS_MAX; |
| 269 | goto set_limit; |
| 270 | } |
| 271 | |
| 272 | err = kstrtoll(buf, 0, &limit); |
| 273 | if (err) |
| 274 | return err; |
| 275 | |
| 276 | if (limit < 0 || limit >= PIDS_MAX) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | set_limit: |
| 280 | /* |
| 281 | * Limit updates don't need to be mutex'd, since it isn't |
| 282 | * critical that any racing fork()s follow the new limit. |
| 283 | */ |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 284 | atomic64_set(&pids->limit, limit); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 285 | return nbytes; |
| 286 | } |
| 287 | |
| 288 | static int pids_max_show(struct seq_file *sf, void *v) |
| 289 | { |
| 290 | struct cgroup_subsys_state *css = seq_css(sf); |
| 291 | struct pids_cgroup *pids = css_pids(css); |
Aleksa Sarai | 93b2cdd | 2019-10-17 02:50:01 +1100 | [diff] [blame] | 292 | int64_t limit = atomic64_read(&pids->limit); |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 293 | |
| 294 | if (limit >= PIDS_MAX) |
| 295 | seq_printf(sf, "%s\n", PIDS_MAX_STR); |
| 296 | else |
| 297 | seq_printf(sf, "%lld\n", limit); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static s64 pids_current_read(struct cgroup_subsys_state *css, |
| 303 | struct cftype *cft) |
| 304 | { |
| 305 | struct pids_cgroup *pids = css_pids(css); |
| 306 | |
| 307 | return atomic64_read(&pids->counter); |
| 308 | } |
| 309 | |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 310 | static int pids_events_show(struct seq_file *sf, void *v) |
| 311 | { |
| 312 | struct pids_cgroup *pids = css_pids(seq_css(sf)); |
| 313 | |
Kenny Yu | 9f6870d | 2016-06-21 11:55:35 -0700 | [diff] [blame] | 314 | seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pids->events_limit)); |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 318 | static struct cftype pids_files[] = { |
| 319 | { |
| 320 | .name = "max", |
| 321 | .write = pids_max_write, |
| 322 | .seq_show = pids_max_show, |
| 323 | .flags = CFTYPE_NOT_ON_ROOT, |
| 324 | }, |
| 325 | { |
| 326 | .name = "current", |
| 327 | .read_s64 = pids_current_read, |
Tejun Heo | 67cde9c | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 328 | .flags = CFTYPE_NOT_ON_ROOT, |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 329 | }, |
Kenny Yu | 135b8b3 | 2016-06-21 14:04:36 -0400 | [diff] [blame] | 330 | { |
| 331 | .name = "events", |
| 332 | .seq_show = pids_events_show, |
| 333 | .file_offset = offsetof(struct pids_cgroup, events_file), |
| 334 | .flags = CFTYPE_NOT_ON_ROOT, |
| 335 | }, |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 336 | { } /* terminate */ |
| 337 | }; |
| 338 | |
| 339 | struct cgroup_subsys pids_cgrp_subsys = { |
| 340 | .css_alloc = pids_css_alloc, |
| 341 | .css_free = pids_css_free, |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 342 | .can_attach = pids_can_attach, |
| 343 | .cancel_attach = pids_cancel_attach, |
| 344 | .can_fork = pids_can_fork, |
| 345 | .cancel_fork = pids_cancel_fork, |
Tejun Heo | afcf6c8 | 2015-10-15 16:41:53 -0400 | [diff] [blame] | 346 | .free = pids_free, |
Aleksa Sarai | 49b786e | 2015-06-09 21:32:10 +1000 | [diff] [blame] | 347 | .legacy_cftypes = pids_files, |
| 348 | .dfl_cftypes = pids_files, |
| 349 | }; |