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