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