cgroup-v1.c 34 KB

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