1 #include "cgroup-internal.h"
3 #include <linux/ctype.h>
4 #include <linux/kmod.h>
5 #include <linux/sort.h>
6 #include <linux/delay.h>
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/delayacct.h>
11 #include <linux/pid_namespace.h>
12 #include <linux/cgroupstats.h>
14 #include <trace/events/cgroup.h>
17 * pidlists linger the following amount before being destroyed. The goal
18 * is avoiding frequent destruction in the middle of consecutive read calls
19 * Expiring in the middle is a performance problem not a correctness one.
20 * 1 sec should be enough.
22 #define CGROUP_PIDLIST_DESTROY_DELAY HZ
24 /* Controllers blocked by the commandline in v1 */
25 static u16 cgroup_no_v1_mask;
28 * pidlist destructions need to be flushed on cgroup destruction. Use a
29 * separate workqueue as flush domain.
31 static struct workqueue_struct *cgroup_pidlist_destroy_wq;
34 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
35 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
37 static DEFINE_SPINLOCK(release_agent_path_lock);
39 bool cgroup1_ssid_disabled(int ssid)
41 return cgroup_no_v1_mask & (1 << ssid);
45 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
46 * @from: attach to all cgroups of a given task
47 * @tsk: the task to be attached
49 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
51 struct cgroup_root *root;
54 mutex_lock(&cgroup_mutex);
55 percpu_down_write(&cgroup_threadgroup_rwsem);
57 struct cgroup *from_cgrp;
59 if (root == &cgrp_dfl_root)
62 spin_lock_irq(&css_set_lock);
63 from_cgrp = task_cgroup_from_root(from, root);
64 spin_unlock_irq(&css_set_lock);
66 retval = cgroup_attach_task(from_cgrp, tsk, false);
70 percpu_up_write(&cgroup_threadgroup_rwsem);
71 mutex_unlock(&cgroup_mutex);
75 EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
78 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
79 * @to: cgroup to which the tasks will be moved
80 * @from: cgroup in which the tasks currently reside
82 * Locking rules between cgroup_post_fork() and the migration path
83 * guarantee that, if a task is forking while being migrated, the new child
84 * is guaranteed to be either visible in the source cgroup after the
85 * parent's migration is complete or put into the target cgroup. No task
86 * can slip out of migration through forking.
88 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
90 DEFINE_CGROUP_MGCTX(mgctx);
91 struct cgrp_cset_link *link;
92 struct css_task_iter it;
93 struct task_struct *task;
96 if (cgroup_on_dfl(to))
99 if (!cgroup_may_migrate_to(to))
102 mutex_lock(&cgroup_mutex);
104 percpu_down_write(&cgroup_threadgroup_rwsem);
106 /* all tasks in @from are being moved, all csets are source */
107 spin_lock_irq(&css_set_lock);
108 list_for_each_entry(link, &from->cset_links, cset_link)
109 cgroup_migrate_add_src(link->cset, to, &mgctx);
110 spin_unlock_irq(&css_set_lock);
112 ret = cgroup_migrate_prepare_dst(&mgctx);
117 * Migrate tasks one-by-one until @from is empty. This fails iff
118 * ->can_attach() fails.
121 css_task_iter_start(&from->self, &it);
122 task = css_task_iter_next(&it);
124 get_task_struct(task);
125 css_task_iter_end(&it);
128 ret = cgroup_migrate(task, false, &mgctx);
130 trace_cgroup_transfer_tasks(to, task, false);
131 put_task_struct(task);
133 } while (task && !ret);
135 cgroup_migrate_finish(&mgctx);
136 percpu_up_write(&cgroup_threadgroup_rwsem);
137 mutex_unlock(&cgroup_mutex);
142 * Stuff for reading the 'tasks'/'procs' files.
144 * Reading this file can return large amounts of data if a cgroup has
145 * *lots* of attached tasks. So it may need several calls to read(),
146 * but we cannot guarantee that the information we produce is correct
147 * unless we produce it entirely atomically.
151 /* which pidlist file are we talking about? */
152 enum cgroup_filetype {
158 * A pidlist is a list of pids that virtually represents the contents of one
159 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
160 * a pair (one each for procs, tasks) for each pid namespace that's relevant
163 struct cgroup_pidlist {
165 * used to find which pidlist is wanted. doesn't change as long as
166 * this particular list stays in the list.
168 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
171 /* how many elements the above list has */
173 /* each of these stored in a list by its cgroup */
174 struct list_head links;
175 /* pointer to the cgroup we belong to, for list removal purposes */
176 struct cgroup *owner;
177 /* for delayed destruction */
178 struct delayed_work destroy_dwork;
182 * The following two functions "fix" the issue where there are more pids
183 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
184 * TODO: replace with a kernel-wide solution to this problem
186 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
187 static void *pidlist_allocate(int count)
189 if (PIDLIST_TOO_LARGE(count))
190 return vmalloc(count * sizeof(pid_t));
192 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
195 static void pidlist_free(void *p)
201 * Used to destroy all pidlists lingering waiting for destroy timer. None
202 * should be left afterwards.
204 void cgroup1_pidlist_destroy_all(struct cgroup *cgrp)
206 struct cgroup_pidlist *l, *tmp_l;
208 mutex_lock(&cgrp->pidlist_mutex);
209 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
210 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
211 mutex_unlock(&cgrp->pidlist_mutex);
213 flush_workqueue(cgroup_pidlist_destroy_wq);
214 BUG_ON(!list_empty(&cgrp->pidlists));
217 static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
219 struct delayed_work *dwork = to_delayed_work(work);
220 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
222 struct cgroup_pidlist *tofree = NULL;
224 mutex_lock(&l->owner->pidlist_mutex);
227 * Destroy iff we didn't get queued again. The state won't change
228 * as destroy_dwork can only be queued while locked.
230 if (!delayed_work_pending(dwork)) {
232 pidlist_free(l->list);
233 put_pid_ns(l->key.ns);
237 mutex_unlock(&l->owner->pidlist_mutex);
242 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
243 * Returns the number of unique elements.
245 static int pidlist_uniq(pid_t *list, int length)
250 * we presume the 0th element is unique, so i starts at 1. trivial
251 * edge cases first; no work needs to be done for either
253 if (length == 0 || length == 1)
255 /* src and dest walk down the list; dest counts unique elements */
256 for (src = 1; src < length; src++) {
257 /* find next unique element */
258 while (list[src] == list[src-1]) {
263 /* dest always points to where the next unique element goes */
264 list[dest] = list[src];
272 * The two pid files - task and cgroup.procs - guaranteed that the result
273 * is sorted, which forced this whole pidlist fiasco. As pid order is
274 * different per namespace, each namespace needs differently sorted list,
275 * making it impossible to use, for example, single rbtree of member tasks
276 * sorted by task pointer. As pidlists can be fairly large, allocating one
277 * per open file is dangerous, so cgroup had to implement shared pool of
278 * pidlists keyed by cgroup and namespace.
280 static int cmppid(const void *a, const void *b)
282 return *(pid_t *)a - *(pid_t *)b;
285 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
286 enum cgroup_filetype type)
288 struct cgroup_pidlist *l;
289 /* don't need task_nsproxy() if we're looking at ourself */
290 struct pid_namespace *ns = task_active_pid_ns(current);
292 lockdep_assert_held(&cgrp->pidlist_mutex);
294 list_for_each_entry(l, &cgrp->pidlists, links)
295 if (l->key.type == type && l->key.ns == ns)
301 * find the appropriate pidlist for our purpose (given procs vs tasks)
302 * returns with the lock on that pidlist already held, and takes care
303 * of the use count, or returns NULL with no locks held if we're out of
306 static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
307 enum cgroup_filetype type)
309 struct cgroup_pidlist *l;
311 lockdep_assert_held(&cgrp->pidlist_mutex);
313 l = cgroup_pidlist_find(cgrp, type);
317 /* entry not found; create a new one */
318 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
322 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
324 /* don't need task_nsproxy() if we're looking at ourself */
325 l->key.ns = get_pid_ns(task_active_pid_ns(current));
327 list_add(&l->links, &cgrp->pidlists);
332 * cgroup_task_count - count the number of tasks in a cgroup.
333 * @cgrp: the cgroup in question
335 * Return the number of tasks in the cgroup. The returned number can be
336 * higher than the actual number of tasks due to css_set references from
337 * namespace roots and temporary usages.
339 static int cgroup_task_count(const struct cgroup *cgrp)
342 struct cgrp_cset_link *link;
344 spin_lock_irq(&css_set_lock);
345 list_for_each_entry(link, &cgrp->cset_links, cset_link)
346 count += atomic_read(&link->cset->refcount);
347 spin_unlock_irq(&css_set_lock);
352 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
354 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
355 struct cgroup_pidlist **lp)
359 int pid, n = 0; /* used for populating the array */
360 struct css_task_iter it;
361 struct task_struct *tsk;
362 struct cgroup_pidlist *l;
364 lockdep_assert_held(&cgrp->pidlist_mutex);
367 * If cgroup gets more users after we read count, we won't have
368 * enough space - tough. This race is indistinguishable to the
369 * caller from the case that the additional cgroup users didn't
370 * show up until sometime later on.
372 length = cgroup_task_count(cgrp);
373 array = pidlist_allocate(length);
376 /* now, populate the array */
377 css_task_iter_start(&cgrp->self, &it);
378 while ((tsk = css_task_iter_next(&it))) {
379 if (unlikely(n == length))
381 /* get tgid or pid for procs or tasks file respectively */
382 if (type == CGROUP_FILE_PROCS)
383 pid = task_tgid_vnr(tsk);
385 pid = task_pid_vnr(tsk);
386 if (pid > 0) /* make sure to only use valid results */
389 css_task_iter_end(&it);
391 /* now sort & (if procs) strip out duplicates */
392 sort(array, length, sizeof(pid_t), cmppid, NULL);
393 if (type == CGROUP_FILE_PROCS)
394 length = pidlist_uniq(array, length);
396 l = cgroup_pidlist_find_create(cgrp, type);
402 /* store array, freeing old if necessary */
403 pidlist_free(l->list);
411 * seq_file methods for the tasks/procs files. The seq_file position is the
412 * next pid to display; the seq_file iterator is a pointer to the pid
413 * in the cgroup->l->list array.
416 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
419 * Initially we receive a position value that corresponds to
420 * one more than the last pid shown (or 0 on the first call or
421 * after a seek to the start). Use a binary-search to find the
422 * next pid to display, if any
424 struct kernfs_open_file *of = s->private;
425 struct cgroup *cgrp = seq_css(s)->cgroup;
426 struct cgroup_pidlist *l;
427 enum cgroup_filetype type = seq_cft(s)->private;
428 int index = 0, pid = *pos;
431 mutex_lock(&cgrp->pidlist_mutex);
434 * !NULL @of->priv indicates that this isn't the first start()
435 * after open. If the matching pidlist is around, we can use that.
436 * Look for it. Note that @of->priv can't be used directly. It
437 * could already have been destroyed.
440 of->priv = cgroup_pidlist_find(cgrp, type);
443 * Either this is the first start() after open or the matching
444 * pidlist has been destroyed inbetween. Create a new one.
447 ret = pidlist_array_load(cgrp, type,
448 (struct cgroup_pidlist **)&of->priv);
457 while (index < end) {
458 int mid = (index + end) / 2;
459 if (l->list[mid] == pid) {
462 } else if (l->list[mid] <= pid)
468 /* If we're off the end of the array, we're done */
469 if (index >= l->length)
471 /* Update the abstract position to be the actual pid that we found */
472 iter = l->list + index;
477 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
479 struct kernfs_open_file *of = s->private;
480 struct cgroup_pidlist *l = of->priv;
483 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
484 CGROUP_PIDLIST_DESTROY_DELAY);
485 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
488 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
490 struct kernfs_open_file *of = s->private;
491 struct cgroup_pidlist *l = of->priv;
493 pid_t *end = l->list + l->length;
495 * Advance to the next pid in the array. If this goes off the
507 static int cgroup_pidlist_show(struct seq_file *s, void *v)
509 seq_printf(s, "%d\n", *(int *)v);
514 static ssize_t cgroup_tasks_write(struct kernfs_open_file *of,
515 char *buf, size_t nbytes, loff_t off)
517 return __cgroup_procs_write(of, buf, nbytes, off, false);
520 static ssize_t cgroup_release_agent_write(struct kernfs_open_file *of,
521 char *buf, size_t nbytes, loff_t off)
525 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
527 cgrp = cgroup_kn_lock_live(of->kn, false);
530 spin_lock(&release_agent_path_lock);
531 strlcpy(cgrp->root->release_agent_path, strstrip(buf),
532 sizeof(cgrp->root->release_agent_path));
533 spin_unlock(&release_agent_path_lock);
534 cgroup_kn_unlock(of->kn);
538 static int cgroup_release_agent_show(struct seq_file *seq, void *v)
540 struct cgroup *cgrp = seq_css(seq)->cgroup;
542 spin_lock(&release_agent_path_lock);
543 seq_puts(seq, cgrp->root->release_agent_path);
544 spin_unlock(&release_agent_path_lock);
549 static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
551 seq_puts(seq, "0\n");
555 static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
558 return notify_on_release(css->cgroup);
561 static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
562 struct cftype *cft, u64 val)
565 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
567 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
571 static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
574 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
577 static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
578 struct cftype *cft, u64 val)
581 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
583 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
587 /* cgroup core interface files for the legacy hierarchies */
588 struct cftype cgroup1_base_files[] = {
590 .name = "cgroup.procs",
591 .seq_start = cgroup_pidlist_start,
592 .seq_next = cgroup_pidlist_next,
593 .seq_stop = cgroup_pidlist_stop,
594 .seq_show = cgroup_pidlist_show,
595 .private = CGROUP_FILE_PROCS,
596 .write = cgroup_procs_write,
599 .name = "cgroup.clone_children",
600 .read_u64 = cgroup_clone_children_read,
601 .write_u64 = cgroup_clone_children_write,
604 .name = "cgroup.sane_behavior",
605 .flags = CFTYPE_ONLY_ON_ROOT,
606 .seq_show = cgroup_sane_behavior_show,
610 .seq_start = cgroup_pidlist_start,
611 .seq_next = cgroup_pidlist_next,
612 .seq_stop = cgroup_pidlist_stop,
613 .seq_show = cgroup_pidlist_show,
614 .private = CGROUP_FILE_TASKS,
615 .write = cgroup_tasks_write,
618 .name = "notify_on_release",
619 .read_u64 = cgroup_read_notify_on_release,
620 .write_u64 = cgroup_write_notify_on_release,
623 .name = "release_agent",
624 .flags = CFTYPE_ONLY_ON_ROOT,
625 .seq_show = cgroup_release_agent_show,
626 .write = cgroup_release_agent_write,
627 .max_write_len = PATH_MAX - 1,
632 /* Display information about each subsystem and each hierarchy */
633 static int proc_cgroupstats_show(struct seq_file *m, void *v)
635 struct cgroup_subsys *ss;
638 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
640 * ideally we don't want subsystems moving around while we do this.
641 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
642 * subsys/hierarchy state.
644 mutex_lock(&cgroup_mutex);
646 for_each_subsys(ss, i)
647 seq_printf(m, "%s\t%d\t%d\t%d\n",
648 ss->legacy_name, ss->root->hierarchy_id,
649 atomic_read(&ss->root->nr_cgrps),
650 cgroup_ssid_enabled(i));
652 mutex_unlock(&cgroup_mutex);
656 static int cgroupstats_open(struct inode *inode, struct file *file)
658 return single_open(file, proc_cgroupstats_show, NULL);
661 const struct file_operations proc_cgroupstats_operations = {
662 .open = cgroupstats_open,
665 .release = single_release,
669 * cgroupstats_build - build and fill cgroupstats
670 * @stats: cgroupstats to fill information into
671 * @dentry: A dentry entry belonging to the cgroup for which stats have
674 * Build and fill cgroupstats so that taskstats can export it to user
677 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
679 struct kernfs_node *kn = kernfs_node_from_dentry(dentry);
681 struct css_task_iter it;
682 struct task_struct *tsk;
684 /* it should be kernfs_node belonging to cgroupfs and is a directory */
685 if (dentry->d_sb->s_type != &cgroup_fs_type || !kn ||
686 kernfs_type(kn) != KERNFS_DIR)
689 mutex_lock(&cgroup_mutex);
692 * We aren't being called from kernfs and there's no guarantee on
693 * @kn->priv's validity. For this and css_tryget_online_from_dir(),
694 * @kn->priv is RCU safe. Let's do the RCU dancing.
697 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv);
698 if (!cgrp || cgroup_is_dead(cgrp)) {
700 mutex_unlock(&cgroup_mutex);
705 css_task_iter_start(&cgrp->self, &it);
706 while ((tsk = css_task_iter_next(&it))) {
707 switch (tsk->state) {
711 case TASK_INTERRUPTIBLE:
712 stats->nr_sleeping++;
714 case TASK_UNINTERRUPTIBLE:
715 stats->nr_uninterruptible++;
721 if (delayacct_is_task_waiting_on_io(tsk))
726 css_task_iter_end(&it);
728 mutex_unlock(&cgroup_mutex);
732 void cgroup1_check_for_release(struct cgroup *cgrp)
734 if (notify_on_release(cgrp) && !cgroup_is_populated(cgrp) &&
735 !css_has_online_children(&cgrp->self) && !cgroup_is_dead(cgrp))
736 schedule_work(&cgrp->release_agent_work);
740 * Notify userspace when a cgroup is released, by running the
741 * configured release agent with the name of the cgroup (path
742 * relative to the root of cgroup file system) as the argument.
744 * Most likely, this user command will try to rmdir this cgroup.
746 * This races with the possibility that some other task will be
747 * attached to this cgroup before it is removed, or that some other
748 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
749 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
750 * unused, and this cgroup will be reprieved from its death sentence,
751 * to continue to serve a useful existence. Next time it's released,
752 * we will get notified again, if it still has 'notify_on_release' set.
754 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
755 * means only wait until the task is successfully execve()'d. The
756 * separate release agent task is forked by call_usermodehelper(),
757 * then control in this thread returns here, without waiting for the
758 * release agent task. We don't bother to wait because the caller of
759 * this routine has no use for the exit status of the release agent
760 * task, so no sense holding our caller up for that.
762 void cgroup1_release_agent(struct work_struct *work)
764 struct cgroup *cgrp =
765 container_of(work, struct cgroup, release_agent_work);
766 char *pathbuf = NULL, *agentbuf = NULL;
767 char *argv[3], *envp[3];
770 mutex_lock(&cgroup_mutex);
772 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
773 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
774 if (!pathbuf || !agentbuf)
777 spin_lock_irq(&css_set_lock);
778 ret = cgroup_path_ns_locked(cgrp, pathbuf, PATH_MAX, &init_cgroup_ns);
779 spin_unlock_irq(&css_set_lock);
780 if (ret < 0 || ret >= PATH_MAX)
787 /* minimal command environment */
789 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
792 mutex_unlock(&cgroup_mutex);
793 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
796 mutex_unlock(&cgroup_mutex);
803 * cgroup_rename - Only allow simple rename of directories in place.
805 static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent,
806 const char *new_name_str)
808 struct cgroup *cgrp = kn->priv;
811 if (kernfs_type(kn) != KERNFS_DIR)
813 if (kn->parent != new_parent)
817 * We're gonna grab cgroup_mutex which nests outside kernfs
818 * active_ref. kernfs_rename() doesn't require active_ref
819 * protection. Break them before grabbing cgroup_mutex.
821 kernfs_break_active_protection(new_parent);
822 kernfs_break_active_protection(kn);
824 mutex_lock(&cgroup_mutex);
826 ret = kernfs_rename(kn, new_parent, new_name_str);
828 trace_cgroup_rename(cgrp);
830 mutex_unlock(&cgroup_mutex);
832 kernfs_unbreak_active_protection(kn);
833 kernfs_unbreak_active_protection(new_parent);
837 static int cgroup1_show_options(struct seq_file *seq, struct kernfs_root *kf_root)
839 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
840 struct cgroup_subsys *ss;
843 for_each_subsys(ss, ssid)
844 if (root->subsys_mask & (1 << ssid))
845 seq_show_option(seq, ss->legacy_name, NULL);
846 if (root->flags & CGRP_ROOT_NOPREFIX)
847 seq_puts(seq, ",noprefix");
848 if (root->flags & CGRP_ROOT_XATTR)
849 seq_puts(seq, ",xattr");
851 spin_lock(&release_agent_path_lock);
852 if (strlen(root->release_agent_path))
853 seq_show_option(seq, "release_agent",
854 root->release_agent_path);
855 spin_unlock(&release_agent_path_lock);
857 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags))
858 seq_puts(seq, ",clone_children");
859 if (strlen(root->name))
860 seq_show_option(seq, "name", root->name);
864 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
866 char *token, *o = data;
867 bool all_ss = false, one_ss = false;
869 struct cgroup_subsys *ss;
873 #ifdef CONFIG_CPUSETS
874 mask = ~((u16)1 << cpuset_cgrp_id);
877 memset(opts, 0, sizeof(*opts));
879 while ((token = strsep(&o, ",")) != NULL) {
884 if (!strcmp(token, "none")) {
885 /* Explicitly have no subsystems */
889 if (!strcmp(token, "all")) {
890 /* Mutually exclusive option 'all' + subsystem name */
896 if (!strcmp(token, "noprefix")) {
897 opts->flags |= CGRP_ROOT_NOPREFIX;
900 if (!strcmp(token, "clone_children")) {
901 opts->cpuset_clone_children = true;
904 if (!strcmp(token, "xattr")) {
905 opts->flags |= CGRP_ROOT_XATTR;
908 if (!strncmp(token, "release_agent=", 14)) {
909 /* Specifying two release agents is forbidden */
910 if (opts->release_agent)
912 opts->release_agent =
913 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
914 if (!opts->release_agent)
918 if (!strncmp(token, "name=", 5)) {
919 const char *name = token + 5;
920 /* Can't specify an empty name */
923 /* Must match [\w.-]+ */
924 for (i = 0; i < strlen(name); i++) {
928 if ((c == '.') || (c == '-') || (c == '_'))
932 /* Specifying two names is forbidden */
935 opts->name = kstrndup(name,
936 MAX_CGROUP_ROOT_NAMELEN - 1,
944 for_each_subsys(ss, i) {
945 if (strcmp(token, ss->legacy_name))
947 if (!cgroup_ssid_enabled(i))
949 if (cgroup1_ssid_disabled(i))
952 /* Mutually exclusive option 'all' + subsystem name */
955 opts->subsys_mask |= (1 << i);
960 if (i == CGROUP_SUBSYS_COUNT)
965 * If the 'all' option was specified select all the subsystems,
966 * otherwise if 'none', 'name=' and a subsystem name options were
967 * not specified, let's default to 'all'
969 if (all_ss || (!one_ss && !opts->none && !opts->name))
970 for_each_subsys(ss, i)
971 if (cgroup_ssid_enabled(i) && !cgroup1_ssid_disabled(i))
972 opts->subsys_mask |= (1 << i);
975 * We either have to specify by name or by subsystems. (So all
976 * empty hierarchies must have a name).
978 if (!opts->subsys_mask && !opts->name)
982 * Option noprefix was introduced just for backward compatibility
983 * with the old cpuset, so we allow noprefix only if mounting just
984 * the cpuset subsystem.
986 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
989 /* Can't specify "none" and some subsystems */
990 if (opts->subsys_mask && opts->none)
996 static int cgroup1_remount(struct kernfs_root *kf_root, int *flags, char *data)
999 struct cgroup_root *root = cgroup_root_from_kf(kf_root);
1000 struct cgroup_sb_opts opts;
1001 u16 added_mask, removed_mask;
1003 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1005 /* See what subsystems are wanted */
1006 ret = parse_cgroupfs_options(data, &opts);
1010 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
1011 pr_warn("option changes via remount are deprecated (pid=%d comm=%s)\n",
1012 task_tgid_nr(current), current->comm);
1014 added_mask = opts.subsys_mask & ~root->subsys_mask;
1015 removed_mask = root->subsys_mask & ~opts.subsys_mask;
1017 /* Don't allow flags or name to change at remount */
1018 if ((opts.flags ^ root->flags) ||
1019 (opts.name && strcmp(opts.name, root->name))) {
1020 pr_err("option or name mismatch, new: 0x%x \"%s\", old: 0x%x \"%s\"\n",
1021 opts.flags, opts.name ?: "", root->flags, root->name);
1026 /* remounting is not allowed for populated hierarchies */
1027 if (!list_empty(&root->cgrp.self.children)) {
1032 ret = rebind_subsystems(root, added_mask);
1036 WARN_ON(rebind_subsystems(&cgrp_dfl_root, removed_mask));
1038 if (opts.release_agent) {
1039 spin_lock(&release_agent_path_lock);
1040 strcpy(root->release_agent_path, opts.release_agent);
1041 spin_unlock(&release_agent_path_lock);
1044 trace_cgroup_remount(root);
1047 kfree(opts.release_agent);
1049 mutex_unlock(&cgroup_mutex);
1053 struct kernfs_syscall_ops cgroup1_kf_syscall_ops = {
1054 .rename = cgroup1_rename,
1055 .show_options = cgroup1_show_options,
1056 .remount_fs = cgroup1_remount,
1057 .mkdir = cgroup_mkdir,
1058 .rmdir = cgroup_rmdir,
1059 .show_path = cgroup_show_path,
1062 struct dentry *cgroup1_mount(struct file_system_type *fs_type, int flags,
1063 void *data, unsigned long magic,
1064 struct cgroup_namespace *ns)
1066 struct super_block *pinned_sb = NULL;
1067 struct cgroup_sb_opts opts;
1068 struct cgroup_root *root;
1069 struct cgroup_subsys *ss;
1070 struct dentry *dentry;
1073 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp);
1075 /* First find the desired set of subsystems */
1076 ret = parse_cgroupfs_options(data, &opts);
1081 * Destruction of cgroup root is asynchronous, so subsystems may
1082 * still be dying after the previous unmount. Let's drain the
1083 * dying subsystems. We just need to ensure that the ones
1084 * unmounted previously finish dying and don't care about new ones
1085 * starting. Testing ref liveliness is good enough.
1087 for_each_subsys(ss, i) {
1088 if (!(opts.subsys_mask & (1 << i)) ||
1089 ss->root == &cgrp_dfl_root)
1092 if (!percpu_ref_tryget_live(&ss->root->cgrp.self.refcnt)) {
1093 mutex_unlock(&cgroup_mutex);
1095 ret = restart_syscall();
1098 cgroup_put(&ss->root->cgrp);
1101 for_each_root(root) {
1102 bool name_match = false;
1104 if (root == &cgrp_dfl_root)
1108 * If we asked for a name then it must match. Also, if
1109 * name matches but sybsys_mask doesn't, we should fail.
1110 * Remember whether name matched.
1113 if (strcmp(opts.name, root->name))
1119 * If we asked for subsystems (or explicitly for no
1120 * subsystems) then they must match.
1122 if ((opts.subsys_mask || opts.none) &&
1123 (opts.subsys_mask != root->subsys_mask)) {
1130 if (root->flags ^ opts.flags)
1131 pr_warn("new mount options do not match the existing superblock, will be ignored\n");
1134 * We want to reuse @root whose lifetime is governed by its
1135 * ->cgrp. Let's check whether @root is alive and keep it
1136 * that way. As cgroup_kill_sb() can happen anytime, we
1137 * want to block it by pinning the sb so that @root doesn't
1138 * get killed before mount is complete.
1140 * With the sb pinned, tryget_live can reliably indicate
1141 * whether @root can be reused. If it's being killed,
1142 * drain it. We can use wait_queue for the wait but this
1143 * path is super cold. Let's just sleep a bit and retry.
1145 pinned_sb = kernfs_pin_sb(root->kf_root, NULL);
1146 if (IS_ERR(pinned_sb) ||
1147 !percpu_ref_tryget_live(&root->cgrp.self.refcnt)) {
1148 mutex_unlock(&cgroup_mutex);
1149 if (!IS_ERR_OR_NULL(pinned_sb))
1150 deactivate_super(pinned_sb);
1152 ret = restart_syscall();
1161 * No such thing, create a new one. name= matching without subsys
1162 * specification is allowed for already existing hierarchies but we
1163 * can't create new one without subsys specification.
1165 if (!opts.subsys_mask && !opts.none) {
1170 /* Hierarchies may only be created in the initial cgroup namespace. */
1171 if (ns != &init_cgroup_ns) {
1176 root = kzalloc(sizeof(*root), GFP_KERNEL);
1182 init_cgroup_root(root, &opts);
1184 ret = cgroup_setup_root(root, opts.subsys_mask);
1186 cgroup_free_root(root);
1189 mutex_unlock(&cgroup_mutex);
1191 kfree(opts.release_agent);
1195 return ERR_PTR(ret);
1197 dentry = cgroup_do_mount(&cgroup_fs_type, flags, root,
1198 CGROUP_SUPER_MAGIC, ns);
1201 * If @pinned_sb, we're reusing an existing root and holding an
1202 * extra ref on its sb. Mount is complete. Put the extra ref.
1205 deactivate_super(pinned_sb);
1210 static int __init cgroup1_wq_init(void)
1213 * Used to destroy pidlists and separate to serve as flush domain.
1214 * Cap @max_active to 1 too.
1216 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
1218 BUG_ON(!cgroup_pidlist_destroy_wq);
1221 core_initcall(cgroup1_wq_init);
1223 static int __init cgroup_no_v1(char *str)
1225 struct cgroup_subsys *ss;
1229 while ((token = strsep(&str, ",")) != NULL) {
1233 if (!strcmp(token, "all")) {
1234 cgroup_no_v1_mask = U16_MAX;
1238 for_each_subsys(ss, i) {
1239 if (strcmp(token, ss->name) &&
1240 strcmp(token, ss->legacy_name))
1243 cgroup_no_v1_mask |= 1 << i;
1248 __setup("cgroup_no_v1=", cgroup_no_v1);
1251 #ifdef CONFIG_CGROUP_DEBUG
1252 static struct cgroup_subsys_state *
1253 debug_css_alloc(struct cgroup_subsys_state *parent_css)
1255 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
1258 return ERR_PTR(-ENOMEM);
1263 static void debug_css_free(struct cgroup_subsys_state *css)
1268 static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
1271 return cgroup_task_count(css->cgroup);
1274 static u64 current_css_set_read(struct cgroup_subsys_state *css,
1277 return (u64)(unsigned long)current->cgroups;
1280 static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
1286 count = atomic_read(&task_css_set(current)->refcount);
1291 static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
1293 struct cgrp_cset_link *link;
1294 struct css_set *cset;
1297 name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
1301 spin_lock_irq(&css_set_lock);
1303 cset = rcu_dereference(current->cgroups);
1304 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
1305 struct cgroup *c = link->cgrp;
1307 cgroup_name(c, name_buf, NAME_MAX + 1);
1308 seq_printf(seq, "Root %d group %s\n",
1309 c->root->hierarchy_id, name_buf);
1312 spin_unlock_irq(&css_set_lock);
1317 #define MAX_TASKS_SHOWN_PER_CSS 25
1318 static int cgroup_css_links_read(struct seq_file *seq, void *v)
1320 struct cgroup_subsys_state *css = seq_css(seq);
1321 struct cgrp_cset_link *link;
1323 spin_lock_irq(&css_set_lock);
1324 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
1325 struct css_set *cset = link->cset;
1326 struct task_struct *task;
1329 seq_printf(seq, "css_set %p\n", cset);
1331 list_for_each_entry(task, &cset->tasks, cg_list) {
1332 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
1334 seq_printf(seq, " task %d\n", task_pid_vnr(task));
1337 list_for_each_entry(task, &cset->mg_tasks, cg_list) {
1338 if (count++ > MAX_TASKS_SHOWN_PER_CSS)
1340 seq_printf(seq, " task %d\n", task_pid_vnr(task));
1344 seq_puts(seq, " ...\n");
1346 spin_unlock_irq(&css_set_lock);
1350 static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
1352 return (!cgroup_is_populated(css->cgroup) &&
1353 !css_has_online_children(&css->cgroup->self));
1356 static struct cftype debug_files[] = {
1358 .name = "taskcount",
1359 .read_u64 = debug_taskcount_read,
1363 .name = "current_css_set",
1364 .read_u64 = current_css_set_read,
1368 .name = "current_css_set_refcount",
1369 .read_u64 = current_css_set_refcount_read,
1373 .name = "current_css_set_cg_links",
1374 .seq_show = current_css_set_cg_links_read,
1378 .name = "cgroup_css_links",
1379 .seq_show = cgroup_css_links_read,
1383 .name = "releasable",
1384 .read_u64 = releasable_read,
1390 struct cgroup_subsys debug_cgrp_subsys = {
1391 .css_alloc = debug_css_alloc,
1392 .css_free = debug_css_free,
1393 .legacy_cftypes = debug_files,
1395 #endif /* CONFIG_CGROUP_DEBUG */