cgroup-v1.c 35 KB

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