cgroup_pids.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include <linux/kernel.h>
  34. #include <linux/threads.h>
  35. #include <linux/atomic.h>
  36. #include <linux/cgroup.h>
  37. #include <linux/slab.h>
  38. #define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
  39. #define PIDS_MAX_STR "max"
  40. struct pids_cgroup {
  41. struct cgroup_subsys_state css;
  42. /*
  43. * Use 64-bit types so that we can safely represent "max" as
  44. * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
  45. */
  46. atomic64_t counter;
  47. int64_t limit;
  48. };
  49. static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css)
  50. {
  51. return container_of(css, struct pids_cgroup, css);
  52. }
  53. static struct pids_cgroup *parent_pids(struct pids_cgroup *pids)
  54. {
  55. return css_pids(pids->css.parent);
  56. }
  57. static struct cgroup_subsys_state *
  58. pids_css_alloc(struct cgroup_subsys_state *parent)
  59. {
  60. struct pids_cgroup *pids;
  61. pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL);
  62. if (!pids)
  63. return ERR_PTR(-ENOMEM);
  64. pids->limit = PIDS_MAX;
  65. atomic64_set(&pids->counter, 0);
  66. return &pids->css;
  67. }
  68. static void pids_css_free(struct cgroup_subsys_state *css)
  69. {
  70. kfree(css_pids(css));
  71. }
  72. /**
  73. * pids_cancel - uncharge the local pid count
  74. * @pids: the pid cgroup state
  75. * @num: the number of pids to cancel
  76. *
  77. * This function will WARN if the pid count goes under 0, because such a case is
  78. * a bug in the pids controller proper.
  79. */
  80. static void pids_cancel(struct pids_cgroup *pids, int num)
  81. {
  82. /*
  83. * A negative count (or overflow for that matter) is invalid,
  84. * and indicates a bug in the `pids` controller proper.
  85. */
  86. WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter));
  87. }
  88. /**
  89. * pids_uncharge - hierarchically uncharge the pid count
  90. * @pids: the pid cgroup state
  91. * @num: the number of pids to uncharge
  92. */
  93. static void pids_uncharge(struct pids_cgroup *pids, int num)
  94. {
  95. struct pids_cgroup *p;
  96. for (p = pids; parent_pids(p); p = parent_pids(p))
  97. pids_cancel(p, num);
  98. }
  99. /**
  100. * pids_charge - hierarchically charge the pid count
  101. * @pids: the pid cgroup state
  102. * @num: the number of pids to charge
  103. *
  104. * This function does *not* follow the pid limit set. It cannot fail and the new
  105. * pid count may exceed the limit. This is only used for reverting failed
  106. * attaches, where there is no other way out than violating the limit.
  107. */
  108. static void pids_charge(struct pids_cgroup *pids, int num)
  109. {
  110. struct pids_cgroup *p;
  111. for (p = pids; parent_pids(p); p = parent_pids(p))
  112. atomic64_add(num, &p->counter);
  113. }
  114. /**
  115. * pids_try_charge - hierarchically try to charge the pid count
  116. * @pids: the pid cgroup state
  117. * @num: the number of pids to charge
  118. *
  119. * This function follows the set limit. It will fail if the charge would cause
  120. * the new value to exceed the hierarchical limit. Returns 0 if the charge
  121. * succeded, otherwise -EAGAIN.
  122. */
  123. static int pids_try_charge(struct pids_cgroup *pids, int num)
  124. {
  125. struct pids_cgroup *p, *q;
  126. for (p = pids; parent_pids(p); p = parent_pids(p)) {
  127. int64_t new = atomic64_add_return(num, &p->counter);
  128. /*
  129. * Since new is capped to the maximum number of pid_t, if
  130. * p->limit is %PIDS_MAX then we know that this test will never
  131. * fail.
  132. */
  133. if (new > p->limit)
  134. goto revert;
  135. }
  136. return 0;
  137. revert:
  138. for (q = pids; q != p; q = parent_pids(q))
  139. pids_cancel(q, num);
  140. pids_cancel(p, num);
  141. return -EAGAIN;
  142. }
  143. static int pids_can_attach(struct cgroup_taskset *tset)
  144. {
  145. struct task_struct *task;
  146. struct cgroup_subsys_state *dst_css;
  147. cgroup_taskset_for_each(task, dst_css, tset) {
  148. struct pids_cgroup *pids = css_pids(dst_css);
  149. struct cgroup_subsys_state *old_css;
  150. struct pids_cgroup *old_pids;
  151. /*
  152. * No need to pin @old_css between here and cancel_attach()
  153. * because cgroup core protects it from being freed before
  154. * the migration completes or fails.
  155. */
  156. old_css = task_css(task, pids_cgrp_id);
  157. old_pids = css_pids(old_css);
  158. pids_charge(pids, 1);
  159. pids_uncharge(old_pids, 1);
  160. }
  161. return 0;
  162. }
  163. static void pids_cancel_attach(struct cgroup_taskset *tset)
  164. {
  165. struct task_struct *task;
  166. struct cgroup_subsys_state *dst_css;
  167. cgroup_taskset_for_each(task, dst_css, tset) {
  168. struct pids_cgroup *pids = css_pids(dst_css);
  169. struct cgroup_subsys_state *old_css;
  170. struct pids_cgroup *old_pids;
  171. old_css = task_css(task, pids_cgrp_id);
  172. old_pids = css_pids(old_css);
  173. pids_charge(old_pids, 1);
  174. pids_uncharge(pids, 1);
  175. }
  176. }
  177. /*
  178. * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
  179. * on threadgroup_change_begin() held by the copy_process().
  180. */
  181. static int pids_can_fork(struct task_struct *task, void **priv_p)
  182. {
  183. struct cgroup_subsys_state *css;
  184. struct pids_cgroup *pids;
  185. css = task_css_check(current, pids_cgrp_id, true);
  186. pids = css_pids(css);
  187. return pids_try_charge(pids, 1);
  188. }
  189. static void pids_cancel_fork(struct task_struct *task, void *priv)
  190. {
  191. struct cgroup_subsys_state *css;
  192. struct pids_cgroup *pids;
  193. css = task_css_check(current, pids_cgrp_id, true);
  194. pids = css_pids(css);
  195. pids_uncharge(pids, 1);
  196. }
  197. static void pids_free(struct task_struct *task)
  198. {
  199. struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id));
  200. pids_uncharge(pids, 1);
  201. }
  202. static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf,
  203. size_t nbytes, loff_t off)
  204. {
  205. struct cgroup_subsys_state *css = of_css(of);
  206. struct pids_cgroup *pids = css_pids(css);
  207. int64_t limit;
  208. int err;
  209. buf = strstrip(buf);
  210. if (!strcmp(buf, PIDS_MAX_STR)) {
  211. limit = PIDS_MAX;
  212. goto set_limit;
  213. }
  214. err = kstrtoll(buf, 0, &limit);
  215. if (err)
  216. return err;
  217. if (limit < 0 || limit >= PIDS_MAX)
  218. return -EINVAL;
  219. set_limit:
  220. /*
  221. * Limit updates don't need to be mutex'd, since it isn't
  222. * critical that any racing fork()s follow the new limit.
  223. */
  224. pids->limit = limit;
  225. return nbytes;
  226. }
  227. static int pids_max_show(struct seq_file *sf, void *v)
  228. {
  229. struct cgroup_subsys_state *css = seq_css(sf);
  230. struct pids_cgroup *pids = css_pids(css);
  231. int64_t limit = pids->limit;
  232. if (limit >= PIDS_MAX)
  233. seq_printf(sf, "%s\n", PIDS_MAX_STR);
  234. else
  235. seq_printf(sf, "%lld\n", limit);
  236. return 0;
  237. }
  238. static s64 pids_current_read(struct cgroup_subsys_state *css,
  239. struct cftype *cft)
  240. {
  241. struct pids_cgroup *pids = css_pids(css);
  242. return atomic64_read(&pids->counter);
  243. }
  244. static struct cftype pids_files[] = {
  245. {
  246. .name = "max",
  247. .write = pids_max_write,
  248. .seq_show = pids_max_show,
  249. .flags = CFTYPE_NOT_ON_ROOT,
  250. },
  251. {
  252. .name = "current",
  253. .read_s64 = pids_current_read,
  254. .flags = CFTYPE_NOT_ON_ROOT,
  255. },
  256. { } /* terminate */
  257. };
  258. struct cgroup_subsys pids_cgrp_subsys = {
  259. .css_alloc = pids_css_alloc,
  260. .css_free = pids_css_free,
  261. .can_attach = pids_can_attach,
  262. .cancel_attach = pids_cancel_attach,
  263. .can_fork = pids_can_fork,
  264. .cancel_fork = pids_cancel_fork,
  265. .free = pids_free,
  266. .legacy_cftypes = pids_files,
  267. .dfl_cftypes = pids_files,
  268. };