cgroup-v1.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. #include "cgroup-internal.h"
  2. #include <linux/ctype.h>
  3. #include <linux/kmod.h>
  4. #include <linux/sort.h>
  5. #include <linux/delay.h>
  6. #include <linux/mm.h>
  7. #include <linux/sched/signal.h>
  8. #include <linux/sched/task.h>
  9. #include <linux/magic.h>
  10. #include <linux/slab.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/delayacct.h>
  13. #include <linux/pid_namespace.h>
  14. #include <linux/cgroupstats.h>
  15. #include <trace/events/cgroup.h>
  16. /*
  17. * pidlists linger the following amount before being destroyed. The goal
  18. * is avoiding frequent destruction in the middle of consecutive read calls
  19. * Expiring in the middle is a performance problem not a correctness one.
  20. * 1 sec should be enough.
  21. */
  22. #define CGROUP_PIDLIST_DESTROY_DELAY HZ
  23. /* Controllers blocked by the commandline in v1 */
  24. static u16 cgroup_no_v1_mask;
  25. /*
  26. * pidlist destructions need to be flushed on cgroup destruction. Use a
  27. * separate workqueue as flush domain.
  28. */
  29. static struct workqueue_struct *cgroup_pidlist_destroy_wq;
  30. /*
  31. * Protects cgroup_subsys->release_agent_path. Modifying it also requires
  32. * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
  33. */
  34. static DEFINE_SPINLOCK(release_agent_path_lock);
  35. bool cgroup1_ssid_disabled(int ssid)
  36. {
  37. return cgroup_no_v1_mask & (1 << ssid);
  38. }
  39. /**
  40. * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
  41. * @from: attach to all cgroups of a given task
  42. * @tsk: the task to be attached
  43. */
  44. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
  45. {
  46. struct cgroup_root *root;
  47. int retval = 0;
  48. mutex_lock(&cgroup_mutex);
  49. percpu_down_write(&cgroup_threadgroup_rwsem);
  50. for_each_root(root) {
  51. struct cgroup *from_cgrp;
  52. if (root == &cgrp_dfl_root)
  53. continue;
  54. spin_lock_irq(&css_set_lock);
  55. from_cgrp = task_cgroup_from_root(from, root);
  56. spin_unlock_irq(&css_set_lock);
  57. retval = cgroup_attach_task(from_cgrp, tsk, false);
  58. if (retval)
  59. break;
  60. }
  61. percpu_up_write(&cgroup_threadgroup_rwsem);
  62. mutex_unlock(&cgroup_mutex);
  63. return retval;
  64. }
  65. EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
  66. /**
  67. * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
  68. * @to: cgroup to which the tasks will be moved
  69. * @from: cgroup in which the tasks currently reside
  70. *
  71. * Locking rules between cgroup_post_fork() and the migration path
  72. * guarantee that, if a task is forking while being migrated, the new child
  73. * is guaranteed to be either visible in the source cgroup after the
  74. * parent's migration is complete or put into the target cgroup. No task
  75. * can slip out of migration through forking.
  76. */
  77. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
  78. {
  79. DEFINE_CGROUP_MGCTX(mgctx);
  80. struct cgrp_cset_link *link;
  81. struct css_task_iter it;
  82. struct task_struct *task;
  83. int ret;
  84. if (cgroup_on_dfl(to))
  85. return -EINVAL;
  86. if (!cgroup_may_migrate_to(to))
  87. return -EBUSY;
  88. mutex_lock(&cgroup_mutex);
  89. percpu_down_write(&cgroup_threadgroup_rwsem);
  90. /* all tasks in @from are being moved, all csets are source */
  91. spin_lock_irq(&css_set_lock);
  92. list_for_each_entry(link, &from->cset_links, cset_link)
  93. cgroup_migrate_add_src(link->cset, to, &mgctx);
  94. spin_unlock_irq(&css_set_lock);
  95. ret = cgroup_migrate_prepare_dst(&mgctx);
  96. if (ret)
  97. goto out_err;
  98. /*
  99. * Migrate tasks one-by-one until @from is empty. This fails iff
  100. * ->can_attach() fails.
  101. */
  102. do {
  103. css_task_iter_start(&from->self, &it);
  104. task = css_task_iter_next(&it);
  105. if (task)
  106. get_task_struct(task);
  107. css_task_iter_end(&it);
  108. if (task) {
  109. ret = cgroup_migrate(task, false, &mgctx);
  110. if (!ret)
  111. trace_cgroup_transfer_tasks(to, task, false);
  112. put_task_struct(task);
  113. }
  114. } while (task && !ret);
  115. out_err:
  116. cgroup_migrate_finish(&mgctx);
  117. percpu_up_write(&cgroup_threadgroup_rwsem);
  118. mutex_unlock(&cgroup_mutex);
  119. return ret;
  120. }
  121. /*
  122. * Stuff for reading the 'tasks'/'procs' files.
  123. *
  124. * Reading this file can return large amounts of data if a cgroup has
  125. * *lots* of attached tasks. So it may need several calls to read(),
  126. * but we cannot guarantee that the information we produce is correct
  127. * unless we produce it entirely atomically.
  128. *
  129. */
  130. /* which pidlist file are we talking about? */
  131. enum cgroup_filetype {
  132. CGROUP_FILE_PROCS,
  133. CGROUP_FILE_TASKS,
  134. };
  135. /*
  136. * A pidlist is a list of pids that virtually represents the contents of one
  137. * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
  138. * a pair (one each for procs, tasks) for each pid namespace that's relevant
  139. * to the cgroup.
  140. */
  141. struct cgroup_pidlist {
  142. /*
  143. * used to find which pidlist is wanted. doesn't change as long as
  144. * this particular list stays in the list.
  145. */
  146. struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
  147. /* array of xids */
  148. pid_t *list;
  149. /* how many elements the above list has */
  150. int length;
  151. /* each of these stored in a list by its cgroup */
  152. struct list_head links;
  153. /* pointer to the cgroup we belong to, for list removal purposes */
  154. struct cgroup *owner;
  155. /* for delayed destruction */
  156. struct delayed_work destroy_dwork;
  157. };
  158. /*
  159. * The following two functions "fix" the issue where there are more pids
  160. * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
  161. * TODO: replace with a kernel-wide solution to this problem
  162. */
  163. #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
  164. static void *pidlist_allocate(int count)
  165. {
  166. if (PIDLIST_TOO_LARGE(count))
  167. return vmalloc(count * sizeof(pid_t));
  168. else
  169. return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
  170. }
  171. static void pidlist_free(void *p)
  172. {
  173. kvfree(p);
  174. }
  175. /*
  176. * Used to destroy all pidlists lingering waiting for destroy timer. None
  177. * should be left afterwards.
  178. */
  179. void cgroup1_pidlist_destroy_all(struct cgroup *cgrp)
  180. {
  181. struct cgroup_pidlist *l, *tmp_l;
  182. mutex_lock(&cgrp->pidlist_mutex);
  183. list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
  184. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
  185. mutex_unlock(&cgrp->pidlist_mutex);
  186. flush_workqueue(cgroup_pidlist_destroy_wq);
  187. BUG_ON(!list_empty(&cgrp->pidlists));
  188. }
  189. static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
  190. {
  191. struct delayed_work *dwork = to_delayed_work(work);
  192. struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
  193. destroy_dwork);
  194. struct cgroup_pidlist *tofree = NULL;
  195. mutex_lock(&l->owner->pidlist_mutex);
  196. /*
  197. * Destroy iff we didn't get queued again. The state won't change
  198. * as destroy_dwork can only be queued while locked.
  199. */
  200. if (!delayed_work_pending(dwork)) {
  201. list_del(&l->links);
  202. pidlist_free(l->list);
  203. put_pid_ns(l->key.ns);
  204. tofree = l;
  205. }
  206. mutex_unlock(&l->owner->pidlist_mutex);
  207. kfree(tofree);
  208. }
  209. /*
  210. * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  211. * Returns the number of unique elements.
  212. */
  213. static int pidlist_uniq(pid_t *list, int length)
  214. {
  215. int src, dest = 1;
  216. /*
  217. * we presume the 0th element is unique, so i starts at 1. trivial
  218. * edge cases first; no work needs to be done for either
  219. */
  220. if (length == 0 || length == 1)
  221. return length;
  222. /* src and dest walk down the list; dest counts unique elements */
  223. for (src = 1; src < length; src++) {
  224. /* find next unique element */
  225. while (list[src] == list[src-1]) {
  226. src++;
  227. if (src == length)
  228. goto after;
  229. }
  230. /* dest always points to where the next unique element goes */
  231. list[dest] = list[src];
  232. dest++;
  233. }
  234. after:
  235. return dest;
  236. }
  237. /*
  238. * The two pid files - task and cgroup.procs - guaranteed that the result
  239. * is sorted, which forced this whole pidlist fiasco. As pid order is
  240. * different per namespace, each namespace needs differently sorted list,
  241. * making it impossible to use, for example, single rbtree of member tasks
  242. * sorted by task pointer. As pidlists can be fairly large, allocating one
  243. * per open file is dangerous, so cgroup had to implement shared pool of
  244. * pidlists keyed by cgroup and namespace.
  245. */
  246. static int cmppid(const void *a, const void *b)
  247. {
  248. return *(pid_t *)a - *(pid_t *)b;
  249. }
  250. static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
  251. enum cgroup_filetype type)
  252. {
  253. struct cgroup_pidlist *l;
  254. /* don't need task_nsproxy() if we're looking at ourself */
  255. struct pid_namespace *ns = task_active_pid_ns(current);
  256. lockdep_assert_held(&cgrp->pidlist_mutex);
  257. list_for_each_entry(l, &cgrp->pidlists, links)
  258. if (l->key.type == type && l->key.ns == ns)
  259. return l;
  260. return NULL;
  261. }
  262. /*
  263. * find the appropriate pidlist for our purpose (given procs vs tasks)
  264. * returns with the lock on that pidlist already held, and takes care
  265. * of the use count, or returns NULL with no locks held if we're out of
  266. * memory.
  267. */
  268. static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
  269. enum cgroup_filetype type)
  270. {
  271. struct cgroup_pidlist *l;
  272. lockdep_assert_held(&cgrp->pidlist_mutex);
  273. l = cgroup_pidlist_find(cgrp, type);
  274. if (l)
  275. return l;
  276. /* entry not found; create a new one */
  277. l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
  278. if (!l)
  279. return l;
  280. INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
  281. l->key.type = type;
  282. /* don't need task_nsproxy() if we're looking at ourself */
  283. l->key.ns = get_pid_ns(task_active_pid_ns(current));
  284. l->owner = cgrp;
  285. list_add(&l->links, &cgrp->pidlists);
  286. return l;
  287. }
  288. /**
  289. * cgroup_task_count - count the number of tasks in a cgroup.
  290. * @cgrp: the cgroup in question
  291. */
  292. int cgroup_task_count(const struct cgroup *cgrp)
  293. {
  294. int count = 0;
  295. struct cgrp_cset_link *link;
  296. spin_lock_irq(&css_set_lock);
  297. list_for_each_entry(link, &cgrp->cset_links, cset_link)
  298. count += link->cset->nr_tasks;
  299. spin_unlock_irq(&css_set_lock);
  300. return count;
  301. }
  302. /*
  303. * Load a cgroup's pidarray with either procs' tgids or tasks' pids
  304. */
  305. static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
  306. struct cgroup_pidlist **lp)
  307. {
  308. pid_t *array;
  309. int length;
  310. int pid, n = 0; /* used for populating the array */
  311. struct css_task_iter it;
  312. struct task_struct *tsk;
  313. struct cgroup_pidlist *l;
  314. lockdep_assert_held(&cgrp->pidlist_mutex);
  315. /*
  316. * If cgroup gets more users after we read count, we won't have
  317. * enough space - tough. This race is indistinguishable to the
  318. * caller from the case that the additional cgroup users didn't
  319. * show up until sometime later on.
  320. */
  321. length = cgroup_task_count(cgrp);
  322. array = pidlist_allocate(length);
  323. if (!array)
  324. return -ENOMEM;
  325. /* now, populate the array */
  326. css_task_iter_start(&cgrp->self, &it);
  327. while ((tsk = css_task_iter_next(&it))) {
  328. if (unlikely(n == length))
  329. break;
  330. /* get tgid or pid for procs or tasks file respectively */
  331. if (type == CGROUP_FILE_PROCS)
  332. pid = task_tgid_vnr(tsk);
  333. else
  334. pid = task_pid_vnr(tsk);
  335. if (pid > 0) /* make sure to only use valid results */
  336. array[n++] = pid;
  337. }
  338. css_task_iter_end(&it);
  339. length = n;
  340. /* now sort & (if procs) strip out duplicates */
  341. sort(array, length, sizeof(pid_t), cmppid, NULL);
  342. if (type == CGROUP_FILE_PROCS)
  343. length = pidlist_uniq(array, length);
  344. l = cgroup_pidlist_find_create(cgrp, type);
  345. if (!l) {
  346. pidlist_free(array);
  347. return -ENOMEM;
  348. }
  349. /* store array, freeing old if necessary */
  350. pidlist_free(l->list);
  351. l->list = array;
  352. l->length = length;
  353. *lp = l;
  354. return 0;
  355. }
  356. /*
  357. * seq_file methods for the tasks/procs files. The seq_file position is the
  358. * next pid to display; the seq_file iterator is a pointer to the pid
  359. * in the cgroup->l->list array.
  360. */
  361. static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
  362. {
  363. /*
  364. * Initially we receive a position value that corresponds to
  365. * one more than the last pid shown (or 0 on the first call or
  366. * after a seek to the start). Use a binary-search to find the
  367. * next pid to display, if any
  368. */
  369. struct kernfs_open_file *of = s->private;
  370. struct cgroup *cgrp = seq_css(s)->cgroup;
  371. struct cgroup_pidlist *l;
  372. enum cgroup_filetype type = seq_cft(s)->private;
  373. int index = 0, pid = *pos;
  374. int *iter, ret;
  375. mutex_lock(&cgrp->pidlist_mutex);
  376. /*
  377. * !NULL @of->priv indicates that this isn't the first start()
  378. * after open. If the matching pidlist is around, we can use that.
  379. * Look for it. Note that @of->priv can't be used directly. It
  380. * could already have been destroyed.
  381. */
  382. if (of->priv)
  383. of->priv = cgroup_pidlist_find(cgrp, type);
  384. /*
  385. * Either this is the first start() after open or the matching
  386. * pidlist has been destroyed inbetween. Create a new one.
  387. */
  388. if (!of->priv) {
  389. ret = pidlist_array_load(cgrp, type,
  390. (struct cgroup_pidlist **)&of->priv);
  391. if (ret)
  392. return ERR_PTR(ret);
  393. }
  394. l = of->priv;
  395. if (pid) {
  396. int end = l->length;
  397. while (index < end) {
  398. int mid = (index + end) / 2;
  399. if (l->list[mid] == pid) {
  400. index = mid;
  401. break;
  402. } else if (l->list[mid] <= pid)
  403. index = mid + 1;
  404. else
  405. end = mid;
  406. }
  407. }
  408. /* If we're off the end of the array, we're done */
  409. if (index >= l->length)
  410. return NULL;
  411. /* Update the abstract position to be the actual pid that we found */
  412. iter = l->list + index;
  413. *pos = *iter;
  414. return iter;
  415. }
  416. static void cgroup_pidlist_stop(struct seq_file *s, void *v)
  417. {
  418. struct kernfs_open_file *of = s->private;
  419. struct cgroup_pidlist *l = of->priv;
  420. if (l)
  421. mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
  422. CGROUP_PIDLIST_DESTROY_DELAY);
  423. mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
  424. }
  425. static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
  426. {
  427. struct kernfs_open_file *of = s->private;
  428. struct cgroup_pidlist *l = of->priv;
  429. pid_t *p = v;
  430. pid_t *end = l->list + l->length;
  431. /*
  432. * Advance to the next pid in the array. If this goes off the
  433. * end, we're done
  434. */
  435. p++;
  436. if (p >= end) {
  437. return NULL;
  438. } else {
  439. *pos = *p;
  440. return p;
  441. }
  442. }
  443. static int cgroup_pidlist_show(struct seq_file *s, void *v)
  444. {
  445. seq_printf(s, "%d\n", *(int *)v);
  446. return 0;
  447. }
  448. static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
  449. char *buf, size_t nbytes, loff_t off)
  450. {
  451. return __cgroup_procs_write(of, buf, nbytes, off, false);
  452. }
  453. static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
  454. char *buf, size_t nbytes, loff_t off)
  455. {
  456. struct cgroup *cgrp;
  457. BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
  458. cgrp = cgroup_kn_lock_live(of->kn, false);
  459. if (!cgrp)
  460. return -ENODEV;
  461. spin_lock(&release_agent_path_lock);
  462. strlcpy(cgrp->root->release_agent_path, strstrip(buf),
  463. sizeof(cgrp->root->release_agent_path));
  464. spin_unlock(&release_agent_path_lock);
  465. cgroup_kn_unlock(of->kn);
  466. return nbytes;
  467. }
  468. static int cgroup_release_agent_show(struct seq_file *seq, void *v)
  469. {
  470. struct cgroup *cgrp = seq_css(seq)->cgroup;
  471. spin_lock(&release_agent_path_lock);
  472. seq_puts(seq, cgrp->root->release_agent_path);
  473. spin_unlock(&release_agent_path_lock);
  474. seq_putc(seq, '\n');
  475. return 0;
  476. }
  477. static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
  478. {
  479. seq_puts(seq, "0\n");
  480. return 0;
  481. }
  482. static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
  483. struct cftype *cft)
  484. {
  485. return notify_on_release(css->cgroup);
  486. }
  487. static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
  488. struct cftype *cft, u64 val)
  489. {
  490. if (val)
  491. set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  492. else
  493. clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
  494. return 0;
  495. }
  496. static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
  497. struct cftype *cft)
  498. {
  499. return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  500. }
  501. static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
  502. struct cftype *cft, u64 val)
  503. {
  504. if (val)
  505. set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  506. else
  507. clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
  508. return 0;
  509. }
  510. /* cgroup core interface files for the legacy hierarchies */
  511. struct cftype cgroup1_base_files[] = {
  512. {
  513. .name = "cgroup.procs",
  514. .seq_start = cgroup_pidlist_start,
  515. .seq_next = cgroup_pidlist_next,
  516. .seq_stop = cgroup_pidlist_stop,
  517. .seq_show = cgroup_pidlist_show,
  518. .private = CGROUP_FILE_PROCS,
  519. .write = cgroup_procs_write,
  520. },
  521. {
  522. .name = "cgroup.clone_children",
  523. .read_u64 = cgroup_clone_children_read,
  524. .write_u64 = cgroup_clone_children_write,
  525. },
  526. {
  527. .name = "cgroup.sane_behavior",
  528. .flags = CFTYPE_ONLY_ON_ROOT,
  529. .seq_show = cgroup_sane_behavior_show,
  530. },
  531. {
  532. .name = "tasks",
  533. .seq_start = cgroup_pidlist_start,
  534. .seq_next = cgroup_pidlist_next,
  535. .seq_stop = cgroup_pidlist_stop,
  536. .seq_show = cgroup_pidlist_show,
  537. .private = CGROUP_FILE_TASKS,
  538. .write = cgroup_tasks_write,
  539. },
  540. {
  541. .name = "notify_on_release",
  542. .read_u64 = cgroup_read_notify_on_release,
  543. .write_u64 = cgroup_write_notify_on_release,
  544. },
  545. {
  546. .name = "release_agent",
  547. .flags = CFTYPE_ONLY_ON_ROOT,
  548. .seq_show = cgroup_release_agent_show,
  549. .write = cgroup_release_agent_write,
  550. .max_write_len = PATH_MAX - 1,
  551. },
  552. { } /* terminate */
  553. };
  554. /* Display information about each subsystem and each hierarchy */
  555. static int proc_cgroupstats_show(struct seq_file *m, void *v)
  556. {
  557. struct cgroup_subsys *ss;
  558. int i;
  559. seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
  560. /*
  561. * ideally we don't want subsystems moving around while we do this.
  562. * cgroup_mutex is also necessary to guarantee an atomic snapshot of
  563. * subsys/hierarchy state.
  564. */
  565. mutex_lock(&cgroup_mutex);
  566. for_each_subsys(ss, i)
  567. seq_printf(m, "%s\t%d\t%d\t%d\n",
  568. ss->legacy_name, ss->root->hierarchy_id,
  569. atomic_read(&ss->root->nr_cgrps),
  570. cgroup_ssid_enabled(i));
  571. mutex_unlock(&cgroup_mutex);
  572. return 0;
  573. }
  574. static int cgroupstats_open(struct inode *inode, struct file *file)
  575. {
  576. return single_open(file, proc_cgroupstats_show, NULL);
  577. }
  578. const struct file_operations proc_cgroupstats_operations = {
  579. .open = cgroupstats_open,
  580. .read = seq_read,
  581. .llseek = seq_lseek,
  582. .release = single_release,
  583. };
  584. /**
  585. * cgroupstats_build - build and fill cgroupstats
  586. * @stats: cgroupstats to fill information into
  587. * @dentry: A dentry entry belonging to the cgroup for which stats have
  588. * been requested.
  589. *
  590. * Build and fill cgroupstats so that taskstats can export it to user
  591. * space.
  592. */
  593. int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
  594. {
  595. struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
  596. struct cgroup *cgrp;
  597. struct css_task_iter it;
  598. struct task_struct *tsk;
  599. /* it should be kernfs_node belonging to cgroupfs and is a directory */
  600. if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
  601. kernfs_type(kn) != KERNFS_DIR)
  602. return -EINVAL;
  603. mutex_lock(&cgroup_mutex);
  604. /*
  605. * We aren't being called from kernfs and there's no guarantee on
  606. * @kn->priv's validity. For this and css_tryget_online_from_dir(),
  607. * @kn->priv is RCU safe. Let's do the RCU dancing.
  608. */
  609. rcu_read_lock();
  610. cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
  611. if (!cgrp || cgroup_is_dead(cgrp)) {
  612. rcu_read_unlock();
  613. mutex_unlock(&cgroup_mutex);
  614. return -ENOENT;
  615. }
  616. rcu_read_unlock();
  617. css_task_iter_start(&cgrp->self, &it);
  618. while ((tsk = css_task_iter_next(&it))) {
  619. switch (tsk->state) {
  620. case TASK_RUNNING:
  621. stats->nr_running++;
  622. break;
  623. case TASK_INTERRUPTIBLE:
  624. stats->nr_sleeping++;
  625. break;
  626. case TASK_UNINTERRUPTIBLE:
  627. stats->nr_uninterruptible++;
  628. break;
  629. case TASK_STOPPED:
  630. stats->nr_stopped++;
  631. break;
  632. default:
  633. if (delayacct_is_task_waiting_on_io(tsk))
  634. stats->nr_io_wait++;
  635. break;
  636. }
  637. }
  638. css_task_iter_end(&it);
  639. mutex_unlock(&cgroup_mutex);
  640. return 0;
  641. }
  642. void cgroup1_check_for_release(struct cgroup *cgrp)
  643. {
  644. if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
  645. !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
  646. schedule_work(&cgrp->release_agent_work);
  647. }
  648. /*
  649. * Notify userspace when a cgroup is released, by running the
  650. * configured release agent with the name of the cgroup (path
  651. * relative to the root of cgroup file system) as the argument.
  652. *
  653. * Most likely, this user command will try to rmdir this cgroup.
  654. *
  655. * This races with the possibility that some other task will be
  656. * attached to this cgroup before it is removed, or that some other
  657. * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
  658. * The presumed 'rmdir' will fail quietly if this cgroup is no longer
  659. * unused, and this cgroup will be reprieved from its death sentence,
  660. * to continue to serve a useful existence. Next time it's released,
  661. * we will get notified again, if it still has 'notify_on_release' set.
  662. *
  663. * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
  664. * means only wait until the task is successfully execve()'d. The
  665. * separate release agent task is forked by call_usermodehelper(),
  666. * then control in this thread returns here, without waiting for the
  667. * release agent task. We don't bother to wait because the caller of
  668. * this routine has no use for the exit status of the release agent
  669. * task, so no sense holding our caller up for that.
  670. */
  671. void cgroup1_release_agent(struct work_struct *work)
  672. {
  673. struct cgroup *cgrp =
  674. container_of(work, struct cgroup, release_agent_work);
  675. char *pathbuf = NULL, *agentbuf = NULL;
  676. char *argv[3], *envp[3];
  677. int ret;
  678. mutex_lock(&cgroup_mutex);
  679. pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  680. agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
  681. if (!pathbuf || !agentbuf)
  682. goto out;
  683. spin_lock_irq(&css_set_lock);
  684. ret = cgroup_path_ns_locked(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
  685. spin_unlock_irq(&css_set_lock);
  686. if (ret < 0 || ret >= PATH_MAX)
  687. goto out;
  688. argv[0] = agentbuf;
  689. argv[1] = pathbuf;
  690. argv[2] = NULL;
  691. /* minimal command environment */
  692. envp[0] = "HOME=/";
  693. envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
  694. envp[2] = NULL;
  695. mutex_unlock(&cgroup_mutex);
  696. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  697. goto out_free;
  698. out:
  699. mutex_unlock(&cgroup_mutex);
  700. out_free:
  701. kfree(agentbuf);
  702. kfree(pathbuf);
  703. }
  704. /*
  705. * cgroup_rename - Only allow simple rename of directories in place.
  706. */
  707. static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
  708. const char *new_name_str)
  709. {
  710. struct cgroup *cgrp = kn->priv;
  711. int ret;
  712. if (kernfs_type(kn) != KERNFS_DIR)
  713. return -ENOTDIR;
  714. if (kn->parent != new_parent)
  715. return -EIO;
  716. /*
  717. * We're gonna grab cgroup_mutex which nests outside kernfs
  718. * active_ref. kernfs_rename() doesn't require active_ref
  719. * protection. Break them before grabbing cgroup_mutex.
  720. */
  721. kernfs_break_active_protection(new_parent);
  722. kernfs_break_active_protection(kn);
  723. mutex_lock(&cgroup_mutex);
  724. ret = kernfs_rename(kn, new_parent, new_name_str);
  725. if (!ret)
  726. trace_cgroup_rename(cgrp);
  727. mutex_unlock(&cgroup_mutex);
  728. kernfs_unbreak_active_protection(kn);
  729. kernfs_unbreak_active_protection(new_parent);
  730. return ret;
  731. }
  732. static int cgroup1_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
  733. {
  734. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  735. struct cgroup_subsys *ss;
  736. int ssid;
  737. for_each_subsys(ss, ssid)
  738. if (root->subsys_mask & (1 << ssid))
  739. seq_show_option(seq, ss->legacy_name, NULL);
  740. if (root->flags & CGRP_ROOT_NOPREFIX)
  741. seq_puts(seq, ",noprefix");
  742. if (root->flags & CGRP_ROOT_XATTR)
  743. seq_puts(seq, ",xattr");
  744. spin_lock(&release_agent_path_lock);
  745. if (strlen(root->release_agent_path))
  746. seq_show_option(seq, "release_agent",
  747. root->release_agent_path);
  748. spin_unlock(&release_agent_path_lock);
  749. if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
  750. seq_puts(seq, ",clone_children");
  751. if (strlen(root->name))
  752. seq_show_option(seq, "name", root->name);
  753. return 0;
  754. }
  755. static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
  756. {
  757. char *token, *o = data;
  758. bool all_ss = false, one_ss = false;
  759. u16 mask = U16_MAX;
  760. struct cgroup_subsys *ss;
  761. int nr_opts = 0;
  762. int i;
  763. #ifdef CONFIG_CPUSETS
  764. mask = ~((u16)1 << cpuset_cgrp_id);
  765. #endif
  766. memset(opts, 0, sizeof(*opts));
  767. while ((token = strsep(&o, ",")) != NULL) {
  768. nr_opts++;
  769. if (!*token)
  770. return -EINVAL;
  771. if (!strcmp(token, "none")) {
  772. /* Explicitly have no subsystems */
  773. opts->none = true;
  774. continue;
  775. }
  776. if (!strcmp(token, "all")) {
  777. /* Mutually exclusive option 'all' + subsystem name */
  778. if (one_ss)
  779. return -EINVAL;
  780. all_ss = true;
  781. continue;
  782. }
  783. if (!strcmp(token, "noprefix")) {
  784. opts->flags |= CGRP_ROOT_NOPREFIX;
  785. continue;
  786. }
  787. if (!strcmp(token, "clone_children")) {
  788. opts->cpuset_clone_children = true;
  789. continue;
  790. }
  791. if (!strcmp(token, "xattr")) {
  792. opts->flags |= CGRP_ROOT_XATTR;
  793. continue;
  794. }
  795. if (!strncmp(token, "release_agent=", 14)) {
  796. /* Specifying two release agents is forbidden */
  797. if (opts->release_agent)
  798. return -EINVAL;
  799. opts->release_agent =
  800. kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
  801. if (!opts->release_agent)
  802. return -ENOMEM;
  803. continue;
  804. }
  805. if (!strncmp(token, "name=", 5)) {
  806. const char *name = token + 5;
  807. /* Can't specify an empty name */
  808. if (!strlen(name))
  809. return -EINVAL;
  810. /* Must match [\w.-]+ */
  811. for (i = 0; i < strlen(name); i++) {
  812. char c = name[i];
  813. if (isalnum(c))
  814. continue;
  815. if ((c == '.') || (c == '-') || (c == '_'))
  816. continue;
  817. return -EINVAL;
  818. }
  819. /* Specifying two names is forbidden */
  820. if (opts->name)
  821. return -EINVAL;
  822. opts->name = kstrndup(name,
  823. MAX_CGROUP_ROOT_NAMELEN - 1,
  824. GFP_KERNEL);
  825. if (!opts->name)
  826. return -ENOMEM;
  827. continue;
  828. }
  829. for_each_subsys(ss, i) {
  830. if (strcmp(token, ss->legacy_name))
  831. continue;
  832. if (!cgroup_ssid_enabled(i))
  833. continue;
  834. if (cgroup1_ssid_disabled(i))
  835. continue;
  836. /* Mutually exclusive option 'all' + subsystem name */
  837. if (all_ss)
  838. return -EINVAL;
  839. opts->subsys_mask |= (1 << i);
  840. one_ss = true;
  841. break;
  842. }
  843. if (i == CGROUP_SUBSYS_COUNT)
  844. return -ENOENT;
  845. }
  846. /*
  847. * If the 'all' option was specified select all the subsystems,
  848. * otherwise if 'none', 'name=' and a subsystem name options were
  849. * not specified, let's default to 'all'
  850. */
  851. if (all_ss || (!one_ss && !opts->none && !opts->name))
  852. for_each_subsys(ss, i)
  853. if (cgroup_ssid_enabled(i) && !cgroup1_ssid_disabled(i))
  854. opts->subsys_mask |= (1 << i);
  855. /*
  856. * We either have to specify by name or by subsystems. (So all
  857. * empty hierarchies must have a name).
  858. */
  859. if (!opts->subsys_mask && !opts->name)
  860. return -EINVAL;
  861. /*
  862. * Option noprefix was introduced just for backward compatibility
  863. * with the old cpuset, so we allow noprefix only if mounting just
  864. * the cpuset subsystem.
  865. */
  866. if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
  867. return -EINVAL;
  868. /* Can't specify "none" and some subsystems */
  869. if (opts->subsys_mask && opts->none)
  870. return -EINVAL;
  871. return 0;
  872. }
  873. static int cgroup1_remount(struct kernfs_root *kf_root, int *flags, char *data)
  874. {
  875. int ret = 0;
  876. struct cgroup_root *root = cgroup_root_from_kf(kf_root);
  877. struct cgroup_sb_opts opts;
  878. u16 added_mask, removed_mask;
  879. cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
  880. /* See what subsystems are wanted */
  881. ret = parse_cgroupfs_options(data, &opts);
  882. if (ret)
  883. goto out_unlock;
  884. if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
  885. pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
  886. task_tgid_nr(current), current->comm);
  887. added_mask = opts.subsys_mask & ~root->subsys_mask;
  888. removed_mask = root->subsys_mask & ~opts.subsys_mask;
  889. /* Don't allow flags or name to change at remount */
  890. if ((opts.flags ^ root->flags) ||
  891. (opts.name && strcmp(opts.name, root->name))) {
  892. pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
  893. opts.flags, opts.name ?: "", root->flags, root->name);
  894. ret = -EINVAL;
  895. goto out_unlock;
  896. }
  897. /* remounting is not allowed for populated hierarchies */
  898. if (!list_empty(&root->cgrp.self.children)) {
  899. ret = -EBUSY;
  900. goto out_unlock;
  901. }
  902. ret = rebind_subsystems(root, added_mask);
  903. if (ret)
  904. goto out_unlock;
  905. WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
  906. if (opts.release_agent) {
  907. spin_lock(&release_agent_path_lock);
  908. strcpy(root->release_agent_path, opts.release_agent);
  909. spin_unlock(&release_agent_path_lock);
  910. }
  911. trace_cgroup_remount(root);
  912. out_unlock:
  913. kfree(opts.release_agent);
  914. kfree(opts.name);
  915. mutex_unlock(&cgroup_mutex);
  916. return ret;
  917. }
  918. struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
  919. .rename = cgroup1_rename,
  920. .show_options = cgroup1_show_options,
  921. .remount_fs = cgroup1_remount,
  922. .mkdir = cgroup_mkdir,
  923. .rmdir = cgroup_rmdir,
  924. .show_path = cgroup_show_path,
  925. };
  926. struct dentry *cgroup1_mount(struct file_system_type *fs_type, int flags,
  927. void *data, unsigned long magic,
  928. struct cgroup_namespace *ns)
  929. {
  930. struct super_block *pinned_sb = NULL;
  931. struct cgroup_sb_opts opts;
  932. struct cgroup_root *root;
  933. struct cgroup_subsys *ss;
  934. struct dentry *dentry;
  935. int i, ret;
  936. bool new_root = false;
  937. cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
  938. /* First find the desired set of subsystems */
  939. ret = parse_cgroupfs_options(data, &opts);
  940. if (ret)
  941. goto out_unlock;
  942. /*
  943. * Destruction of cgroup root is asynchronous, so subsystems may
  944. * still be dying after the previous unmount. Let's drain the
  945. * dying subsystems. We just need to ensure that the ones
  946. * unmounted previously finish dying and don't care about new ones
  947. * starting. Testing ref liveliness is good enough.
  948. */
  949. for_each_subsys(ss, i) {
  950. if (!(opts.subsys_mask & (1 << i)) ||
  951. ss->root == &cgrp_dfl_root)
  952. continue;
  953. if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
  954. mutex_unlock(&cgroup_mutex);
  955. msleep(10);
  956. ret = restart_syscall();
  957. goto out_free;
  958. }
  959. cgroup_put(&ss->root->cgrp);
  960. }
  961. for_each_root(root) {
  962. bool name_match = false;
  963. if (root == &cgrp_dfl_root)
  964. continue;
  965. /*
  966. * If we asked for a name then it must match. Also, if
  967. * name matches but sybsys_mask doesn't, we should fail.
  968. * Remember whether name matched.
  969. */
  970. if (opts.name) {
  971. if (strcmp(opts.name, root->name))
  972. continue;
  973. name_match = true;
  974. }
  975. /*
  976. * If we asked for subsystems (or explicitly for no
  977. * subsystems) then they must match.
  978. */
  979. if ((opts.subsys_mask || opts.none) &&
  980. (opts.subsys_mask != root->subsys_mask)) {
  981. if (!name_match)
  982. continue;
  983. ret = -EBUSY;
  984. goto out_unlock;
  985. }
  986. if (root->flags ^ opts.flags)
  987. pr_warn("new mount options do not match the existing superblock, will be ignored\n");
  988. /*
  989. * We want to reuse @root whose lifetime is governed by its
  990. * ->cgrp. Let's check whether @root is alive and keep it
  991. * that way. As cgroup_kill_sb() can happen anytime, we
  992. * want to block it by pinning the sb so that @root doesn't
  993. * get killed before mount is complete.
  994. *
  995. * With the sb pinned, tryget_live can reliably indicate
  996. * whether @root can be reused. If it's being killed,
  997. * drain it. We can use wait_queue for the wait but this
  998. * path is super cold. Let's just sleep a bit and retry.
  999. */
  1000. pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
  1001. if (IS_ERR(pinned_sb) ||
  1002. !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
  1003. mutex_unlock(&cgroup_mutex);
  1004. if (!IS_ERR_OR_NULL(pinned_sb))
  1005. deactivate_super(pinned_sb);
  1006. msleep(10);
  1007. ret = restart_syscall();
  1008. goto out_free;
  1009. }
  1010. ret = 0;
  1011. goto out_unlock;
  1012. }
  1013. /*
  1014. * No such thing, create a new one. name= matching without subsys
  1015. * specification is allowed for already existing hierarchies but we
  1016. * can't create new one without subsys specification.
  1017. */
  1018. if (!opts.subsys_mask && !opts.none) {
  1019. ret = -EINVAL;
  1020. goto out_unlock;
  1021. }
  1022. /* Hierarchies may only be created in the initial cgroup namespace. */
  1023. if (ns != &init_cgroup_ns) {
  1024. ret = -EPERM;
  1025. goto out_unlock;
  1026. }
  1027. root = kzalloc(sizeof(*root), GFP_KERNEL);
  1028. if (!root) {
  1029. ret = -ENOMEM;
  1030. goto out_unlock;
  1031. }
  1032. new_root = true;
  1033. init_cgroup_root(root, &opts);
  1034. ret = cgroup_setup_root(root, opts.subsys_mask, PERCPU_REF_INIT_DEAD);
  1035. if (ret)
  1036. cgroup_free_root(root);
  1037. out_unlock:
  1038. mutex_unlock(&cgroup_mutex);
  1039. out_free:
  1040. kfree(opts.release_agent);
  1041. kfree(opts.name);
  1042. if (ret)
  1043. return ERR_PTR(ret);
  1044. dentry = cgroup_do_mount(&cgroup_fs_type, flags, root,
  1045. CGROUP_SUPER_MAGIC, ns);
  1046. /*
  1047. * There's a race window after we release cgroup_mutex and before
  1048. * allocating a superblock. Make sure a concurrent process won't
  1049. * be able to re-use the root during this window by delaying the
  1050. * initialization of root refcnt.
  1051. */
  1052. if (new_root) {
  1053. mutex_lock(&cgroup_mutex);
  1054. percpu_ref_reinit(&root->cgrp.self.refcnt);
  1055. mutex_unlock(&cgroup_mutex);
  1056. }
  1057. /*
  1058. * If @pinned_sb, we're reusing an existing root and holding an
  1059. * extra ref on its sb. Mount is complete. Put the extra ref.
  1060. */
  1061. if (pinned_sb)
  1062. deactivate_super(pinned_sb);
  1063. return dentry;
  1064. }
  1065. static int __init cgroup1_wq_init(void)
  1066. {
  1067. /*
  1068. * Used to destroy pidlists and separate to serve as flush domain.
  1069. * Cap @max_active to 1 too.
  1070. */
  1071. cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
  1072. 0, 1);
  1073. BUG_ON(!cgroup_pidlist_destroy_wq);
  1074. return 0;
  1075. }
  1076. core_initcall(cgroup1_wq_init);
  1077. static int __init cgroup_no_v1(char *str)
  1078. {
  1079. struct cgroup_subsys *ss;
  1080. char *token;
  1081. int i;
  1082. while ((token = strsep(&str, ",")) != NULL) {
  1083. if (!*token)
  1084. continue;
  1085. if (!strcmp(token, "all")) {
  1086. cgroup_no_v1_mask = U16_MAX;
  1087. break;
  1088. }
  1089. for_each_subsys(ss, i) {
  1090. if (strcmp(token, ss->name) &&
  1091. strcmp(token, ss->legacy_name))
  1092. continue;
  1093. cgroup_no_v1_mask |= 1 << i;
  1094. }
  1095. }
  1096. return 1;
  1097. }
  1098. __setup("cgroup_no_v1=", cgroup_no_v1);