xref: /openbmc/linux/kernel/cgroup/cgroup-v1.c (revision 1ca0b605)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20a268dbdSTejun Heo #include "cgroup-internal.h"
30a268dbdSTejun Heo 
41592c9b2STejun Heo #include <linux/ctype.h>
50a268dbdSTejun Heo #include <linux/kmod.h>
60a268dbdSTejun Heo #include <linux/sort.h>
71592c9b2STejun Heo #include <linux/delay.h>
80a268dbdSTejun Heo #include <linux/mm.h>
9c3edc401SIngo Molnar #include <linux/sched/signal.h>
1056cd6973SIngo Molnar #include <linux/sched/task.h>
1150ff9d13SIngo Molnar #include <linux/magic.h>
120a268dbdSTejun Heo #include <linux/slab.h>
130a268dbdSTejun Heo #include <linux/vmalloc.h>
140a268dbdSTejun Heo #include <linux/delayacct.h>
150a268dbdSTejun Heo #include <linux/pid_namespace.h>
160a268dbdSTejun Heo #include <linux/cgroupstats.h>
178d2451f4SAl Viro #include <linux/fs_parser.h>
180a268dbdSTejun Heo 
190a268dbdSTejun Heo #include <trace/events/cgroup.h>
200a268dbdSTejun Heo 
210a268dbdSTejun Heo /*
220a268dbdSTejun Heo  * pidlists linger the following amount before being destroyed.  The goal
230a268dbdSTejun Heo  * is avoiding frequent destruction in the middle of consecutive read calls
240a268dbdSTejun Heo  * Expiring in the middle is a performance problem not a correctness one.
250a268dbdSTejun Heo  * 1 sec should be enough.
260a268dbdSTejun Heo  */
270a268dbdSTejun Heo #define CGROUP_PIDLIST_DESTROY_DELAY	HZ
280a268dbdSTejun Heo 
290a268dbdSTejun Heo /* Controllers blocked by the commandline in v1 */
300a268dbdSTejun Heo static u16 cgroup_no_v1_mask;
310a268dbdSTejun Heo 
323fc9c12dSTejun Heo /* disable named v1 mounts */
333fc9c12dSTejun Heo static bool cgroup_no_v1_named;
343fc9c12dSTejun Heo 
350a268dbdSTejun Heo /*
360a268dbdSTejun Heo  * pidlist destructions need to be flushed on cgroup destruction.  Use a
370a268dbdSTejun Heo  * separate workqueue as flush domain.
380a268dbdSTejun Heo  */
390a268dbdSTejun Heo static struct workqueue_struct *cgroup_pidlist_destroy_wq;
400a268dbdSTejun Heo 
41e7b20d97STejun Heo /* protects cgroup_subsys->release_agent_path */
421592c9b2STejun Heo static DEFINE_SPINLOCK(release_agent_path_lock);
430a268dbdSTejun Heo 
cgroup1_ssid_disabled(int ssid)44d62beb7fSTejun Heo bool cgroup1_ssid_disabled(int ssid)
450a268dbdSTejun Heo {
460a268dbdSTejun Heo 	return cgroup_no_v1_mask & (1 << ssid);
470a268dbdSTejun Heo }
480a268dbdSTejun Heo 
490a268dbdSTejun Heo /**
500a268dbdSTejun Heo  * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
510a268dbdSTejun Heo  * @from: attach to all cgroups of a given task
520a268dbdSTejun Heo  * @tsk: the task to be attached
53b4cc6196SRandy Dunlap  *
54b4cc6196SRandy Dunlap  * Return: %0 on success or a negative errno code on failure
550a268dbdSTejun Heo  */
cgroup_attach_task_all(struct task_struct * from,struct task_struct * tsk)560a268dbdSTejun Heo int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
570a268dbdSTejun Heo {
580a268dbdSTejun Heo 	struct cgroup_root *root;
590a268dbdSTejun Heo 	int retval = 0;
600a268dbdSTejun Heo 
614cdb91b0SKamalesh Babulal 	cgroup_lock();
62075b593fSTetsuo Handa 	cgroup_attach_lock(true);
630a268dbdSTejun Heo 	for_each_root(root) {
640a268dbdSTejun Heo 		struct cgroup *from_cgrp;
650a268dbdSTejun Heo 
660a268dbdSTejun Heo 		spin_lock_irq(&css_set_lock);
670a268dbdSTejun Heo 		from_cgrp = task_cgroup_from_root(from, root);
680a268dbdSTejun Heo 		spin_unlock_irq(&css_set_lock);
690a268dbdSTejun Heo 
700a268dbdSTejun Heo 		retval = cgroup_attach_task(from_cgrp, tsk, false);
710a268dbdSTejun Heo 		if (retval)
720a268dbdSTejun Heo 			break;
730a268dbdSTejun Heo 	}
74075b593fSTetsuo Handa 	cgroup_attach_unlock(true);
754cdb91b0SKamalesh Babulal 	cgroup_unlock();
760a268dbdSTejun Heo 
770a268dbdSTejun Heo 	return retval;
780a268dbdSTejun Heo }
790a268dbdSTejun Heo EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
800a268dbdSTejun Heo 
810a268dbdSTejun Heo /**
82b4cc6196SRandy Dunlap  * cgroup_transfer_tasks - move tasks from one cgroup to another
830a268dbdSTejun Heo  * @to: cgroup to which the tasks will be moved
840a268dbdSTejun Heo  * @from: cgroup in which the tasks currently reside
850a268dbdSTejun Heo  *
860a268dbdSTejun Heo  * Locking rules between cgroup_post_fork() and the migration path
870a268dbdSTejun Heo  * guarantee that, if a task is forking while being migrated, the new child
880a268dbdSTejun Heo  * is guaranteed to be either visible in the source cgroup after the
890a268dbdSTejun Heo  * parent's migration is complete or put into the target cgroup.  No task
900a268dbdSTejun Heo  * can slip out of migration through forking.
91b4cc6196SRandy Dunlap  *
92b4cc6196SRandy Dunlap  * Return: %0 on success or a negative errno code on failure
930a268dbdSTejun Heo  */
cgroup_transfer_tasks(struct cgroup * to,struct cgroup * from)940a268dbdSTejun Heo int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
950a268dbdSTejun Heo {
96e595cd70STejun Heo 	DEFINE_CGROUP_MGCTX(mgctx);
970a268dbdSTejun Heo 	struct cgrp_cset_link *link;
980a268dbdSTejun Heo 	struct css_task_iter it;
990a268dbdSTejun Heo 	struct task_struct *task;
1000a268dbdSTejun Heo 	int ret;
1010a268dbdSTejun Heo 
1020a268dbdSTejun Heo 	if (cgroup_on_dfl(to))
1030a268dbdSTejun Heo 		return -EINVAL;
1040a268dbdSTejun Heo 
1058cfd8147STejun Heo 	ret = cgroup_migrate_vet_dst(to);
1068cfd8147STejun Heo 	if (ret)
1078cfd8147STejun Heo 		return ret;
1080a268dbdSTejun Heo 
1094cdb91b0SKamalesh Babulal 	cgroup_lock();
1100a268dbdSTejun Heo 
111ab1de7eaSQi Zheng 	cgroup_attach_lock(true);
1120a268dbdSTejun Heo 
1130a268dbdSTejun Heo 	/* all tasks in @from are being moved, all csets are source */
1140a268dbdSTejun Heo 	spin_lock_irq(&css_set_lock);
1150a268dbdSTejun Heo 	list_for_each_entry(link, &from->cset_links, cset_link)
116e595cd70STejun Heo 		cgroup_migrate_add_src(link->cset, to, &mgctx);
1170a268dbdSTejun Heo 	spin_unlock_irq(&css_set_lock);
1180a268dbdSTejun Heo 
119e595cd70STejun Heo 	ret = cgroup_migrate_prepare_dst(&mgctx);
1200a268dbdSTejun Heo 	if (ret)
1210a268dbdSTejun Heo 		goto out_err;
1220a268dbdSTejun Heo 
1230a268dbdSTejun Heo 	/*
1240a268dbdSTejun Heo 	 * Migrate tasks one-by-one until @from is empty.  This fails iff
1250a268dbdSTejun Heo 	 * ->can_attach() fails.
1260a268dbdSTejun Heo 	 */
1270a268dbdSTejun Heo 	do {
128bc2fb7edSTejun Heo 		css_task_iter_start(&from->self, 0, &it);
129116d2f74SPrateek Sood 
130116d2f74SPrateek Sood 		do {
1310a268dbdSTejun Heo 			task = css_task_iter_next(&it);
132116d2f74SPrateek Sood 		} while (task && (task->flags & PF_EXITING));
133116d2f74SPrateek Sood 
1340a268dbdSTejun Heo 		if (task)
1350a268dbdSTejun Heo 			get_task_struct(task);
1360a268dbdSTejun Heo 		css_task_iter_end(&it);
1370a268dbdSTejun Heo 
1380a268dbdSTejun Heo 		if (task) {
139bfc2cf6fSTejun Heo 			ret = cgroup_migrate(task, false, &mgctx);
1400a268dbdSTejun Heo 			if (!ret)
141e4f8d81cSSteven Rostedt (VMware) 				TRACE_CGROUP_PATH(transfer_tasks, to, task, false);
1420a268dbdSTejun Heo 			put_task_struct(task);
1430a268dbdSTejun Heo 		}
1440a268dbdSTejun Heo 	} while (task && !ret);
1450a268dbdSTejun Heo out_err:
146e595cd70STejun Heo 	cgroup_migrate_finish(&mgctx);
147ab1de7eaSQi Zheng 	cgroup_attach_unlock(true);
1484cdb91b0SKamalesh Babulal 	cgroup_unlock();
1490a268dbdSTejun Heo 	return ret;
1500a268dbdSTejun Heo }
1510a268dbdSTejun Heo 
1520a268dbdSTejun Heo /*
1530a268dbdSTejun Heo  * Stuff for reading the 'tasks'/'procs' files.
1540a268dbdSTejun Heo  *
1550a268dbdSTejun Heo  * Reading this file can return large amounts of data if a cgroup has
1560a268dbdSTejun Heo  * *lots* of attached tasks. So it may need several calls to read(),
1570a268dbdSTejun Heo  * but we cannot guarantee that the information we produce is correct
1580a268dbdSTejun Heo  * unless we produce it entirely atomically.
1590a268dbdSTejun Heo  *
1600a268dbdSTejun Heo  */
1610a268dbdSTejun Heo 
1620a268dbdSTejun Heo /* which pidlist file are we talking about? */
1630a268dbdSTejun Heo enum cgroup_filetype {
1640a268dbdSTejun Heo 	CGROUP_FILE_PROCS,
1650a268dbdSTejun Heo 	CGROUP_FILE_TASKS,
1660a268dbdSTejun Heo };
1670a268dbdSTejun Heo 
1680a268dbdSTejun Heo /*
1690a268dbdSTejun Heo  * A pidlist is a list of pids that virtually represents the contents of one
1700a268dbdSTejun Heo  * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
1710a268dbdSTejun Heo  * a pair (one each for procs, tasks) for each pid namespace that's relevant
1720a268dbdSTejun Heo  * to the cgroup.
1730a268dbdSTejun Heo  */
1740a268dbdSTejun Heo struct cgroup_pidlist {
1750a268dbdSTejun Heo 	/*
1760a268dbdSTejun Heo 	 * used to find which pidlist is wanted. doesn't change as long as
1770a268dbdSTejun Heo 	 * this particular list stays in the list.
1780a268dbdSTejun Heo 	*/
1790a268dbdSTejun Heo 	struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
1800a268dbdSTejun Heo 	/* array of xids */
1810a268dbdSTejun Heo 	pid_t *list;
1820a268dbdSTejun Heo 	/* how many elements the above list has */
1830a268dbdSTejun Heo 	int length;
1840a268dbdSTejun Heo 	/* each of these stored in a list by its cgroup */
1850a268dbdSTejun Heo 	struct list_head links;
1860a268dbdSTejun Heo 	/* pointer to the cgroup we belong to, for list removal purposes */
1870a268dbdSTejun Heo 	struct cgroup *owner;
1880a268dbdSTejun Heo 	/* for delayed destruction */
1890a268dbdSTejun Heo 	struct delayed_work destroy_dwork;
1900a268dbdSTejun Heo };
1910a268dbdSTejun Heo 
1920a268dbdSTejun Heo /*
1930a268dbdSTejun Heo  * Used to destroy all pidlists lingering waiting for destroy timer.  None
1940a268dbdSTejun Heo  * should be left afterwards.
1950a268dbdSTejun Heo  */
cgroup1_pidlist_destroy_all(struct cgroup * cgrp)196d62beb7fSTejun Heo void cgroup1_pidlist_destroy_all(struct cgroup *cgrp)
1970a268dbdSTejun Heo {
1980a268dbdSTejun Heo 	struct cgroup_pidlist *l, *tmp_l;
1990a268dbdSTejun Heo 
2000a268dbdSTejun Heo 	mutex_lock(&cgrp->pidlist_mutex);
2010a268dbdSTejun Heo 	list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
2020a268dbdSTejun Heo 		mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
2030a268dbdSTejun Heo 	mutex_unlock(&cgrp->pidlist_mutex);
2040a268dbdSTejun Heo 
2050a268dbdSTejun Heo 	flush_workqueue(cgroup_pidlist_destroy_wq);
2060a268dbdSTejun Heo 	BUG_ON(!list_empty(&cgrp->pidlists));
2070a268dbdSTejun Heo }
2080a268dbdSTejun Heo 
cgroup_pidlist_destroy_work_fn(struct work_struct * work)2090a268dbdSTejun Heo static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
2100a268dbdSTejun Heo {
2110a268dbdSTejun Heo 	struct delayed_work *dwork = to_delayed_work(work);
2120a268dbdSTejun Heo 	struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
2130a268dbdSTejun Heo 						destroy_dwork);
2140a268dbdSTejun Heo 	struct cgroup_pidlist *tofree = NULL;
2150a268dbdSTejun Heo 
2160a268dbdSTejun Heo 	mutex_lock(&l->owner->pidlist_mutex);
2170a268dbdSTejun Heo 
2180a268dbdSTejun Heo 	/*
2190a268dbdSTejun Heo 	 * Destroy iff we didn't get queued again.  The state won't change
2200a268dbdSTejun Heo 	 * as destroy_dwork can only be queued while locked.
2210a268dbdSTejun Heo 	 */
2220a268dbdSTejun Heo 	if (!delayed_work_pending(dwork)) {
2230a268dbdSTejun Heo 		list_del(&l->links);
224653a23caSMarc Koderer 		kvfree(l->list);
2250a268dbdSTejun Heo 		put_pid_ns(l->key.ns);
2260a268dbdSTejun Heo 		tofree = l;
2270a268dbdSTejun Heo 	}
2280a268dbdSTejun Heo 
2290a268dbdSTejun Heo 	mutex_unlock(&l->owner->pidlist_mutex);
2300a268dbdSTejun Heo 	kfree(tofree);
2310a268dbdSTejun Heo }
2320a268dbdSTejun Heo 
2330a268dbdSTejun Heo /*
2340a268dbdSTejun Heo  * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2350a268dbdSTejun Heo  * Returns the number of unique elements.
2360a268dbdSTejun Heo  */
pidlist_uniq(pid_t * list,int length)2370a268dbdSTejun Heo static int pidlist_uniq(pid_t *list, int length)
2380a268dbdSTejun Heo {
2390a268dbdSTejun Heo 	int src, dest = 1;
2400a268dbdSTejun Heo 
2410a268dbdSTejun Heo 	/*
2420a268dbdSTejun Heo 	 * we presume the 0th element is unique, so i starts at 1. trivial
2430a268dbdSTejun Heo 	 * edge cases first; no work needs to be done for either
2440a268dbdSTejun Heo 	 */
2450a268dbdSTejun Heo 	if (length == 0 || length == 1)
2460a268dbdSTejun Heo 		return length;
2470a268dbdSTejun Heo 	/* src and dest walk down the list; dest counts unique elements */
2480a268dbdSTejun Heo 	for (src = 1; src < length; src++) {
2490a268dbdSTejun Heo 		/* find next unique element */
2500a268dbdSTejun Heo 		while (list[src] == list[src-1]) {
2510a268dbdSTejun Heo 			src++;
2520a268dbdSTejun Heo 			if (src == length)
2530a268dbdSTejun Heo 				goto after;
2540a268dbdSTejun Heo 		}
2550a268dbdSTejun Heo 		/* dest always points to where the next unique element goes */
2560a268dbdSTejun Heo 		list[dest] = list[src];
2570a268dbdSTejun Heo 		dest++;
2580a268dbdSTejun Heo 	}
2590a268dbdSTejun Heo after:
2600a268dbdSTejun Heo 	return dest;
2610a268dbdSTejun Heo }
2620a268dbdSTejun Heo 
2630a268dbdSTejun Heo /*
2640a268dbdSTejun Heo  * The two pid files - task and cgroup.procs - guaranteed that the result
2650a268dbdSTejun Heo  * is sorted, which forced this whole pidlist fiasco.  As pid order is
2660a268dbdSTejun Heo  * different per namespace, each namespace needs differently sorted list,
2670a268dbdSTejun Heo  * making it impossible to use, for example, single rbtree of member tasks
2680a268dbdSTejun Heo  * sorted by task pointer.  As pidlists can be fairly large, allocating one
2690a268dbdSTejun Heo  * per open file is dangerous, so cgroup had to implement shared pool of
2700a268dbdSTejun Heo  * pidlists keyed by cgroup and namespace.
2710a268dbdSTejun Heo  */
cmppid(const void * a,const void * b)2720a268dbdSTejun Heo static int cmppid(const void *a, const void *b)
2730a268dbdSTejun Heo {
2740a268dbdSTejun Heo 	return *(pid_t *)a - *(pid_t *)b;
2750a268dbdSTejun Heo }
2760a268dbdSTejun Heo 
cgroup_pidlist_find(struct cgroup * cgrp,enum cgroup_filetype type)2770a268dbdSTejun Heo static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2780a268dbdSTejun Heo 						  enum cgroup_filetype type)
2790a268dbdSTejun Heo {
2800a268dbdSTejun Heo 	struct cgroup_pidlist *l;
2810a268dbdSTejun Heo 	/* don't need task_nsproxy() if we're looking at ourself */
2820a268dbdSTejun Heo 	struct pid_namespace *ns = task_active_pid_ns(current);
2830a268dbdSTejun Heo 
2840a268dbdSTejun Heo 	lockdep_assert_held(&cgrp->pidlist_mutex);
2850a268dbdSTejun Heo 
2860a268dbdSTejun Heo 	list_for_each_entry(l, &cgrp->pidlists, links)
2870a268dbdSTejun Heo 		if (l->key.type == type && l->key.ns == ns)
2880a268dbdSTejun Heo 			return l;
2890a268dbdSTejun Heo 	return NULL;
2900a268dbdSTejun Heo }
2910a268dbdSTejun Heo 
2920a268dbdSTejun Heo /*
2930a268dbdSTejun Heo  * find the appropriate pidlist for our purpose (given procs vs tasks)
2940a268dbdSTejun Heo  * returns with the lock on that pidlist already held, and takes care
2950a268dbdSTejun Heo  * of the use count, or returns NULL with no locks held if we're out of
2960a268dbdSTejun Heo  * memory.
2970a268dbdSTejun Heo  */
cgroup_pidlist_find_create(struct cgroup * cgrp,enum cgroup_filetype type)2980a268dbdSTejun Heo static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
2990a268dbdSTejun Heo 						enum cgroup_filetype type)
3000a268dbdSTejun Heo {
3010a268dbdSTejun Heo 	struct cgroup_pidlist *l;
3020a268dbdSTejun Heo 
3030a268dbdSTejun Heo 	lockdep_assert_held(&cgrp->pidlist_mutex);
3040a268dbdSTejun Heo 
3050a268dbdSTejun Heo 	l = cgroup_pidlist_find(cgrp, type);
3060a268dbdSTejun Heo 	if (l)
3070a268dbdSTejun Heo 		return l;
3080a268dbdSTejun Heo 
3090a268dbdSTejun Heo 	/* entry not found; create a new one */
3100a268dbdSTejun Heo 	l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3110a268dbdSTejun Heo 	if (!l)
3120a268dbdSTejun Heo 		return l;
3130a268dbdSTejun Heo 
3140a268dbdSTejun Heo 	INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
3150a268dbdSTejun Heo 	l->key.type = type;
3160a268dbdSTejun Heo 	/* don't need task_nsproxy() if we're looking at ourself */
3170a268dbdSTejun Heo 	l->key.ns = get_pid_ns(task_active_pid_ns(current));
3180a268dbdSTejun Heo 	l->owner = cgrp;
3190a268dbdSTejun Heo 	list_add(&l->links, &cgrp->pidlists);
3200a268dbdSTejun Heo 	return l;
3210a268dbdSTejun Heo }
3220a268dbdSTejun Heo 
3230a268dbdSTejun Heo /*
3240a268dbdSTejun Heo  * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3250a268dbdSTejun Heo  */
pidlist_array_load(struct cgroup * cgrp,enum cgroup_filetype type,struct cgroup_pidlist ** lp)3260a268dbdSTejun Heo static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3270a268dbdSTejun Heo 			      struct cgroup_pidlist **lp)
3280a268dbdSTejun Heo {
3290a268dbdSTejun Heo 	pid_t *array;
3300a268dbdSTejun Heo 	int length;
3310a268dbdSTejun Heo 	int pid, n = 0; /* used for populating the array */
3320a268dbdSTejun Heo 	struct css_task_iter it;
3330a268dbdSTejun Heo 	struct task_struct *tsk;
3340a268dbdSTejun Heo 	struct cgroup_pidlist *l;
3350a268dbdSTejun Heo 
3360a268dbdSTejun Heo 	lockdep_assert_held(&cgrp->pidlist_mutex);
3370a268dbdSTejun Heo 
3380a268dbdSTejun Heo 	/*
3390a268dbdSTejun Heo 	 * If cgroup gets more users after we read count, we won't have
3400a268dbdSTejun Heo 	 * enough space - tough.  This race is indistinguishable to the
3410a268dbdSTejun Heo 	 * caller from the case that the additional cgroup users didn't
3420a268dbdSTejun Heo 	 * show up until sometime later on.
3430a268dbdSTejun Heo 	 */
3440a268dbdSTejun Heo 	length = cgroup_task_count(cgrp);
345653a23caSMarc Koderer 	array = kvmalloc_array(length, sizeof(pid_t), GFP_KERNEL);
3460a268dbdSTejun Heo 	if (!array)
3470a268dbdSTejun Heo 		return -ENOMEM;
3480a268dbdSTejun Heo 	/* now, populate the array */
349bc2fb7edSTejun Heo 	css_task_iter_start(&cgrp->self, 0, &it);
3500a268dbdSTejun Heo 	while ((tsk = css_task_iter_next(&it))) {
3510a268dbdSTejun Heo 		if (unlikely(n == length))
3520a268dbdSTejun Heo 			break;
3530a268dbdSTejun Heo 		/* get tgid or pid for procs or tasks file respectively */
3540a268dbdSTejun Heo 		if (type == CGROUP_FILE_PROCS)
3550a268dbdSTejun Heo 			pid = task_tgid_vnr(tsk);
3560a268dbdSTejun Heo 		else
3570a268dbdSTejun Heo 			pid = task_pid_vnr(tsk);
3580a268dbdSTejun Heo 		if (pid > 0) /* make sure to only use valid results */
3590a268dbdSTejun Heo 			array[n++] = pid;
3600a268dbdSTejun Heo 	}
3610a268dbdSTejun Heo 	css_task_iter_end(&it);
3620a268dbdSTejun Heo 	length = n;
363*1ca0b605SMichal Koutný 	/* now sort & strip out duplicates (tgids or recycled thread PIDs) */
3640a268dbdSTejun Heo 	sort(array, length, sizeof(pid_t), cmppid, NULL);
3650a268dbdSTejun Heo 	length = pidlist_uniq(array, length);
3660a268dbdSTejun Heo 
3670a268dbdSTejun Heo 	l = cgroup_pidlist_find_create(cgrp, type);
3680a268dbdSTejun Heo 	if (!l) {
369653a23caSMarc Koderer 		kvfree(array);
3700a268dbdSTejun Heo 		return -ENOMEM;
3710a268dbdSTejun Heo 	}
3720a268dbdSTejun Heo 
3730a268dbdSTejun Heo 	/* store array, freeing old if necessary */
374653a23caSMarc Koderer 	kvfree(l->list);
3750a268dbdSTejun Heo 	l->list = array;
3760a268dbdSTejun Heo 	l->length = length;
3770a268dbdSTejun Heo 	*lp = l;
3780a268dbdSTejun Heo 	return 0;
3790a268dbdSTejun Heo }
3800a268dbdSTejun Heo 
3810a268dbdSTejun Heo /*
3820a268dbdSTejun Heo  * seq_file methods for the tasks/procs files. The seq_file position is the
3830a268dbdSTejun Heo  * next pid to display; the seq_file iterator is a pointer to the pid
3840a268dbdSTejun Heo  * in the cgroup->l->list array.
3850a268dbdSTejun Heo  */
3860a268dbdSTejun Heo 
cgroup_pidlist_start(struct seq_file * s,loff_t * pos)3870a268dbdSTejun Heo static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3880a268dbdSTejun Heo {
3890a268dbdSTejun Heo 	/*
3900a268dbdSTejun Heo 	 * Initially we receive a position value that corresponds to
3910a268dbdSTejun Heo 	 * one more than the last pid shown (or 0 on the first call or
3920a268dbdSTejun Heo 	 * after a seek to the start). Use a binary-search to find the
3930a268dbdSTejun Heo 	 * next pid to display, if any
3940a268dbdSTejun Heo 	 */
3950a268dbdSTejun Heo 	struct kernfs_open_file *of = s->private;
3960d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
3970a268dbdSTejun Heo 	struct cgroup *cgrp = seq_css(s)->cgroup;
3980a268dbdSTejun Heo 	struct cgroup_pidlist *l;
3990a268dbdSTejun Heo 	enum cgroup_filetype type = seq_cft(s)->private;
4000a268dbdSTejun Heo 	int index = 0, pid = *pos;
4010a268dbdSTejun Heo 	int *iter, ret;
4020a268dbdSTejun Heo 
4030a268dbdSTejun Heo 	mutex_lock(&cgrp->pidlist_mutex);
4040a268dbdSTejun Heo 
4050a268dbdSTejun Heo 	/*
4060d2b5955STejun Heo 	 * !NULL @ctx->procs1.pidlist indicates that this isn't the first
4070d2b5955STejun Heo 	 * start() after open. If the matching pidlist is around, we can use
4080d2b5955STejun Heo 	 * that. Look for it. Note that @ctx->procs1.pidlist can't be used
4090d2b5955STejun Heo 	 * directly. It could already have been destroyed.
4100a268dbdSTejun Heo 	 */
4110d2b5955STejun Heo 	if (ctx->procs1.pidlist)
4120d2b5955STejun Heo 		ctx->procs1.pidlist = cgroup_pidlist_find(cgrp, type);
4130a268dbdSTejun Heo 
4140a268dbdSTejun Heo 	/*
4150a268dbdSTejun Heo 	 * Either this is the first start() after open or the matching
4160a268dbdSTejun Heo 	 * pidlist has been destroyed inbetween.  Create a new one.
4170a268dbdSTejun Heo 	 */
4180d2b5955STejun Heo 	if (!ctx->procs1.pidlist) {
4190d2b5955STejun Heo 		ret = pidlist_array_load(cgrp, type, &ctx->procs1.pidlist);
4200a268dbdSTejun Heo 		if (ret)
4210a268dbdSTejun Heo 			return ERR_PTR(ret);
4220a268dbdSTejun Heo 	}
4230d2b5955STejun Heo 	l = ctx->procs1.pidlist;
4240a268dbdSTejun Heo 
4250a268dbdSTejun Heo 	if (pid) {
4260a268dbdSTejun Heo 		int end = l->length;
4270a268dbdSTejun Heo 
4280a268dbdSTejun Heo 		while (index < end) {
4290a268dbdSTejun Heo 			int mid = (index + end) / 2;
4300a268dbdSTejun Heo 			if (l->list[mid] == pid) {
4310a268dbdSTejun Heo 				index = mid;
4320a268dbdSTejun Heo 				break;
433e7e64a1bSMiaohe Lin 			} else if (l->list[mid] < pid)
4340a268dbdSTejun Heo 				index = mid + 1;
4350a268dbdSTejun Heo 			else
4360a268dbdSTejun Heo 				end = mid;
4370a268dbdSTejun Heo 		}
4380a268dbdSTejun Heo 	}
4390a268dbdSTejun Heo 	/* If we're off the end of the array, we're done */
4400a268dbdSTejun Heo 	if (index >= l->length)
4410a268dbdSTejun Heo 		return NULL;
4420a268dbdSTejun Heo 	/* Update the abstract position to be the actual pid that we found */
4430a268dbdSTejun Heo 	iter = l->list + index;
4440a268dbdSTejun Heo 	*pos = *iter;
4450a268dbdSTejun Heo 	return iter;
4460a268dbdSTejun Heo }
4470a268dbdSTejun Heo 
cgroup_pidlist_stop(struct seq_file * s,void * v)4480a268dbdSTejun Heo static void cgroup_pidlist_stop(struct seq_file *s, void *v)
4490a268dbdSTejun Heo {
4500a268dbdSTejun Heo 	struct kernfs_open_file *of = s->private;
4510d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
4520d2b5955STejun Heo 	struct cgroup_pidlist *l = ctx->procs1.pidlist;
4530a268dbdSTejun Heo 
4540a268dbdSTejun Heo 	if (l)
4550a268dbdSTejun Heo 		mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
4560a268dbdSTejun Heo 				 CGROUP_PIDLIST_DESTROY_DELAY);
4570a268dbdSTejun Heo 	mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
4580a268dbdSTejun Heo }
4590a268dbdSTejun Heo 
cgroup_pidlist_next(struct seq_file * s,void * v,loff_t * pos)4600a268dbdSTejun Heo static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
4610a268dbdSTejun Heo {
4620a268dbdSTejun Heo 	struct kernfs_open_file *of = s->private;
4630d2b5955STejun Heo 	struct cgroup_file_ctx *ctx = of->priv;
4640d2b5955STejun Heo 	struct cgroup_pidlist *l = ctx->procs1.pidlist;
4650a268dbdSTejun Heo 	pid_t *p = v;
4660a268dbdSTejun Heo 	pid_t *end = l->list + l->length;
4670a268dbdSTejun Heo 	/*
4680a268dbdSTejun Heo 	 * Advance to the next pid in the array. If this goes off the
4690a268dbdSTejun Heo 	 * end, we're done
4700a268dbdSTejun Heo 	 */
4710a268dbdSTejun Heo 	p++;
4720a268dbdSTejun Heo 	if (p >= end) {
473db8dd969SVasily Averin 		(*pos)++;
4740a268dbdSTejun Heo 		return NULL;
4750a268dbdSTejun Heo 	} else {
4760a268dbdSTejun Heo 		*pos = *p;
4770a268dbdSTejun Heo 		return p;
4780a268dbdSTejun Heo 	}
4790a268dbdSTejun Heo }
4800a268dbdSTejun Heo 
cgroup_pidlist_show(struct seq_file * s,void * v)4810a268dbdSTejun Heo static int cgroup_pidlist_show(struct seq_file *s, void *v)
4820a268dbdSTejun Heo {
4830a268dbdSTejun Heo 	seq_printf(s, "%d\n", *(int *)v);
4840a268dbdSTejun Heo 
4850a268dbdSTejun Heo 	return 0;
4860a268dbdSTejun Heo }
4870a268dbdSTejun Heo 
__cgroup1_procs_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off,bool threadgroup)488715c809dSTejun Heo static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,
489715c809dSTejun Heo 				     char *buf, size_t nbytes, loff_t off,
490715c809dSTejun Heo 				     bool threadgroup)
491715c809dSTejun Heo {
492715c809dSTejun Heo 	struct cgroup *cgrp;
493715c809dSTejun Heo 	struct task_struct *task;
494715c809dSTejun Heo 	const struct cred *cred, *tcred;
495715c809dSTejun Heo 	ssize_t ret;
4969a3284faSMichal Koutný 	bool locked;
497715c809dSTejun Heo 
498715c809dSTejun Heo 	cgrp = cgroup_kn_lock_live(of->kn, false);
499715c809dSTejun Heo 	if (!cgrp)
500715c809dSTejun Heo 		return -ENODEV;
501715c809dSTejun Heo 
5029a3284faSMichal Koutný 	task = cgroup_procs_write_start(buf, threadgroup, &locked);
503715c809dSTejun Heo 	ret = PTR_ERR_OR_ZERO(task);
504715c809dSTejun Heo 	if (ret)
505715c809dSTejun Heo 		goto out_unlock;
506715c809dSTejun Heo 
507715c809dSTejun Heo 	/*
5081756d799STejun Heo 	 * Even if we're attaching all tasks in the thread group, we only need
5091756d799STejun Heo 	 * to check permissions on one of them. Check permissions using the
5101756d799STejun Heo 	 * credentials from file open to protect against inherited fd attacks.
511715c809dSTejun Heo 	 */
5121756d799STejun Heo 	cred = of->file->f_cred;
513715c809dSTejun Heo 	tcred = get_task_cred(task);
514715c809dSTejun Heo 	if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
515715c809dSTejun Heo 	    !uid_eq(cred->euid, tcred->uid) &&
516715c809dSTejun Heo 	    !uid_eq(cred->euid, tcred->suid))
517715c809dSTejun Heo 		ret = -EACCES;
518715c809dSTejun Heo 	put_cred(tcred);
519715c809dSTejun Heo 	if (ret)
520715c809dSTejun Heo 		goto out_finish;
521715c809dSTejun Heo 
522715c809dSTejun Heo 	ret = cgroup_attach_task(cgrp, task, threadgroup);
523715c809dSTejun Heo 
524715c809dSTejun Heo out_finish:
5259a3284faSMichal Koutný 	cgroup_procs_write_finish(task, locked);
526715c809dSTejun Heo out_unlock:
527715c809dSTejun Heo 	cgroup_kn_unlock(of->kn);
528715c809dSTejun Heo 
529715c809dSTejun Heo 	return ret ?: nbytes;
530715c809dSTejun Heo }
531715c809dSTejun Heo 
cgroup1_procs_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)532715c809dSTejun Heo static ssize_t cgroup1_procs_write(struct kernfs_open_file *of,
5330a268dbdSTejun Heo 				   char *buf, size_t nbytes, loff_t off)
5340a268dbdSTejun Heo {
535715c809dSTejun Heo 	return __cgroup1_procs_write(of, buf, nbytes, off, true);
536715c809dSTejun Heo }
537715c809dSTejun Heo 
cgroup1_tasks_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)538715c809dSTejun Heo static ssize_t cgroup1_tasks_write(struct kernfs_open_file *of,
539715c809dSTejun Heo 				   char *buf, size_t nbytes, loff_t off)
540715c809dSTejun Heo {
541715c809dSTejun Heo 	return __cgroup1_procs_write(of, buf, nbytes, off, false);
5420a268dbdSTejun Heo }
5430a268dbdSTejun Heo 
cgroup_release_agent_write(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)5440a268dbdSTejun Heo static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
5450a268dbdSTejun Heo 					  char *buf, size_t nbytes, loff_t off)
5460a268dbdSTejun Heo {
5470a268dbdSTejun Heo 	struct cgroup *cgrp;
548467a726bSMichal Koutný 	struct cgroup_file_ctx *ctx;
5490a268dbdSTejun Heo 
5500a268dbdSTejun Heo 	BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
5510a268dbdSTejun Heo 
55224f60085SEric W. Biederman 	/*
55324f60085SEric W. Biederman 	 * Release agent gets called with all capabilities,
55424f60085SEric W. Biederman 	 * require capabilities to set release agent.
55524f60085SEric W. Biederman 	 */
556467a726bSMichal Koutný 	ctx = of->priv;
557467a726bSMichal Koutný 	if ((ctx->ns->user_ns != &init_user_ns) ||
558467a726bSMichal Koutný 	    !file_ns_capable(of->file, &init_user_ns, CAP_SYS_ADMIN))
55924f60085SEric W. Biederman 		return -EPERM;
56024f60085SEric W. Biederman 
5610a268dbdSTejun Heo 	cgrp = cgroup_kn_lock_live(of->kn, false);
5620a268dbdSTejun Heo 	if (!cgrp)
5630a268dbdSTejun Heo 		return -ENODEV;
5640a268dbdSTejun Heo 	spin_lock(&release_agent_path_lock);
565c33080cdSAzeem Shaikh 	strscpy(cgrp->root->release_agent_path, strstrip(buf),
5660a268dbdSTejun Heo 		sizeof(cgrp->root->release_agent_path));
5670a268dbdSTejun Heo 	spin_unlock(&release_agent_path_lock);
5680a268dbdSTejun Heo 	cgroup_kn_unlock(of->kn);
5690a268dbdSTejun Heo 	return nbytes;
5700a268dbdSTejun Heo }
5710a268dbdSTejun Heo 
cgroup_release_agent_show(struct seq_file * seq,void * v)5720a268dbdSTejun Heo static int cgroup_release_agent_show(struct seq_file *seq, void *v)
5730a268dbdSTejun Heo {
5740a268dbdSTejun Heo 	struct cgroup *cgrp = seq_css(seq)->cgroup;
5750a268dbdSTejun Heo 
5760a268dbdSTejun Heo 	spin_lock(&release_agent_path_lock);
5770a268dbdSTejun Heo 	seq_puts(seq, cgrp->root->release_agent_path);
5780a268dbdSTejun Heo 	spin_unlock(&release_agent_path_lock);
5790a268dbdSTejun Heo 	seq_putc(seq, '\n');
5800a268dbdSTejun Heo 	return 0;
5810a268dbdSTejun Heo }
5820a268dbdSTejun Heo 
cgroup_sane_behavior_show(struct seq_file * seq,void * v)5830a268dbdSTejun Heo static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
5840a268dbdSTejun Heo {
5850a268dbdSTejun Heo 	seq_puts(seq, "0\n");
5860a268dbdSTejun Heo 	return 0;
5870a268dbdSTejun Heo }
5880a268dbdSTejun Heo 
cgroup_read_notify_on_release(struct cgroup_subsys_state * css,struct cftype * cft)5890a268dbdSTejun Heo static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
5900a268dbdSTejun Heo 					 struct cftype *cft)
5910a268dbdSTejun Heo {
5920a268dbdSTejun Heo 	return notify_on_release(css->cgroup);
5930a268dbdSTejun Heo }
5940a268dbdSTejun Heo 
cgroup_write_notify_on_release(struct cgroup_subsys_state * css,struct cftype * cft,u64 val)5950a268dbdSTejun Heo static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
5960a268dbdSTejun Heo 					  struct cftype *cft, u64 val)
5970a268dbdSTejun Heo {
5980a268dbdSTejun Heo 	if (val)
5990a268dbdSTejun Heo 		set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
6000a268dbdSTejun Heo 	else
6010a268dbdSTejun Heo 		clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
6020a268dbdSTejun Heo 	return 0;
6030a268dbdSTejun Heo }
6040a268dbdSTejun Heo 
cgroup_clone_children_read(struct cgroup_subsys_state * css,struct cftype * cft)6050a268dbdSTejun Heo static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
6060a268dbdSTejun Heo 				      struct cftype *cft)
6070a268dbdSTejun Heo {
6080a268dbdSTejun Heo 	return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
6090a268dbdSTejun Heo }
6100a268dbdSTejun Heo 
cgroup_clone_children_write(struct cgroup_subsys_state * css,struct cftype * cft,u64 val)6110a268dbdSTejun Heo static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
6120a268dbdSTejun Heo 				       struct cftype *cft, u64 val)
6130a268dbdSTejun Heo {
6140a268dbdSTejun Heo 	if (val)
6150a268dbdSTejun Heo 		set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
6160a268dbdSTejun Heo 	else
6170a268dbdSTejun Heo 		clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
6180a268dbdSTejun Heo 	return 0;
6190a268dbdSTejun Heo }
6200a268dbdSTejun Heo 
6210a268dbdSTejun Heo /* cgroup core interface files for the legacy hierarchies */
622d62beb7fSTejun Heo struct cftype cgroup1_base_files[] = {
6230a268dbdSTejun Heo 	{
6240a268dbdSTejun Heo 		.name = "cgroup.procs",
6250a268dbdSTejun Heo 		.seq_start = cgroup_pidlist_start,
6260a268dbdSTejun Heo 		.seq_next = cgroup_pidlist_next,
6270a268dbdSTejun Heo 		.seq_stop = cgroup_pidlist_stop,
6280a268dbdSTejun Heo 		.seq_show = cgroup_pidlist_show,
6290a268dbdSTejun Heo 		.private = CGROUP_FILE_PROCS,
630715c809dSTejun Heo 		.write = cgroup1_procs_write,
6310a268dbdSTejun Heo 	},
6320a268dbdSTejun Heo 	{
6330a268dbdSTejun Heo 		.name = "cgroup.clone_children",
6340a268dbdSTejun Heo 		.read_u64 = cgroup_clone_children_read,
6350a268dbdSTejun Heo 		.write_u64 = cgroup_clone_children_write,
6360a268dbdSTejun Heo 	},
6370a268dbdSTejun Heo 	{
6380a268dbdSTejun Heo 		.name = "cgroup.sane_behavior",
6390a268dbdSTejun Heo 		.flags = CFTYPE_ONLY_ON_ROOT,
6400a268dbdSTejun Heo 		.seq_show = cgroup_sane_behavior_show,
6410a268dbdSTejun Heo 	},
6420a268dbdSTejun Heo 	{
6430a268dbdSTejun Heo 		.name = "tasks",
6440a268dbdSTejun Heo 		.seq_start = cgroup_pidlist_start,
6450a268dbdSTejun Heo 		.seq_next = cgroup_pidlist_next,
6460a268dbdSTejun Heo 		.seq_stop = cgroup_pidlist_stop,
6470a268dbdSTejun Heo 		.seq_show = cgroup_pidlist_show,
6480a268dbdSTejun Heo 		.private = CGROUP_FILE_TASKS,
649715c809dSTejun Heo 		.write = cgroup1_tasks_write,
6500a268dbdSTejun Heo 	},
6510a268dbdSTejun Heo 	{
6520a268dbdSTejun Heo 		.name = "notify_on_release",
6530a268dbdSTejun Heo 		.read_u64 = cgroup_read_notify_on_release,
6540a268dbdSTejun Heo 		.write_u64 = cgroup_write_notify_on_release,
6550a268dbdSTejun Heo 	},
6560a268dbdSTejun Heo 	{
6570a268dbdSTejun Heo 		.name = "release_agent",
6580a268dbdSTejun Heo 		.flags = CFTYPE_ONLY_ON_ROOT,
6590a268dbdSTejun Heo 		.seq_show = cgroup_release_agent_show,
6600a268dbdSTejun Heo 		.write = cgroup_release_agent_write,
6610a268dbdSTejun Heo 		.max_write_len = PATH_MAX - 1,
6620a268dbdSTejun Heo 	},
6630a268dbdSTejun Heo 	{ }	/* terminate */
6640a268dbdSTejun Heo };
6650a268dbdSTejun Heo 
6660a268dbdSTejun Heo /* Display information about each subsystem and each hierarchy */
proc_cgroupstats_show(struct seq_file * m,void * v)6673f3942acSChristoph Hellwig int proc_cgroupstats_show(struct seq_file *m, void *v)
6680a268dbdSTejun Heo {
6690a268dbdSTejun Heo 	struct cgroup_subsys *ss;
6700a268dbdSTejun Heo 	int i;
6710a268dbdSTejun Heo 
6720a268dbdSTejun Heo 	seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
6730a268dbdSTejun Heo 	/*
674822bc9baSShakeel Butt 	 * Grab the subsystems state racily. No need to add avenue to
675822bc9baSShakeel Butt 	 * cgroup_mutex contention.
6760a268dbdSTejun Heo 	 */
6770a268dbdSTejun Heo 
6780a268dbdSTejun Heo 	for_each_subsys(ss, i)
6790a268dbdSTejun Heo 		seq_printf(m, "%s\t%d\t%d\t%d\n",
6800a268dbdSTejun Heo 			   ss->legacy_name, ss->root->hierarchy_id,
6810a268dbdSTejun Heo 			   atomic_read(&ss->root->nr_cgrps),
6820a268dbdSTejun Heo 			   cgroup_ssid_enabled(i));
6830a268dbdSTejun Heo 
6840a268dbdSTejun Heo 	return 0;
6850a268dbdSTejun Heo }
6860a268dbdSTejun Heo 
6870a268dbdSTejun Heo /**
6880a268dbdSTejun Heo  * cgroupstats_build - build and fill cgroupstats
6890a268dbdSTejun Heo  * @stats: cgroupstats to fill information into
6900a268dbdSTejun Heo  * @dentry: A dentry entry belonging to the cgroup for which stats have
6910a268dbdSTejun Heo  * been requested.
6920a268dbdSTejun Heo  *
6930a268dbdSTejun Heo  * Build and fill cgroupstats so that taskstats can export it to user
6940a268dbdSTejun Heo  * space.
695b4cc6196SRandy Dunlap  *
696b4cc6196SRandy Dunlap  * Return: %0 on success or a negative errno code on failure
6970a268dbdSTejun Heo  */
cgroupstats_build(struct cgroupstats * stats,struct dentry * dentry)6980a268dbdSTejun Heo int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
6990a268dbdSTejun Heo {
7000a268dbdSTejun Heo 	struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
7010a268dbdSTejun Heo 	struct cgroup *cgrp;
7020a268dbdSTejun Heo 	struct css_task_iter it;
7030a268dbdSTejun Heo 	struct task_struct *tsk;
7040a268dbdSTejun Heo 
7050a268dbdSTejun Heo 	/* it should be kernfs_node belonging to cgroupfs and is a directory */
7060a268dbdSTejun Heo 	if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
7070a268dbdSTejun Heo 	    kernfs_type(kn) != KERNFS_DIR)
7080a268dbdSTejun Heo 		return -EINVAL;
7090a268dbdSTejun Heo 
7100a268dbdSTejun Heo 	/*
7110a268dbdSTejun Heo 	 * We aren't being called from kernfs and there's no guarantee on
7120a268dbdSTejun Heo 	 * @kn->priv's validity.  For this and css_tryget_online_from_dir(),
7130a268dbdSTejun Heo 	 * @kn->priv is RCU safe.  Let's do the RCU dancing.
7140a268dbdSTejun Heo 	 */
7150a268dbdSTejun Heo 	rcu_read_lock();
716e0aed7c7STejun Heo 	cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
717bb758421SShakeel Butt 	if (!cgrp || !cgroup_tryget(cgrp)) {
7180a268dbdSTejun Heo 		rcu_read_unlock();
7190a268dbdSTejun Heo 		return -ENOENT;
7200a268dbdSTejun Heo 	}
7210a268dbdSTejun Heo 	rcu_read_unlock();
7220a268dbdSTejun Heo 
723bc2fb7edSTejun Heo 	css_task_iter_start(&cgrp->self, 0, &it);
7240a268dbdSTejun Heo 	while ((tsk = css_task_iter_next(&it))) {
7252f064a59SPeter Zijlstra 		switch (READ_ONCE(tsk->__state)) {
7260a268dbdSTejun Heo 		case TASK_RUNNING:
7270a268dbdSTejun Heo 			stats->nr_running++;
7280a268dbdSTejun Heo 			break;
7290a268dbdSTejun Heo 		case TASK_INTERRUPTIBLE:
7300a268dbdSTejun Heo 			stats->nr_sleeping++;
7310a268dbdSTejun Heo 			break;
7320a268dbdSTejun Heo 		case TASK_UNINTERRUPTIBLE:
7330a268dbdSTejun Heo 			stats->nr_uninterruptible++;
7340a268dbdSTejun Heo 			break;
7350a268dbdSTejun Heo 		case TASK_STOPPED:
7360a268dbdSTejun Heo 			stats->nr_stopped++;
7370a268dbdSTejun Heo 			break;
7380a268dbdSTejun Heo 		default:
739ffeee417SChunguang Xu 			if (tsk->in_iowait)
7400a268dbdSTejun Heo 				stats->nr_io_wait++;
7410a268dbdSTejun Heo 			break;
7420a268dbdSTejun Heo 		}
7430a268dbdSTejun Heo 	}
7440a268dbdSTejun Heo 	css_task_iter_end(&it);
7450a268dbdSTejun Heo 
746bb758421SShakeel Butt 	cgroup_put(cgrp);
7470a268dbdSTejun Heo 	return 0;
7480a268dbdSTejun Heo }
7490a268dbdSTejun Heo 
cgroup1_check_for_release(struct cgroup * cgrp)750d62beb7fSTejun Heo void cgroup1_check_for_release(struct cgroup *cgrp)
7510a268dbdSTejun Heo {
7520a268dbdSTejun Heo 	if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
7530a268dbdSTejun Heo 	    !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
7540a268dbdSTejun Heo 		schedule_work(&cgrp->release_agent_work);
7550a268dbdSTejun Heo }
7560a268dbdSTejun Heo 
7570a268dbdSTejun Heo /*
7580a268dbdSTejun Heo  * Notify userspace when a cgroup is released, by running the
7590a268dbdSTejun Heo  * configured release agent with the name of the cgroup (path
7600a268dbdSTejun Heo  * relative to the root of cgroup file system) as the argument.
7610a268dbdSTejun Heo  *
7620a268dbdSTejun Heo  * Most likely, this user command will try to rmdir this cgroup.
7630a268dbdSTejun Heo  *
7640a268dbdSTejun Heo  * This races with the possibility that some other task will be
7650a268dbdSTejun Heo  * attached to this cgroup before it is removed, or that some other
7660a268dbdSTejun Heo  * user task will 'mkdir' a child cgroup of this cgroup.  That's ok.
7670a268dbdSTejun Heo  * The presumed 'rmdir' will fail quietly if this cgroup is no longer
7680a268dbdSTejun Heo  * unused, and this cgroup will be reprieved from its death sentence,
7690a268dbdSTejun Heo  * to continue to serve a useful existence.  Next time it's released,
7700a268dbdSTejun Heo  * we will get notified again, if it still has 'notify_on_release' set.
7710a268dbdSTejun Heo  *
7720a268dbdSTejun Heo  * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
7730a268dbdSTejun Heo  * means only wait until the task is successfully execve()'d.  The
7740a268dbdSTejun Heo  * separate release agent task is forked by call_usermodehelper(),
7750a268dbdSTejun Heo  * then control in this thread returns here, without waiting for the
7760a268dbdSTejun Heo  * release agent task.  We don't bother to wait because the caller of
7770a268dbdSTejun Heo  * this routine has no use for the exit status of the release agent
7780a268dbdSTejun Heo  * task, so no sense holding our caller up for that.
7790a268dbdSTejun Heo  */
cgroup1_release_agent(struct work_struct * work)780d62beb7fSTejun Heo void cgroup1_release_agent(struct work_struct *work)
7810a268dbdSTejun Heo {
7820a268dbdSTejun Heo 	struct cgroup *cgrp =
7830a268dbdSTejun Heo 		container_of(work, struct cgroup, release_agent_work);
784e7b20d97STejun Heo 	char *pathbuf, *agentbuf;
7850a268dbdSTejun Heo 	char *argv[3], *envp[3];
7860a268dbdSTejun Heo 	int ret;
7870a268dbdSTejun Heo 
788e7b20d97STejun Heo 	/* snoop agent path and exit early if empty */
789e7b20d97STejun Heo 	if (!cgrp->root->release_agent_path[0])
790e7b20d97STejun Heo 		return;
7910a268dbdSTejun Heo 
792e7b20d97STejun Heo 	/* prepare argument buffers */
7930a268dbdSTejun Heo 	pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
794e7b20d97STejun Heo 	agentbuf = kmalloc(PATH_MAX, GFP_KERNEL);
795e7b20d97STejun Heo 	if (!pathbuf || !agentbuf)
796e7b20d97STejun Heo 		goto out_free;
7970a268dbdSTejun Heo 
798e7b20d97STejun Heo 	spin_lock(&release_agent_path_lock);
799c33080cdSAzeem Shaikh 	strscpy(agentbuf, cgrp->root->release_agent_path, PATH_MAX);
800e7b20d97STejun Heo 	spin_unlock(&release_agent_path_lock);
801e7b20d97STejun Heo 	if (!agentbuf[0])
802e7b20d97STejun Heo 		goto out_free;
803e7b20d97STejun Heo 
804e7b20d97STejun Heo 	ret = cgroup_path_ns(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
8050a268dbdSTejun Heo 	if (ret < 0 || ret >= PATH_MAX)
806e7b20d97STejun Heo 		goto out_free;
8070a268dbdSTejun Heo 
8080a268dbdSTejun Heo 	argv[0] = agentbuf;
8090a268dbdSTejun Heo 	argv[1] = pathbuf;
8100a268dbdSTejun Heo 	argv[2] = NULL;
8110a268dbdSTejun Heo 
8120a268dbdSTejun Heo 	/* minimal command environment */
8130a268dbdSTejun Heo 	envp[0] = "HOME=/";
8140a268dbdSTejun Heo 	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
8150a268dbdSTejun Heo 	envp[2] = NULL;
8160a268dbdSTejun Heo 
8170a268dbdSTejun Heo 	call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
8180a268dbdSTejun Heo out_free:
8190a268dbdSTejun Heo 	kfree(agentbuf);
8200a268dbdSTejun Heo 	kfree(pathbuf);
8210a268dbdSTejun Heo }
8220a268dbdSTejun Heo 
8230a268dbdSTejun Heo /*
8240a268dbdSTejun Heo  * cgroup_rename - Only allow simple rename of directories in place.
8250a268dbdSTejun Heo  */
cgroup1_rename(struct kernfs_node * kn,struct kernfs_node * new_parent,const char * new_name_str)8261592c9b2STejun Heo static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
8270a268dbdSTejun Heo 			  const char *new_name_str)
8280a268dbdSTejun Heo {
8290a268dbdSTejun Heo 	struct cgroup *cgrp = kn->priv;
8300a268dbdSTejun Heo 	int ret;
8310a268dbdSTejun Heo 
832b7e24eb1SAlexander Kuznetsov 	/* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */
833b7e24eb1SAlexander Kuznetsov 	if (strchr(new_name_str, '\n'))
834b7e24eb1SAlexander Kuznetsov 		return -EINVAL;
835b7e24eb1SAlexander Kuznetsov 
8360a268dbdSTejun Heo 	if (kernfs_type(kn) != KERNFS_DIR)
8370a268dbdSTejun Heo 		return -ENOTDIR;
8380a268dbdSTejun Heo 	if (kn->parent != new_parent)
8390a268dbdSTejun Heo 		return -EIO;
8400a268dbdSTejun Heo 
8410a268dbdSTejun Heo 	/*
8420a268dbdSTejun Heo 	 * We're gonna grab cgroup_mutex which nests outside kernfs
8430a268dbdSTejun Heo 	 * active_ref.  kernfs_rename() doesn't require active_ref
8440a268dbdSTejun Heo 	 * protection.  Break them before grabbing cgroup_mutex.
8450a268dbdSTejun Heo 	 */
8460a268dbdSTejun Heo 	kernfs_break_active_protection(new_parent);
8470a268dbdSTejun Heo 	kernfs_break_active_protection(kn);
8480a268dbdSTejun Heo 
8494cdb91b0SKamalesh Babulal 	cgroup_lock();
8500a268dbdSTejun Heo 
8510a268dbdSTejun Heo 	ret = kernfs_rename(kn, new_parent, new_name_str);
8520a268dbdSTejun Heo 	if (!ret)
853e4f8d81cSSteven Rostedt (VMware) 		TRACE_CGROUP_PATH(rename, cgrp);
8540a268dbdSTejun Heo 
8554cdb91b0SKamalesh Babulal 	cgroup_unlock();
8560a268dbdSTejun Heo 
8570a268dbdSTejun Heo 	kernfs_unbreak_active_protection(kn);
8580a268dbdSTejun Heo 	kernfs_unbreak_active_protection(new_parent);
8590a268dbdSTejun Heo 	return ret;
8600a268dbdSTejun Heo }
8610a268dbdSTejun Heo 
cgroup1_show_options(struct seq_file * seq,struct kernfs_root * kf_root)8621592c9b2STejun Heo static int cgroup1_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
8631592c9b2STejun Heo {
8641592c9b2STejun Heo 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
8651592c9b2STejun Heo 	struct cgroup_subsys *ss;
8661592c9b2STejun Heo 	int ssid;
8671592c9b2STejun Heo 
8681592c9b2STejun Heo 	for_each_subsys(ss, ssid)
8691592c9b2STejun Heo 		if (root->subsys_mask & (1 << ssid))
8701592c9b2STejun Heo 			seq_show_option(seq, ss->legacy_name, NULL);
8711592c9b2STejun Heo 	if (root->flags & CGRP_ROOT_NOPREFIX)
8721592c9b2STejun Heo 		seq_puts(seq, ",noprefix");
8731592c9b2STejun Heo 	if (root->flags & CGRP_ROOT_XATTR)
8741592c9b2STejun Heo 		seq_puts(seq, ",xattr");
875e1cba4b8SWaiman Long 	if (root->flags & CGRP_ROOT_CPUSET_V2_MODE)
876e1cba4b8SWaiman Long 		seq_puts(seq, ",cpuset_v2_mode");
8776a010a49STejun Heo 	if (root->flags & CGRP_ROOT_FAVOR_DYNMODS)
8786a010a49STejun Heo 		seq_puts(seq, ",favordynmods");
8791592c9b2STejun Heo 
8801592c9b2STejun Heo 	spin_lock(&release_agent_path_lock);
8811592c9b2STejun Heo 	if (strlen(root->release_agent_path))
8821592c9b2STejun Heo 		seq_show_option(seq, "release_agent",
8831592c9b2STejun Heo 				root->release_agent_path);
8841592c9b2STejun Heo 	spin_unlock(&release_agent_path_lock);
8851592c9b2STejun Heo 
8861592c9b2STejun Heo 	if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
8871592c9b2STejun Heo 		seq_puts(seq, ",clone_children");
8881592c9b2STejun Heo 	if (strlen(root->name))
8891592c9b2STejun Heo 		seq_show_option(seq, "name", root->name);
8901592c9b2STejun Heo 	return 0;
8911592c9b2STejun Heo }
8921592c9b2STejun Heo 
8938d2451f4SAl Viro enum cgroup1_param {
8948d2451f4SAl Viro 	Opt_all,
8958d2451f4SAl Viro 	Opt_clone_children,
8968d2451f4SAl Viro 	Opt_cpuset_v2_mode,
8978d2451f4SAl Viro 	Opt_name,
8988d2451f4SAl Viro 	Opt_none,
8998d2451f4SAl Viro 	Opt_noprefix,
9008d2451f4SAl Viro 	Opt_release_agent,
9018d2451f4SAl Viro 	Opt_xattr,
9026a010a49STejun Heo 	Opt_favordynmods,
9036a010a49STejun Heo 	Opt_nofavordynmods,
9048d2451f4SAl Viro };
9051592c9b2STejun Heo 
906d7167b14SAl Viro const struct fs_parameter_spec cgroup1_fs_parameters[] = {
9078d2451f4SAl Viro 	fsparam_flag  ("all",		Opt_all),
9088d2451f4SAl Viro 	fsparam_flag  ("clone_children", Opt_clone_children),
9098d2451f4SAl Viro 	fsparam_flag  ("cpuset_v2_mode", Opt_cpuset_v2_mode),
9108d2451f4SAl Viro 	fsparam_string("name",		Opt_name),
9118d2451f4SAl Viro 	fsparam_flag  ("none",		Opt_none),
9128d2451f4SAl Viro 	fsparam_flag  ("noprefix",	Opt_noprefix),
9138d2451f4SAl Viro 	fsparam_string("release_agent",	Opt_release_agent),
9148d2451f4SAl Viro 	fsparam_flag  ("xattr",		Opt_xattr),
9156a010a49STejun Heo 	fsparam_flag  ("favordynmods",	Opt_favordynmods),
9166a010a49STejun Heo 	fsparam_flag  ("nofavordynmods", Opt_nofavordynmods),
9178d2451f4SAl Viro 	{}
9188d2451f4SAl Viro };
9198d2451f4SAl Viro 
cgroup1_parse_param(struct fs_context * fc,struct fs_parameter * param)9208d2451f4SAl Viro int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param)
9218d2451f4SAl Viro {
9228d2451f4SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
9238d2451f4SAl Viro 	struct cgroup_subsys *ss;
9248d2451f4SAl Viro 	struct fs_parse_result result;
9258d2451f4SAl Viro 	int opt, i;
9268d2451f4SAl Viro 
927d7167b14SAl Viro 	opt = fs_parse(fc, cgroup1_fs_parameters, param, &result);
9288d2451f4SAl Viro 	if (opt == -ENOPARAM) {
929d1d488d8SChristian Brauner 		int ret;
930d1d488d8SChristian Brauner 
931d1d488d8SChristian Brauner 		ret = vfs_parse_fs_param_source(fc, param);
932d1d488d8SChristian Brauner 		if (ret != -ENOPARAM)
933d1d488d8SChristian Brauner 			return ret;
9348d2451f4SAl Viro 		for_each_subsys(ss, i) {
9358d2451f4SAl Viro 			if (strcmp(param->key, ss->legacy_name))
9368d2451f4SAl Viro 				continue;
93761e960b0SChen Zhou 			if (!cgroup_ssid_enabled(i) || cgroup1_ssid_disabled(i))
93861e960b0SChen Zhou 				return invalfc(fc, "Disabled controller '%s'",
93961e960b0SChen Zhou 					       param->key);
9408d2451f4SAl Viro 			ctx->subsys_mask |= (1 << i);
9418d2451f4SAl Viro 			return 0;
9428d2451f4SAl Viro 		}
94358c025f0SAl Viro 		return invalfc(fc, "Unknown subsys name '%s'", param->key);
9448d2451f4SAl Viro 	}
9458d2451f4SAl Viro 	if (opt < 0)
9468d2451f4SAl Viro 		return opt;
9478d2451f4SAl Viro 
9488d2451f4SAl Viro 	switch (opt) {
9498d2451f4SAl Viro 	case Opt_none:
9501592c9b2STejun Heo 		/* Explicitly have no subsystems */
951f5dfb531SAl Viro 		ctx->none = true;
9528d2451f4SAl Viro 		break;
9538d2451f4SAl Viro 	case Opt_all:
954f5dfb531SAl Viro 		ctx->all_ss = true;
9558d2451f4SAl Viro 		break;
9568d2451f4SAl Viro 	case Opt_noprefix:
957f5dfb531SAl Viro 		ctx->flags |= CGRP_ROOT_NOPREFIX;
9588d2451f4SAl Viro 		break;
9598d2451f4SAl Viro 	case Opt_clone_children:
960f5dfb531SAl Viro 		ctx->cpuset_clone_children = true;
9618d2451f4SAl Viro 		break;
9628d2451f4SAl Viro 	case Opt_cpuset_v2_mode:
963f5dfb531SAl Viro 		ctx->flags |= CGRP_ROOT_CPUSET_V2_MODE;
9648d2451f4SAl Viro 		break;
9658d2451f4SAl Viro 	case Opt_xattr:
966f5dfb531SAl Viro 		ctx->flags |= CGRP_ROOT_XATTR;
9678d2451f4SAl Viro 		break;
9686a010a49STejun Heo 	case Opt_favordynmods:
9696a010a49STejun Heo 		ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS;
9706a010a49STejun Heo 		break;
9716a010a49STejun Heo 	case Opt_nofavordynmods:
9726a010a49STejun Heo 		ctx->flags &= ~CGRP_ROOT_FAVOR_DYNMODS;
9736a010a49STejun Heo 		break;
9748d2451f4SAl Viro 	case Opt_release_agent:
9751592c9b2STejun Heo 		/* Specifying two release agents is forbidden */
976f5dfb531SAl Viro 		if (ctx->release_agent)
97758c025f0SAl Viro 			return invalfc(fc, "release_agent respecified");
97824f60085SEric W. Biederman 		/*
97924f60085SEric W. Biederman 		 * Release agent gets called with all capabilities,
98024f60085SEric W. Biederman 		 * require capabilities to set release agent.
98124f60085SEric W. Biederman 		 */
98224f60085SEric W. Biederman 		if ((fc->user_ns != &init_user_ns) || !capable(CAP_SYS_ADMIN))
98324f60085SEric W. Biederman 			return invalfc(fc, "Setting release_agent not allowed");
9848d2451f4SAl Viro 		ctx->release_agent = param->string;
9858d2451f4SAl Viro 		param->string = NULL;
9868d2451f4SAl Viro 		break;
9878d2451f4SAl Viro 	case Opt_name:
9883fc9c12dSTejun Heo 		/* blocked by boot param? */
9893fc9c12dSTejun Heo 		if (cgroup_no_v1_named)
9903fc9c12dSTejun Heo 			return -ENOENT;
9911592c9b2STejun Heo 		/* Can't specify an empty name */
9928d2451f4SAl Viro 		if (!param->size)
99358c025f0SAl Viro 			return invalfc(fc, "Empty name");
9948d2451f4SAl Viro 		if (param->size > MAX_CGROUP_ROOT_NAMELEN - 1)
99558c025f0SAl Viro 			return invalfc(fc, "Name too long");
9961592c9b2STejun Heo 		/* Must match [\w.-]+ */
9978d2451f4SAl Viro 		for (i = 0; i < param->size; i++) {
9988d2451f4SAl Viro 			char c = param->string[i];
9991592c9b2STejun Heo 			if (isalnum(c))
10001592c9b2STejun Heo 				continue;
10011592c9b2STejun Heo 			if ((c == '.') || (c == '-') || (c == '_'))
10021592c9b2STejun Heo 				continue;
100358c025f0SAl Viro 			return invalfc(fc, "Invalid name");
10041592c9b2STejun Heo 		}
10051592c9b2STejun Heo 		/* Specifying two names is forbidden */
1006f5dfb531SAl Viro 		if (ctx->name)
100758c025f0SAl Viro 			return invalfc(fc, "name respecified");
10088d2451f4SAl Viro 		ctx->name = param->string;
10098d2451f4SAl Viro 		param->string = NULL;
10101592c9b2STejun Heo 		break;
10111592c9b2STejun Heo 	}
1012f5dfb531SAl Viro 	return 0;
1013f5dfb531SAl Viro }
10141592c9b2STejun Heo 
check_cgroupfs_options(struct fs_context * fc)10158d2451f4SAl Viro static int check_cgroupfs_options(struct fs_context *fc)
1016f5dfb531SAl Viro {
10178d2451f4SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
1018f5dfb531SAl Viro 	u16 mask = U16_MAX;
1019f5dfb531SAl Viro 	u16 enabled = 0;
1020f5dfb531SAl Viro 	struct cgroup_subsys *ss;
1021f5dfb531SAl Viro 	int i;
1022f5dfb531SAl Viro 
1023f5dfb531SAl Viro #ifdef CONFIG_CPUSETS
1024f5dfb531SAl Viro 	mask = ~((u16)1 << cpuset_cgrp_id);
1025f5dfb531SAl Viro #endif
10261592c9b2STejun Heo 	for_each_subsys(ss, i)
1027d62beb7fSTejun Heo 		if (cgroup_ssid_enabled(i) && !cgroup1_ssid_disabled(i))
1028f5dfb531SAl Viro 			enabled |= 1 << i;
1029f5dfb531SAl Viro 
1030f5dfb531SAl Viro 	ctx->subsys_mask &= enabled;
1031f5dfb531SAl Viro 
1032f5dfb531SAl Viro 	/*
103308b2b6fdSZhen Lei 	 * In absence of 'none', 'name=' and subsystem name options,
1034f5dfb531SAl Viro 	 * let's default to 'all'.
1035f5dfb531SAl Viro 	 */
1036f5dfb531SAl Viro 	if (!ctx->subsys_mask && !ctx->none && !ctx->name)
1037f5dfb531SAl Viro 		ctx->all_ss = true;
1038f5dfb531SAl Viro 
1039f5dfb531SAl Viro 	if (ctx->all_ss) {
1040f5dfb531SAl Viro 		/* Mutually exclusive option 'all' + subsystem name */
1041f5dfb531SAl Viro 		if (ctx->subsys_mask)
104258c025f0SAl Viro 			return invalfc(fc, "subsys name conflicts with all");
1043f5dfb531SAl Viro 		/* 'all' => select all the subsystems */
1044f5dfb531SAl Viro 		ctx->subsys_mask = enabled;
1045f5dfb531SAl Viro 	}
10461592c9b2STejun Heo 
10471592c9b2STejun Heo 	/*
10481592c9b2STejun Heo 	 * We either have to specify by name or by subsystems. (So all
10491592c9b2STejun Heo 	 * empty hierarchies must have a name).
10501592c9b2STejun Heo 	 */
1051f5dfb531SAl Viro 	if (!ctx->subsys_mask && !ctx->name)
105258c025f0SAl Viro 		return invalfc(fc, "Need name or subsystem set");
10531592c9b2STejun Heo 
10541592c9b2STejun Heo 	/*
10551592c9b2STejun Heo 	 * Option noprefix was introduced just for backward compatibility
10561592c9b2STejun Heo 	 * with the old cpuset, so we allow noprefix only if mounting just
10571592c9b2STejun Heo 	 * the cpuset subsystem.
10581592c9b2STejun Heo 	 */
1059f5dfb531SAl Viro 	if ((ctx->flags & CGRP_ROOT_NOPREFIX) && (ctx->subsys_mask & mask))
106058c025f0SAl Viro 		return invalfc(fc, "noprefix used incorrectly");
10611592c9b2STejun Heo 
10621592c9b2STejun Heo 	/* Can't specify "none" and some subsystems */
1063f5dfb531SAl Viro 	if (ctx->subsys_mask && ctx->none)
106458c025f0SAl Viro 		return invalfc(fc, "none used incorrectly");
10651592c9b2STejun Heo 
10661592c9b2STejun Heo 	return 0;
10671592c9b2STejun Heo }
10681592c9b2STejun Heo 
cgroup1_reconfigure(struct fs_context * fc)106990129625SAl Viro int cgroup1_reconfigure(struct fs_context *fc)
10701592c9b2STejun Heo {
107190129625SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
107290129625SAl Viro 	struct kernfs_root *kf_root = kernfs_root_from_sb(fc->root->d_sb);
10731592c9b2STejun Heo 	struct cgroup_root *root = cgroup_root_from_kf(kf_root);
107490129625SAl Viro 	int ret = 0;
10751592c9b2STejun Heo 	u16 added_mask, removed_mask;
10761592c9b2STejun Heo 
10771592c9b2STejun Heo 	cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
10781592c9b2STejun Heo 
10791592c9b2STejun Heo 	/* See what subsystems are wanted */
10808d2451f4SAl Viro 	ret = check_cgroupfs_options(fc);
10811592c9b2STejun Heo 	if (ret)
10821592c9b2STejun Heo 		goto out_unlock;
10831592c9b2STejun Heo 
1084f5dfb531SAl Viro 	if (ctx->subsys_mask != root->subsys_mask || ctx->release_agent)
10851592c9b2STejun Heo 		pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
10861592c9b2STejun Heo 			task_tgid_nr(current), current->comm);
10871592c9b2STejun Heo 
1088f5dfb531SAl Viro 	added_mask = ctx->subsys_mask & ~root->subsys_mask;
1089f5dfb531SAl Viro 	removed_mask = root->subsys_mask & ~ctx->subsys_mask;
10901592c9b2STejun Heo 
10911592c9b2STejun Heo 	/* Don't allow flags or name to change at remount */
1092f5dfb531SAl Viro 	if ((ctx->flags ^ root->flags) ||
1093f5dfb531SAl Viro 	    (ctx->name && strcmp(ctx->name, root->name))) {
109458c025f0SAl Viro 		errorfc(fc, "option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"",
1095f5dfb531SAl Viro 		       ctx->flags, ctx->name ?: "", root->flags, root->name);
10961592c9b2STejun Heo 		ret = -EINVAL;
10971592c9b2STejun Heo 		goto out_unlock;
10981592c9b2STejun Heo 	}
10991592c9b2STejun Heo 
11001592c9b2STejun Heo 	/* remounting is not allowed for populated hierarchies */
11011592c9b2STejun Heo 	if (!list_empty(&root->cgrp.self.children)) {
11021592c9b2STejun Heo 		ret = -EBUSY;
11031592c9b2STejun Heo 		goto out_unlock;
11041592c9b2STejun Heo 	}
11051592c9b2STejun Heo 
11061592c9b2STejun Heo 	ret = rebind_subsystems(root, added_mask);
11071592c9b2STejun Heo 	if (ret)
11081592c9b2STejun Heo 		goto out_unlock;
11091592c9b2STejun Heo 
11101592c9b2STejun Heo 	WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
11111592c9b2STejun Heo 
1112f5dfb531SAl Viro 	if (ctx->release_agent) {
11131592c9b2STejun Heo 		spin_lock(&release_agent_path_lock);
1114f5dfb531SAl Viro 		strcpy(root->release_agent_path, ctx->release_agent);
11151592c9b2STejun Heo 		spin_unlock(&release_agent_path_lock);
11161592c9b2STejun Heo 	}
11171592c9b2STejun Heo 
11181592c9b2STejun Heo 	trace_cgroup_remount(root);
11191592c9b2STejun Heo 
11201592c9b2STejun Heo  out_unlock:
11214cdb91b0SKamalesh Babulal 	cgroup_unlock();
11221592c9b2STejun Heo 	return ret;
11231592c9b2STejun Heo }
11241592c9b2STejun Heo 
11251592c9b2STejun Heo struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
11261592c9b2STejun Heo 	.rename			= cgroup1_rename,
11271592c9b2STejun Heo 	.show_options		= cgroup1_show_options,
11281592c9b2STejun Heo 	.mkdir			= cgroup_mkdir,
11291592c9b2STejun Heo 	.rmdir			= cgroup_rmdir,
11301592c9b2STejun Heo 	.show_path		= cgroup_show_path,
11311592c9b2STejun Heo };
11321592c9b2STejun Heo 
11336678889fSAl Viro /*
11346678889fSAl Viro  * The guts of cgroup1 mount - find or create cgroup_root to use.
11356678889fSAl Viro  * Called with cgroup_mutex held; returns 0 on success, -E... on
11366678889fSAl Viro  * error and positive - in case when the candidate is busy dying.
11376678889fSAl Viro  * On success it stashes a reference to cgroup_root into given
11386678889fSAl Viro  * cgroup_fs_context; that reference is *NOT* counting towards the
11396678889fSAl Viro  * cgroup_root refcount.
11406678889fSAl Viro  */
cgroup1_root_to_use(struct fs_context * fc)11416678889fSAl Viro static int cgroup1_root_to_use(struct fs_context *fc)
11421592c9b2STejun Heo {
11437feeef58SAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
11441592c9b2STejun Heo 	struct cgroup_root *root;
11451592c9b2STejun Heo 	struct cgroup_subsys *ss;
11461592c9b2STejun Heo 	int i, ret;
11471592c9b2STejun Heo 
11481592c9b2STejun Heo 	/* First find the desired set of subsystems */
11498d2451f4SAl Viro 	ret = check_cgroupfs_options(fc);
11501592c9b2STejun Heo 	if (ret)
11516678889fSAl Viro 		return ret;
11521592c9b2STejun Heo 
11531592c9b2STejun Heo 	/*
11541592c9b2STejun Heo 	 * Destruction of cgroup root is asynchronous, so subsystems may
11551592c9b2STejun Heo 	 * still be dying after the previous unmount.  Let's drain the
11561592c9b2STejun Heo 	 * dying subsystems.  We just need to ensure that the ones
11571592c9b2STejun Heo 	 * unmounted previously finish dying and don't care about new ones
11581592c9b2STejun Heo 	 * starting.  Testing ref liveliness is good enough.
11591592c9b2STejun Heo 	 */
11601592c9b2STejun Heo 	for_each_subsys(ss, i) {
1161f5dfb531SAl Viro 		if (!(ctx->subsys_mask & (1 << i)) ||
11621592c9b2STejun Heo 		    ss->root == &cgrp_dfl_root)
11631592c9b2STejun Heo 			continue;
11641592c9b2STejun Heo 
11656678889fSAl Viro 		if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt))
11666678889fSAl Viro 			return 1;	/* restart */
11671592c9b2STejun Heo 		cgroup_put(&ss->root->cgrp);
11681592c9b2STejun Heo 	}
11691592c9b2STejun Heo 
11701592c9b2STejun Heo 	for_each_root(root) {
11711592c9b2STejun Heo 		bool name_match = false;
11721592c9b2STejun Heo 
11731592c9b2STejun Heo 		if (root == &cgrp_dfl_root)
11741592c9b2STejun Heo 			continue;
11751592c9b2STejun Heo 
11761592c9b2STejun Heo 		/*
11771592c9b2STejun Heo 		 * If we asked for a name then it must match.  Also, if
11781592c9b2STejun Heo 		 * name matches but sybsys_mask doesn't, we should fail.
11791592c9b2STejun Heo 		 * Remember whether name matched.
11801592c9b2STejun Heo 		 */
1181f5dfb531SAl Viro 		if (ctx->name) {
1182f5dfb531SAl Viro 			if (strcmp(ctx->name, root->name))
11831592c9b2STejun Heo 				continue;
11841592c9b2STejun Heo 			name_match = true;
11851592c9b2STejun Heo 		}
11861592c9b2STejun Heo 
11871592c9b2STejun Heo 		/*
11881592c9b2STejun Heo 		 * If we asked for subsystems (or explicitly for no
11891592c9b2STejun Heo 		 * subsystems) then they must match.
11901592c9b2STejun Heo 		 */
1191f5dfb531SAl Viro 		if ((ctx->subsys_mask || ctx->none) &&
1192f5dfb531SAl Viro 		    (ctx->subsys_mask != root->subsys_mask)) {
11931592c9b2STejun Heo 			if (!name_match)
11941592c9b2STejun Heo 				continue;
11956678889fSAl Viro 			return -EBUSY;
11961592c9b2STejun Heo 		}
11971592c9b2STejun Heo 
1198f5dfb531SAl Viro 		if (root->flags ^ ctx->flags)
11991592c9b2STejun Heo 			pr_warn("new mount options do not match the existing superblock, will be ignored\n");
12001592c9b2STejun Heo 
1201cf6299b1SAl Viro 		ctx->root = root;
12026678889fSAl Viro 		return 0;
12031592c9b2STejun Heo 	}
12041592c9b2STejun Heo 
12051592c9b2STejun Heo 	/*
12061592c9b2STejun Heo 	 * No such thing, create a new one.  name= matching without subsys
12071592c9b2STejun Heo 	 * specification is allowed for already existing hierarchies but we
12081592c9b2STejun Heo 	 * can't create new one without subsys specification.
12091592c9b2STejun Heo 	 */
12106678889fSAl Viro 	if (!ctx->subsys_mask && !ctx->none)
121158c025f0SAl Viro 		return invalfc(fc, "No subsys list or none specified");
12121592c9b2STejun Heo 
12131592c9b2STejun Heo 	/* Hierarchies may only be created in the initial cgroup namespace. */
1214cca8f327SAl Viro 	if (ctx->ns != &init_cgroup_ns)
12156678889fSAl Viro 		return -EPERM;
12161592c9b2STejun Heo 
12171592c9b2STejun Heo 	root = kzalloc(sizeof(*root), GFP_KERNEL);
12186678889fSAl Viro 	if (!root)
12196678889fSAl Viro 		return -ENOMEM;
12201592c9b2STejun Heo 
1221cf6299b1SAl Viro 	ctx->root = root;
1222cf6299b1SAl Viro 	init_cgroup_root(ctx);
12231592c9b2STejun Heo 
1224f5dfb531SAl Viro 	ret = cgroup_setup_root(root, ctx->subsys_mask);
12256a010a49STejun Heo 	if (!ret)
12266a010a49STejun Heo 		cgroup_favor_dynmods(root, ctx->flags & CGRP_ROOT_FAVOR_DYNMODS);
12276a010a49STejun Heo 	else
12281592c9b2STejun Heo 		cgroup_free_root(root);
12296a010a49STejun Heo 
12307feeef58SAl Viro 	return ret;
12316678889fSAl Viro }
12321592c9b2STejun Heo 
cgroup1_get_tree(struct fs_context * fc)12336678889fSAl Viro int cgroup1_get_tree(struct fs_context *fc)
12346678889fSAl Viro {
12356678889fSAl Viro 	struct cgroup_fs_context *ctx = cgroup_fc2context(fc);
12366678889fSAl Viro 	int ret;
12376678889fSAl Viro 
12386678889fSAl Viro 	/* Check if the caller has permission to mount. */
1239cca8f327SAl Viro 	if (!ns_capable(ctx->ns->user_ns, CAP_SYS_ADMIN))
12406678889fSAl Viro 		return -EPERM;
12416678889fSAl Viro 
12426678889fSAl Viro 	cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
12436678889fSAl Viro 
12446678889fSAl Viro 	ret = cgroup1_root_to_use(fc);
12456678889fSAl Viro 	if (!ret && !percpu_ref_tryget_live(&ctx->root->cgrp.self.refcnt))
12466678889fSAl Viro 		ret = 1;	/* restart */
12476678889fSAl Viro 
12484cdb91b0SKamalesh Babulal 	cgroup_unlock();
12496678889fSAl Viro 
12506678889fSAl Viro 	if (!ret)
1251cca8f327SAl Viro 		ret = cgroup_do_get_tree(fc);
12526678889fSAl Viro 
12536678889fSAl Viro 	if (!ret && percpu_ref_is_dying(&ctx->root->cgrp.self.refcnt)) {
12541e7107c5SPaul Gortmaker 		fc_drop_locked(fc);
12556678889fSAl Viro 		ret = 1;
12566678889fSAl Viro 	}
12576678889fSAl Viro 
12586678889fSAl Viro 	if (unlikely(ret > 0)) {
125935ac1184SAl Viro 		msleep(10);
12607feeef58SAl Viro 		return restart_syscall();
12619732adc5SZefan Li 	}
126271d883c3SAl Viro 	return ret;
12631592c9b2STejun Heo }
12641592c9b2STejun Heo 
cgroup1_wq_init(void)12650a268dbdSTejun Heo static int __init cgroup1_wq_init(void)
12660a268dbdSTejun Heo {
12670a268dbdSTejun Heo 	/*
12680a268dbdSTejun Heo 	 * Used to destroy pidlists and separate to serve as flush domain.
12690a268dbdSTejun Heo 	 * Cap @max_active to 1 too.
12700a268dbdSTejun Heo 	 */
12710a268dbdSTejun Heo 	cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
12720a268dbdSTejun Heo 						    0, 1);
12730a268dbdSTejun Heo 	BUG_ON(!cgroup_pidlist_destroy_wq);
12740a268dbdSTejun Heo 	return 0;
12750a268dbdSTejun Heo }
12760a268dbdSTejun Heo core_initcall(cgroup1_wq_init);
12770a268dbdSTejun Heo 
cgroup_no_v1(char * str)12780a268dbdSTejun Heo static int __init cgroup_no_v1(char *str)
12790a268dbdSTejun Heo {
12800a268dbdSTejun Heo 	struct cgroup_subsys *ss;
12810a268dbdSTejun Heo 	char *token;
12820a268dbdSTejun Heo 	int i;
12830a268dbdSTejun Heo 
12840a268dbdSTejun Heo 	while ((token = strsep(&str, ",")) != NULL) {
12850a268dbdSTejun Heo 		if (!*token)
12860a268dbdSTejun Heo 			continue;
12870a268dbdSTejun Heo 
12880a268dbdSTejun Heo 		if (!strcmp(token, "all")) {
12890a268dbdSTejun Heo 			cgroup_no_v1_mask = U16_MAX;
12903fc9c12dSTejun Heo 			continue;
12913fc9c12dSTejun Heo 		}
12923fc9c12dSTejun Heo 
12933fc9c12dSTejun Heo 		if (!strcmp(token, "named")) {
12943fc9c12dSTejun Heo 			cgroup_no_v1_named = true;
12953fc9c12dSTejun Heo 			continue;
12960a268dbdSTejun Heo 		}
12970a268dbdSTejun Heo 
12980a268dbdSTejun Heo 		for_each_subsys(ss, i) {
12990a268dbdSTejun Heo 			if (strcmp(token, ss->name) &&
13000a268dbdSTejun Heo 			    strcmp(token, ss->legacy_name))
13010a268dbdSTejun Heo 				continue;
13020a268dbdSTejun Heo 
13030a268dbdSTejun Heo 			cgroup_no_v1_mask |= 1 << i;
13040a268dbdSTejun Heo 		}
13050a268dbdSTejun Heo 	}
13060a268dbdSTejun Heo 	return 1;
13070a268dbdSTejun Heo }
13080a268dbdSTejun Heo __setup("cgroup_no_v1=", cgroup_no_v1);
1309