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