xref: /openbmc/linux/kernel/cgroup/cgroup-v1.c (revision 9123e3a7)
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 
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  */
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  */
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  */
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 
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  */
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  */
271 static int cmppid(const void *a, const void *b)
272 {
273 	return *(pid_t *)a - *(pid_t *)b;
274 }
275 
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  */
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  */
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 
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 *cgrp = seq_css(s)->cgroup;
397 	struct cgroup_pidlist *l;
398 	enum cgroup_filetype type = seq_cft(s)->private;
399 	int index = 0, pid = *pos;
400 	int *iter, ret;
401 
402 	mutex_lock(&cgrp->pidlist_mutex);
403 
404 	/*
405 	 * !NULL @of->priv indicates that this isn't the first start()
406 	 * after open.  If the matching pidlist is around, we can use that.
407 	 * Look for it.  Note that @of->priv can't be used directly.  It
408 	 * could already have been destroyed.
409 	 */
410 	if (of->priv)
411 		of->priv = cgroup_pidlist_find(cgrp, type);
412 
413 	/*
414 	 * Either this is the first start() after open or the matching
415 	 * pidlist has been destroyed inbetween.  Create a new one.
416 	 */
417 	if (!of->priv) {
418 		ret = pidlist_array_load(cgrp, type,
419 					 (struct cgroup_pidlist **)&of->priv);
420 		if (ret)
421 			return ERR_PTR(ret);
422 	}
423 	l = of->priv;
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 
448 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
449 {
450 	struct kernfs_open_file *of = s->private;
451 	struct cgroup_pidlist *l = of->priv;
452 
453 	if (l)
454 		mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
455 				 CGROUP_PIDLIST_DESTROY_DELAY);
456 	mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
457 }
458 
459 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
460 {
461 	struct kernfs_open_file *of = s->private;
462 	struct cgroup_pidlist *l = of->priv;
463 	pid_t *p = v;
464 	pid_t *end = l->list + l->length;
465 	/*
466 	 * Advance to the next pid in the array. If this goes off the
467 	 * end, we're done
468 	 */
469 	p++;
470 	if (p >= end) {
471 		(*pos)++;
472 		return NULL;
473 	} else {
474 		*pos = *p;
475 		return p;
476 	}
477 }
478 
479 static int cgroup_pidlist_show(struct seq_file *s, void *v)
480 {
481 	seq_printf(s, "%d\n", *(int *)v);
482 
483 	return 0;
484 }
485 
486 static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,
487 				     char *buf, size_t nbytes, loff_t off,
488 				     bool threadgroup)
489 {
490 	struct cgroup *cgrp;
491 	struct task_struct *task;
492 	const struct cred *cred, *tcred;
493 	ssize_t ret;
494 	bool locked;
495 
496 	cgrp = cgroup_kn_lock_live(of->kn, false);
497 	if (!cgrp)
498 		return -ENODEV;
499 
500 	task = cgroup_procs_write_start(buf, threadgroup, &locked);
501 	ret = PTR_ERR_OR_ZERO(task);
502 	if (ret)
503 		goto out_unlock;
504 
505 	/*
506 	 * Even if we're attaching all tasks in the thread group, we only
507 	 * need to check permissions on one of them.
508 	 */
509 	cred = current_cred();
510 	tcred = get_task_cred(task);
511 	if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
512 	    !uid_eq(cred->euid, tcred->uid) &&
513 	    !uid_eq(cred->euid, tcred->suid))
514 		ret = -EACCES;
515 	put_cred(tcred);
516 	if (ret)
517 		goto out_finish;
518 
519 	ret = cgroup_attach_task(cgrp, task, threadgroup);
520 
521 out_finish:
522 	cgroup_procs_write_finish(task, locked);
523 out_unlock:
524 	cgroup_kn_unlock(of->kn);
525 
526 	return ret ?: nbytes;
527 }
528 
529 static ssize_t cgroup1_procs_write(struct kernfs_open_file *of,
530 				   char *buf, size_t nbytes, loff_t off)
531 {
532 	return __cgroup1_procs_write(of, buf, nbytes, off, true);
533 }
534 
535 static ssize_t cgroup1_tasks_write(struct kernfs_open_file *of,
536 				   char *buf, size_t nbytes, loff_t off)
537 {
538 	return __cgroup1_procs_write(of, buf, nbytes, off, false);
539 }
540 
541 static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
542 					  char *buf, size_t nbytes, loff_t off)
543 {
544 	struct cgroup *cgrp;
545 
546 	BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
547 
548 	cgrp = cgroup_kn_lock_live(of->kn, false);
549 	if (!cgrp)
550 		return -ENODEV;
551 	spin_lock(&release_agent_path_lock);
552 	strlcpy(cgrp->root->release_agent_path, strstrip(buf),
553 		sizeof(cgrp->root->release_agent_path));
554 	spin_unlock(&release_agent_path_lock);
555 	cgroup_kn_unlock(of->kn);
556 	return nbytes;
557 }
558 
559 static int cgroup_release_agent_show(struct seq_file *seq, void *v)
560 {
561 	struct cgroup *cgrp = seq_css(seq)->cgroup;
562 
563 	spin_lock(&release_agent_path_lock);
564 	seq_puts(seq, cgrp->root->release_agent_path);
565 	spin_unlock(&release_agent_path_lock);
566 	seq_putc(seq, '\n');
567 	return 0;
568 }
569 
570 static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
571 {
572 	seq_puts(seq, "0\n");
573 	return 0;
574 }
575 
576 static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
577 					 struct cftype *cft)
578 {
579 	return notify_on_release(css->cgroup);
580 }
581 
582 static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
583 					  struct cftype *cft, u64 val)
584 {
585 	if (val)
586 		set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
587 	else
588 		clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
589 	return 0;
590 }
591 
592 static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
593 				      struct cftype *cft)
594 {
595 	return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
596 }
597 
598 static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
599 				       struct cftype *cft, u64 val)
600 {
601 	if (val)
602 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
603 	else
604 		clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
605 	return 0;
606 }
607 
608 /* cgroup core interface files for the legacy hierarchies */
609 struct cftype cgroup1_base_files[] = {
610 	{
611 		.name = "cgroup.procs",
612 		.seq_start = cgroup_pidlist_start,
613 		.seq_next = cgroup_pidlist_next,
614 		.seq_stop = cgroup_pidlist_stop,
615 		.seq_show = cgroup_pidlist_show,
616 		.private = CGROUP_FILE_PROCS,
617 		.write = cgroup1_procs_write,
618 	},
619 	{
620 		.name = "cgroup.clone_children",
621 		.read_u64 = cgroup_clone_children_read,
622 		.write_u64 = cgroup_clone_children_write,
623 	},
624 	{
625 		.name = "cgroup.sane_behavior",
626 		.flags = CFTYPE_ONLY_ON_ROOT,
627 		.seq_show = cgroup_sane_behavior_show,
628 	},
629 	{
630 		.name = "tasks",
631 		.seq_start = cgroup_pidlist_start,
632 		.seq_next = cgroup_pidlist_next,
633 		.seq_stop = cgroup_pidlist_stop,
634 		.seq_show = cgroup_pidlist_show,
635 		.private = CGROUP_FILE_TASKS,
636 		.write = cgroup1_tasks_write,
637 	},
638 	{
639 		.name = "notify_on_release",
640 		.read_u64 = cgroup_read_notify_on_release,
641 		.write_u64 = cgroup_write_notify_on_release,
642 	},
643 	{
644 		.name = "release_agent",
645 		.flags = CFTYPE_ONLY_ON_ROOT,
646 		.seq_show = cgroup_release_agent_show,
647 		.write = cgroup_release_agent_write,
648 		.max_write_len = PATH_MAX - 1,
649 	},
650 	{ }	/* terminate */
651 };
652 
653 /* Display information about each subsystem and each hierarchy */
654 int proc_cgroupstats_show(struct seq_file *m, void *v)
655 {
656 	struct cgroup_subsys *ss;
657 	int i;
658 
659 	seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
660 	/*
661 	 * ideally we don't want subsystems moving around while we do this.
662 	 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
663 	 * subsys/hierarchy state.
664 	 */
665 	mutex_lock(&cgroup_mutex);
666 
667 	for_each_subsys(ss, i)
668 		seq_printf(m, "%s\t%d\t%d\t%d\n",
669 			   ss->legacy_name, ss->root->hierarchy_id,
670 			   atomic_read(&ss->root->nr_cgrps),
671 			   cgroup_ssid_enabled(i));
672 
673 	mutex_unlock(&cgroup_mutex);
674 	return 0;
675 }
676 
677 /**
678  * cgroupstats_build - build and fill cgroupstats
679  * @stats: cgroupstats to fill information into
680  * @dentry: A dentry entry belonging to the cgroup for which stats have
681  * been requested.
682  *
683  * Build and fill cgroupstats so that taskstats can export it to user
684  * space.
685  */
686 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
687 {
688 	struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
689 	struct cgroup *cgrp;
690 	struct css_task_iter it;
691 	struct task_struct *tsk;
692 
693 	/* it should be kernfs_node belonging to cgroupfs and is a directory */
694 	if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
695 	    kernfs_type(kn) != KERNFS_DIR)
696 		return -EINVAL;
697 
698 	mutex_lock(&cgroup_mutex);
699 
700 	/*
701 	 * We aren't being called from kernfs and there's no guarantee on
702 	 * @kn->priv's validity.  For this and css_tryget_online_from_dir(),
703 	 * @kn->priv is RCU safe.  Let's do the RCU dancing.
704 	 */
705 	rcu_read_lock();
706 	cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
707 	if (!cgrp || cgroup_is_dead(cgrp)) {
708 		rcu_read_unlock();
709 		mutex_unlock(&cgroup_mutex);
710 		return -ENOENT;
711 	}
712 	rcu_read_unlock();
713 
714 	css_task_iter_start(&cgrp->self, 0, &it);
715 	while ((tsk = css_task_iter_next(&it))) {
716 		switch (tsk->state) {
717 		case TASK_RUNNING:
718 			stats->nr_running++;
719 			break;
720 		case TASK_INTERRUPTIBLE:
721 			stats->nr_sleeping++;
722 			break;
723 		case TASK_UNINTERRUPTIBLE:
724 			stats->nr_uninterruptible++;
725 			break;
726 		case TASK_STOPPED:
727 			stats->nr_stopped++;
728 			break;
729 		default:
730 			if (delayacct_is_task_waiting_on_io(tsk))
731 				stats->nr_io_wait++;
732 			break;
733 		}
734 	}
735 	css_task_iter_end(&it);
736 
737 	mutex_unlock(&cgroup_mutex);
738 	return 0;
739 }
740 
741 void cgroup1_check_for_release(struct cgroup *cgrp)
742 {
743 	if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
744 	    !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
745 		schedule_work(&cgrp->release_agent_work);
746 }
747 
748 /*
749  * Notify userspace when a cgroup is released, by running the
750  * configured release agent with the name of the cgroup (path
751  * relative to the root of cgroup file system) as the argument.
752  *
753  * Most likely, this user command will try to rmdir this cgroup.
754  *
755  * This races with the possibility that some other task will be
756  * attached to this cgroup before it is removed, or that some other
757  * user task will 'mkdir' a child cgroup of this cgroup.  That's ok.
758  * The presumed 'rmdir' will fail quietly if this cgroup is no longer
759  * unused, and this cgroup will be reprieved from its death sentence,
760  * to continue to serve a useful existence.  Next time it's released,
761  * we will get notified again, if it still has 'notify_on_release' set.
762  *
763  * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
764  * means only wait until the task is successfully execve()'d.  The
765  * separate release agent task is forked by call_usermodehelper(),
766  * then control in this thread returns here, without waiting for the
767  * release agent task.  We don't bother to wait because the caller of
768  * this routine has no use for the exit status of the release agent
769  * task, so no sense holding our caller up for that.
770  */
771 void cgroup1_release_agent(struct work_struct *work)
772 {
773 	struct cgroup *cgrp =
774 		container_of(work, struct cgroup, release_agent_work);
775 	char *pathbuf, *agentbuf;
776 	char *argv[3], *envp[3];
777 	int ret;
778 
779 	/* snoop agent path and exit early if empty */
780 	if (!cgrp->root->release_agent_path[0])
781 		return;
782 
783 	/* prepare argument buffers */
784 	pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
785 	agentbuf = kmalloc(PATH_MAX, GFP_KERNEL);
786 	if (!pathbuf || !agentbuf)
787 		goto out_free;
788 
789 	spin_lock(&release_agent_path_lock);
790 	strlcpy(agentbuf, cgrp->root->release_agent_path, PATH_MAX);
791 	spin_unlock(&release_agent_path_lock);
792 	if (!agentbuf[0])
793 		goto out_free;
794 
795 	ret = cgroup_path_ns(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
796 	if (ret < 0 || ret >= PATH_MAX)
797 		goto out_free;
798 
799 	argv[0] = agentbuf;
800 	argv[1] = pathbuf;
801 	argv[2] = NULL;
802 
803 	/* minimal command environment */
804 	envp[0] = "HOME=/";
805 	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
806 	envp[2] = NULL;
807 
808 	call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
809 out_free:
810 	kfree(agentbuf);
811 	kfree(pathbuf);
812 }
813 
814 /*
815  * cgroup_rename - Only allow simple rename of directories in place.
816  */
817 static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
818 			  const char *new_name_str)
819 {
820 	struct cgroup *cgrp = kn->priv;
821 	int ret;
822 
823 	if (kernfs_type(kn) != KERNFS_DIR)
824 		return -ENOTDIR;
825 	if (kn->parent != new_parent)
826 		return -EIO;
827 
828 	/*
829 	 * We're gonna grab cgroup_mutex which nests outside kernfs
830 	 * active_ref.  kernfs_rename() doesn't require active_ref
831 	 * protection.  Break them before grabbing cgroup_mutex.
832 	 */
833 	kernfs_break_active_protection(new_parent);
834 	kernfs_break_active_protection(kn);
835 
836 	mutex_lock(&cgroup_mutex);
837 
838 	ret = kernfs_rename(kn, new_parent, new_name_str);
839 	if (!ret)
840 		TRACE_CGROUP_PATH(rename, cgrp);
841 
842 	mutex_unlock(&cgroup_mutex);
843 
844 	kernfs_unbreak_active_protection(kn);
845 	kernfs_unbreak_active_protection(new_parent);
846 	return ret;
847 }
848 
849 static int cgroup1_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
850 {
851 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
852 	struct cgroup_subsys *ss;
853 	int ssid;
854 
855 	for_each_subsys(ss, ssid)
856 		if (root->subsys_mask & (1 << ssid))
857 			seq_show_option(seq, ss->legacy_name, NULL);
858 	if (root->flags & CGRP_ROOT_NOPREFIX)
859 		seq_puts(seq, ",noprefix");
860 	if (root->flags & CGRP_ROOT_XATTR)
861 		seq_puts(seq, ",xattr");
862 	if (root->flags & CGRP_ROOT_CPUSET_V2_MODE)
863 		seq_puts(seq, ",cpuset_v2_mode");
864 
865 	spin_lock(&release_agent_path_lock);
866 	if (strlen(root->release_agent_path))
867 		seq_show_option(seq, "release_agent",
868 				root->release_agent_path);
869 	spin_unlock(&release_agent_path_lock);
870 
871 	if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
872 		seq_puts(seq, ",clone_children");
873 	if (strlen(root->name))
874 		seq_show_option(seq, "name", root->name);
875 	return 0;
876 }
877 
878 enum cgroup1_param {
879 	Opt_all,
880 	Opt_clone_children,
881 	Opt_cpuset_v2_mode,
882 	Opt_name,
883 	Opt_none,
884 	Opt_noprefix,
885 	Opt_release_agent,
886 	Opt_xattr,
887 };
888 
889 const struct fs_parameter_spec cgroup1_fs_parameters[] = {
890 	fsparam_flag  ("all",		Opt_all),
891 	fsparam_flag  ("clone_children", Opt_clone_children),
892 	fsparam_flag  ("cpuset_v2_mode", Opt_cpuset_v2_mode),
893 	fsparam_string("name",		Opt_name),
894 	fsparam_flag  ("none",		Opt_none),
895 	fsparam_flag  ("noprefix",	Opt_noprefix),
896 	fsparam_string("release_agent",	Opt_release_agent),
897 	fsparam_flag  ("xattr",		Opt_xattr),
898 	{}
899 };
900 
901 int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param)
902 {
903 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
904 	struct cgroup_subsys *ss;
905 	struct fs_parse_result result;
906 	int opt, i;
907 
908 	opt = fs_parse(fc, cgroup1_fs_parameters, param, &result);
909 	if (opt == -ENOPARAM) {
910 		if (strcmp(param->key, "source") == 0) {
911 			fc->source = param->string;
912 			param->string = NULL;
913 			return 0;
914 		}
915 		for_each_subsys(ss, i) {
916 			if (strcmp(param->key, ss->legacy_name))
917 				continue;
918 			ctx->subsys_mask |= (1 << i);
919 			return 0;
920 		}
921 		return invalfc(fc, "Unknown subsys name '%s'", param->key);
922 	}
923 	if (opt < 0)
924 		return opt;
925 
926 	switch (opt) {
927 	case Opt_none:
928 		/* Explicitly have no subsystems */
929 		ctx->none = true;
930 		break;
931 	case Opt_all:
932 		ctx->all_ss = true;
933 		break;
934 	case Opt_noprefix:
935 		ctx->flags |= CGRP_ROOT_NOPREFIX;
936 		break;
937 	case Opt_clone_children:
938 		ctx->cpuset_clone_children = true;
939 		break;
940 	case Opt_cpuset_v2_mode:
941 		ctx->flags |= CGRP_ROOT_CPUSET_V2_MODE;
942 		break;
943 	case Opt_xattr:
944 		ctx->flags |= CGRP_ROOT_XATTR;
945 		break;
946 	case Opt_release_agent:
947 		/* Specifying two release agents is forbidden */
948 		if (ctx->release_agent)
949 			return invalfc(fc, "release_agent respecified");
950 		ctx->release_agent = param->string;
951 		param->string = NULL;
952 		break;
953 	case Opt_name:
954 		/* blocked by boot param? */
955 		if (cgroup_no_v1_named)
956 			return -ENOENT;
957 		/* Can't specify an empty name */
958 		if (!param->size)
959 			return invalfc(fc, "Empty name");
960 		if (param->size > MAX_CGROUP_ROOT_NAMELEN - 1)
961 			return invalfc(fc, "Name too long");
962 		/* Must match [\w.-]+ */
963 		for (i = 0; i < param->size; i++) {
964 			char c = param->string[i];
965 			if (isalnum(c))
966 				continue;
967 			if ((c == '.') || (c == '-') || (c == '_'))
968 				continue;
969 			return invalfc(fc, "Invalid name");
970 		}
971 		/* Specifying two names is forbidden */
972 		if (ctx->name)
973 			return invalfc(fc, "name respecified");
974 		ctx->name = param->string;
975 		param->string = NULL;
976 		break;
977 	}
978 	return 0;
979 }
980 
981 static int check_cgroupfs_options(struct fs_context *fc)
982 {
983 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
984 	u16 mask = U16_MAX;
985 	u16 enabled = 0;
986 	struct cgroup_subsys *ss;
987 	int i;
988 
989 #ifdef CONFIG_CPUSETS
990 	mask = ~((u16)1 << cpuset_cgrp_id);
991 #endif
992 	for_each_subsys(ss, i)
993 		if (cgroup_ssid_enabled(i) && !cgroup1_ssid_disabled(i))
994 			enabled |= 1 << i;
995 
996 	ctx->subsys_mask &= enabled;
997 
998 	/*
999 	 * In absense of 'none', 'name=' or subsystem name options,
1000 	 * let's default to 'all'.
1001 	 */
1002 	if (!ctx->subsys_mask && !ctx->none && !ctx->name)
1003 		ctx->all_ss = true;
1004 
1005 	if (ctx->all_ss) {
1006 		/* Mutually exclusive option 'all' + subsystem name */
1007 		if (ctx->subsys_mask)
1008 			return invalfc(fc, "subsys name conflicts with all");
1009 		/* 'all' => select all the subsystems */
1010 		ctx->subsys_mask = enabled;
1011 	}
1012 
1013 	/*
1014 	 * We either have to specify by name or by subsystems. (So all
1015 	 * empty hierarchies must have a name).
1016 	 */
1017 	if (!ctx->subsys_mask && !ctx->name)
1018 		return invalfc(fc, "Need name or subsystem set");
1019 
1020 	/*
1021 	 * Option noprefix was introduced just for backward compatibility
1022 	 * with the old cpuset, so we allow noprefix only if mounting just
1023 	 * the cpuset subsystem.
1024 	 */
1025 	if ((ctx->flags & CGRP_ROOT_NOPREFIX) && (ctx->subsys_mask & mask))
1026 		return invalfc(fc, "noprefix used incorrectly");
1027 
1028 	/* Can't specify "none" and some subsystems */
1029 	if (ctx->subsys_mask && ctx->none)
1030 		return invalfc(fc, "none used incorrectly");
1031 
1032 	return 0;
1033 }
1034 
1035 int cgroup1_reconfigure(struct fs_context *fc)
1036 {
1037 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1038 	struct kernfs_root *kf_root = kernfs_root_from_sb(fc->root->d_sb);
1039 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1040 	int ret = 0;
1041 	u16 added_mask, removed_mask;
1042 
1043 	cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1044 
1045 	/* See what subsystems are wanted */
1046 	ret = check_cgroupfs_options(fc);
1047 	if (ret)
1048 		goto out_unlock;
1049 
1050 	if (ctx->subsys_mask != root->subsys_mask || ctx->release_agent)
1051 		pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
1052 			task_tgid_nr(current), current->comm);
1053 
1054 	added_mask = ctx->subsys_mask & ~root->subsys_mask;
1055 	removed_mask = root->subsys_mask & ~ctx->subsys_mask;
1056 
1057 	/* Don't allow flags or name to change at remount */
1058 	if ((ctx->flags ^ root->flags) ||
1059 	    (ctx->name && strcmp(ctx->name, root->name))) {
1060 		errorfc(fc, "option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"",
1061 		       ctx->flags, ctx->name ?: "", root->flags, root->name);
1062 		ret = -EINVAL;
1063 		goto out_unlock;
1064 	}
1065 
1066 	/* remounting is not allowed for populated hierarchies */
1067 	if (!list_empty(&root->cgrp.self.children)) {
1068 		ret = -EBUSY;
1069 		goto out_unlock;
1070 	}
1071 
1072 	ret = rebind_subsystems(root, added_mask);
1073 	if (ret)
1074 		goto out_unlock;
1075 
1076 	WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
1077 
1078 	if (ctx->release_agent) {
1079 		spin_lock(&release_agent_path_lock);
1080 		strcpy(root->release_agent_path, ctx->release_agent);
1081 		spin_unlock(&release_agent_path_lock);
1082 	}
1083 
1084 	trace_cgroup_remount(root);
1085 
1086  out_unlock:
1087 	mutex_unlock(&cgroup_mutex);
1088 	return ret;
1089 }
1090 
1091 struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
1092 	.rename			= cgroup1_rename,
1093 	.show_options		= cgroup1_show_options,
1094 	.mkdir			= cgroup_mkdir,
1095 	.rmdir			= cgroup_rmdir,
1096 	.show_path		= cgroup_show_path,
1097 };
1098 
1099 /*
1100  * The guts of cgroup1 mount - find or create cgroup_root to use.
1101  * Called with cgroup_mutex held; returns 0 on success, -E... on
1102  * error and positive - in case when the candidate is busy dying.
1103  * On success it stashes a reference to cgroup_root into given
1104  * cgroup_fs_context; that reference is *NOT* counting towards the
1105  * cgroup_root refcount.
1106  */
1107 static int cgroup1_root_to_use(struct fs_context *fc)
1108 {
1109 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1110 	struct cgroup_root *root;
1111 	struct cgroup_subsys *ss;
1112 	int i, ret;
1113 
1114 	/* First find the desired set of subsystems */
1115 	ret = check_cgroupfs_options(fc);
1116 	if (ret)
1117 		return ret;
1118 
1119 	/*
1120 	 * Destruction of cgroup root is asynchronous, so subsystems may
1121 	 * still be dying after the previous unmount.  Let's drain the
1122 	 * dying subsystems.  We just need to ensure that the ones
1123 	 * unmounted previously finish dying and don't care about new ones
1124 	 * starting.  Testing ref liveliness is good enough.
1125 	 */
1126 	for_each_subsys(ss, i) {
1127 		if (!(ctx->subsys_mask & (1 << i)) ||
1128 		    ss->root == &cgrp_dfl_root)
1129 			continue;
1130 
1131 		if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt))
1132 			return 1;	/* restart */
1133 		cgroup_put(&ss->root->cgrp);
1134 	}
1135 
1136 	for_each_root(root) {
1137 		bool name_match = false;
1138 
1139 		if (root == &cgrp_dfl_root)
1140 			continue;
1141 
1142 		/*
1143 		 * If we asked for a name then it must match.  Also, if
1144 		 * name matches but sybsys_mask doesn't, we should fail.
1145 		 * Remember whether name matched.
1146 		 */
1147 		if (ctx->name) {
1148 			if (strcmp(ctx->name, root->name))
1149 				continue;
1150 			name_match = true;
1151 		}
1152 
1153 		/*
1154 		 * If we asked for subsystems (or explicitly for no
1155 		 * subsystems) then they must match.
1156 		 */
1157 		if ((ctx->subsys_mask || ctx->none) &&
1158 		    (ctx->subsys_mask != root->subsys_mask)) {
1159 			if (!name_match)
1160 				continue;
1161 			return -EBUSY;
1162 		}
1163 
1164 		if (root->flags ^ ctx->flags)
1165 			pr_warn("new mount options do not match the existing superblock, will be ignored\n");
1166 
1167 		ctx->root = root;
1168 		return 0;
1169 	}
1170 
1171 	/*
1172 	 * No such thing, create a new one.  name= matching without subsys
1173 	 * specification is allowed for already existing hierarchies but we
1174 	 * can't create new one without subsys specification.
1175 	 */
1176 	if (!ctx->subsys_mask && !ctx->none)
1177 		return invalfc(fc, "No subsys list or none specified");
1178 
1179 	/* Hierarchies may only be created in the initial cgroup namespace. */
1180 	if (ctx->ns != &init_cgroup_ns)
1181 		return -EPERM;
1182 
1183 	root = kzalloc(sizeof(*root), GFP_KERNEL);
1184 	if (!root)
1185 		return -ENOMEM;
1186 
1187 	ctx->root = root;
1188 	init_cgroup_root(ctx);
1189 
1190 	ret = cgroup_setup_root(root, ctx->subsys_mask);
1191 	if (ret)
1192 		cgroup_free_root(root);
1193 	return ret;
1194 }
1195 
1196 int cgroup1_get_tree(struct fs_context *fc)
1197 {
1198 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1199 	int ret;
1200 
1201 	/* Check if the caller has permission to mount. */
1202 	if (!ns_capable(ctx->ns->user_ns, CAP_SYS_ADMIN))
1203 		return -EPERM;
1204 
1205 	cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1206 
1207 	ret = cgroup1_root_to_use(fc);
1208 	if (!ret && !percpu_ref_tryget_live(&ctx->root->cgrp.self.refcnt))
1209 		ret = 1;	/* restart */
1210 
1211 	mutex_unlock(&cgroup_mutex);
1212 
1213 	if (!ret)
1214 		ret = cgroup_do_get_tree(fc);
1215 
1216 	if (!ret && percpu_ref_is_dying(&ctx->root->cgrp.self.refcnt)) {
1217 		struct super_block *sb = fc->root->d_sb;
1218 		dput(fc->root);
1219 		deactivate_locked_super(sb);
1220 		ret = 1;
1221 	}
1222 
1223 	if (unlikely(ret > 0)) {
1224 		msleep(10);
1225 		return restart_syscall();
1226 	}
1227 	return ret;
1228 }
1229 
1230 static int __init cgroup1_wq_init(void)
1231 {
1232 	/*
1233 	 * Used to destroy pidlists and separate to serve as flush domain.
1234 	 * Cap @max_active to 1 too.
1235 	 */
1236 	cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
1237 						    0, 1);
1238 	BUG_ON(!cgroup_pidlist_destroy_wq);
1239 	return 0;
1240 }
1241 core_initcall(cgroup1_wq_init);
1242 
1243 static int __init cgroup_no_v1(char *str)
1244 {
1245 	struct cgroup_subsys *ss;
1246 	char *token;
1247 	int i;
1248 
1249 	while ((token = strsep(&str, ",")) != NULL) {
1250 		if (!*token)
1251 			continue;
1252 
1253 		if (!strcmp(token, "all")) {
1254 			cgroup_no_v1_mask = U16_MAX;
1255 			continue;
1256 		}
1257 
1258 		if (!strcmp(token, "named")) {
1259 			cgroup_no_v1_named = true;
1260 			continue;
1261 		}
1262 
1263 		for_each_subsys(ss, i) {
1264 			if (strcmp(token, ss->name) &&
1265 			    strcmp(token, ss->legacy_name))
1266 				continue;
1267 
1268 			cgroup_no_v1_mask |= 1 << i;
1269 		}
1270 	}
1271 	return 1;
1272 }
1273 __setup("cgroup_no_v1=", cgroup_no_v1);
1274