hpsa: fix bad -ENOMEM return value in hpsa_big_passthru_ioctl
[linux/fpc-iii.git] / kernel / cgroup.c
blob1c204fdb85d8dd8e874af091f5684e3ded34c567
1 /*
2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
33 #include <linux/init_task.h>
34 #include <linux/kernel.h>
35 #include <linux/list.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/backing-dev.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/magic.h>
47 #include <linux/spinlock.h>
48 #include <linux/string.h>
49 #include <linux/sort.h>
50 #include <linux/kmod.h>
51 #include <linux/module.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/namei.h>
56 #include <linux/pid_namespace.h>
57 #include <linux/idr.h>
58 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59 #include <linux/eventfd.h>
60 #include <linux/poll.h>
61 #include <linux/flex_array.h> /* used in cgroup_attach_task */
62 #include <linux/kthread.h>
63 #include <linux/file.h>
65 #include <linux/atomic.h>
68 * cgroup_mutex is the master lock. Any modification to cgroup or its
69 * hierarchy must be performed while holding it.
71 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
72 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
73 * release_agent_path and so on. Modifying requires both cgroup_mutex and
74 * cgroup_root_mutex. Readers can acquire either of the two. This is to
75 * break the following locking order cycle.
77 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
78 * B. namespace_sem -> cgroup_mutex
80 * B happens only through cgroup_show_options() and using cgroup_root_mutex
81 * breaks it.
83 #ifdef CONFIG_PROVE_RCU
84 DEFINE_MUTEX(cgroup_mutex);
85 EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
86 #else
87 static DEFINE_MUTEX(cgroup_mutex);
88 #endif
90 static DEFINE_MUTEX(cgroup_root_mutex);
93 * cgroup destruction makes heavy use of work items and there can be a lot
94 * of concurrent destructions. Use a separate workqueue so that cgroup
95 * destruction work items don't end up filling up max_active of system_wq
96 * which may lead to deadlock.
98 static struct workqueue_struct *cgroup_destroy_wq;
101 * Generate an array of cgroup subsystem pointers. At boot time, this is
102 * populated with the built in subsystems, and modular subsystems are
103 * registered after that. The mutable section of this array is protected by
104 * cgroup_mutex.
106 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
107 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
108 static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
109 #include <linux/cgroup_subsys.h>
113 * The dummy hierarchy, reserved for the subsystems that are otherwise
114 * unattached - it never has more than a single cgroup, and all tasks are
115 * part of that cgroup.
117 static struct cgroupfs_root cgroup_dummy_root;
119 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
120 static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
123 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
125 struct cfent {
126 struct list_head node;
127 struct dentry *dentry;
128 struct cftype *type;
129 struct cgroup_subsys_state *css;
131 /* file xattrs */
132 struct simple_xattrs xattrs;
136 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
137 * cgroup_subsys->use_id != 0.
139 #define CSS_ID_MAX (65535)
140 struct css_id {
142 * The css to which this ID points. This pointer is set to valid value
143 * after cgroup is populated. If cgroup is removed, this will be NULL.
144 * This pointer is expected to be RCU-safe because destroy()
145 * is called after synchronize_rcu(). But for safe use, css_tryget()
146 * should be used for avoiding race.
148 struct cgroup_subsys_state __rcu *css;
150 * ID of this css.
152 unsigned short id;
154 * Depth in hierarchy which this ID belongs to.
156 unsigned short depth;
158 * ID is freed by RCU. (and lookup routine is RCU safe.)
160 struct rcu_head rcu_head;
162 * Hierarchy of CSS ID belongs to.
164 unsigned short stack[0]; /* Array of Length (depth+1) */
168 * cgroup_event represents events which userspace want to receive.
170 struct cgroup_event {
172 * css which the event belongs to.
174 struct cgroup_subsys_state *css;
176 * Control file which the event associated.
178 struct cftype *cft;
180 * eventfd to signal userspace about the event.
182 struct eventfd_ctx *eventfd;
184 * Each of these stored in a list by the cgroup.
186 struct list_head list;
188 * All fields below needed to unregister event when
189 * userspace closes eventfd.
191 poll_table pt;
192 wait_queue_head_t *wqh;
193 wait_queue_t wait;
194 struct work_struct remove;
197 /* The list of hierarchy roots */
199 static LIST_HEAD(cgroup_roots);
200 static int cgroup_root_count;
203 * Hierarchy ID allocation and mapping. It follows the same exclusion
204 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
205 * writes, either for reads.
207 static DEFINE_IDR(cgroup_hierarchy_idr);
209 static struct cgroup_name root_cgroup_name = { .name = "/" };
212 * Assign a monotonically increasing serial number to cgroups. It
213 * guarantees cgroups with bigger numbers are newer than those with smaller
214 * numbers. Also, as cgroups are always appended to the parent's
215 * ->children list, it guarantees that sibling cgroups are always sorted in
216 * the ascending serial number order on the list. Protected by
217 * cgroup_mutex.
219 static u64 cgroup_serial_nr_next = 1;
221 /* This flag indicates whether tasks in the fork and exit paths should
222 * check for fork/exit handlers to call. This avoids us having to do
223 * extra work in the fork/exit path if none of the subsystems need to
224 * be called.
226 static int need_forkexit_callback __read_mostly;
228 static struct cftype cgroup_base_files[];
230 static void cgroup_destroy_css_killed(struct cgroup *cgrp);
231 static int cgroup_destroy_locked(struct cgroup *cgrp);
232 static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
233 bool is_add);
234 static int cgroup_file_release(struct inode *inode, struct file *file);
237 * cgroup_css - obtain a cgroup's css for the specified subsystem
238 * @cgrp: the cgroup of interest
239 * @ss: the subsystem of interest (%NULL returns the dummy_css)
241 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
242 * function must be called either under cgroup_mutex or rcu_read_lock() and
243 * the caller is responsible for pinning the returned css if it wants to
244 * keep accessing it outside the said locks. This function may return
245 * %NULL if @cgrp doesn't have @subsys_id enabled.
247 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
248 struct cgroup_subsys *ss)
250 if (ss)
251 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
252 lockdep_is_held(&cgroup_mutex));
253 else
254 return &cgrp->dummy_css;
257 /* convenient tests for these bits */
258 static inline bool cgroup_is_dead(const struct cgroup *cgrp)
260 return test_bit(CGRP_DEAD, &cgrp->flags);
264 * cgroup_is_descendant - test ancestry
265 * @cgrp: the cgroup to be tested
266 * @ancestor: possible ancestor of @cgrp
268 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
269 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
270 * and @ancestor are accessible.
272 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
274 while (cgrp) {
275 if (cgrp == ancestor)
276 return true;
277 cgrp = cgrp->parent;
279 return false;
281 EXPORT_SYMBOL_GPL(cgroup_is_descendant);
283 static int cgroup_is_releasable(const struct cgroup *cgrp)
285 const int bits =
286 (1 << CGRP_RELEASABLE) |
287 (1 << CGRP_NOTIFY_ON_RELEASE);
288 return (cgrp->flags & bits) == bits;
291 static int notify_on_release(const struct cgroup *cgrp)
293 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
297 * for_each_subsys - iterate all loaded cgroup subsystems
298 * @ss: the iteration cursor
299 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
301 * Should be called under cgroup_mutex.
303 #define for_each_subsys(ss, i) \
304 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
305 if (({ lockdep_assert_held(&cgroup_mutex); \
306 !((ss) = cgroup_subsys[i]); })) { } \
307 else
310 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
311 * @ss: the iteration cursor
312 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
314 * Bulit-in subsystems are always present and iteration itself doesn't
315 * require any synchronization.
317 #define for_each_builtin_subsys(ss, i) \
318 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
319 (((ss) = cgroup_subsys[i]) || true); (i)++)
321 /* iterate each subsystem attached to a hierarchy */
322 #define for_each_root_subsys(root, ss) \
323 list_for_each_entry((ss), &(root)->subsys_list, sibling)
325 /* iterate across the active hierarchies */
326 #define for_each_active_root(root) \
327 list_for_each_entry((root), &cgroup_roots, root_list)
329 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
331 return dentry->d_fsdata;
334 static inline struct cfent *__d_cfe(struct dentry *dentry)
336 return dentry->d_fsdata;
339 static inline struct cftype *__d_cft(struct dentry *dentry)
341 return __d_cfe(dentry)->type;
345 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
346 * @cgrp: the cgroup to be checked for liveness
348 * On success, returns true; the mutex should be later unlocked. On
349 * failure returns false with no lock held.
351 static bool cgroup_lock_live_group(struct cgroup *cgrp)
353 mutex_lock(&cgroup_mutex);
354 if (cgroup_is_dead(cgrp)) {
355 mutex_unlock(&cgroup_mutex);
356 return false;
358 return true;
361 /* the list of cgroups eligible for automatic release. Protected by
362 * release_list_lock */
363 static LIST_HEAD(release_list);
364 static DEFINE_RAW_SPINLOCK(release_list_lock);
365 static void cgroup_release_agent(struct work_struct *work);
366 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
367 static void check_for_release(struct cgroup *cgrp);
370 * A cgroup can be associated with multiple css_sets as different tasks may
371 * belong to different cgroups on different hierarchies. In the other
372 * direction, a css_set is naturally associated with multiple cgroups.
373 * This M:N relationship is represented by the following link structure
374 * which exists for each association and allows traversing the associations
375 * from both sides.
377 struct cgrp_cset_link {
378 /* the cgroup and css_set this link associates */
379 struct cgroup *cgrp;
380 struct css_set *cset;
382 /* list of cgrp_cset_links anchored at cgrp->cset_links */
383 struct list_head cset_link;
385 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
386 struct list_head cgrp_link;
389 /* The default css_set - used by init and its children prior to any
390 * hierarchies being mounted. It contains a pointer to the root state
391 * for each subsystem. Also used to anchor the list of css_sets. Not
392 * reference-counted, to improve performance when child cgroups
393 * haven't been created.
396 static struct css_set init_css_set;
397 static struct cgrp_cset_link init_cgrp_cset_link;
399 static int cgroup_init_idr(struct cgroup_subsys *ss,
400 struct cgroup_subsys_state *css);
403 * css_set_lock protects the list of css_set objects, and the chain of
404 * tasks off each css_set. Nests outside task->alloc_lock due to
405 * css_task_iter_start().
407 static DEFINE_RWLOCK(css_set_lock);
408 static int css_set_count;
411 * hash table for cgroup groups. This improves the performance to find
412 * an existing css_set. This hash doesn't (currently) take into
413 * account cgroups in empty hierarchies.
415 #define CSS_SET_HASH_BITS 7
416 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
418 static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
420 unsigned long key = 0UL;
421 struct cgroup_subsys *ss;
422 int i;
424 for_each_subsys(ss, i)
425 key += (unsigned long)css[i];
426 key = (key >> 16) ^ key;
428 return key;
432 * We don't maintain the lists running through each css_set to its task
433 * until after the first call to css_task_iter_start(). This reduces the
434 * fork()/exit() overhead for people who have cgroups compiled into their
435 * kernel but not actually in use.
437 static int use_task_css_set_links __read_mostly;
439 static void __put_css_set(struct css_set *cset, int taskexit)
441 struct cgrp_cset_link *link, *tmp_link;
444 * Ensure that the refcount doesn't hit zero while any readers
445 * can see it. Similar to atomic_dec_and_lock(), but for an
446 * rwlock
448 if (atomic_add_unless(&cset->refcount, -1, 1))
449 return;
450 write_lock(&css_set_lock);
451 if (!atomic_dec_and_test(&cset->refcount)) {
452 write_unlock(&css_set_lock);
453 return;
456 /* This css_set is dead. unlink it and release cgroup refcounts */
457 hash_del(&cset->hlist);
458 css_set_count--;
460 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
461 struct cgroup *cgrp = link->cgrp;
463 list_del(&link->cset_link);
464 list_del(&link->cgrp_link);
466 /* @cgrp can't go away while we're holding css_set_lock */
467 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
468 if (taskexit)
469 set_bit(CGRP_RELEASABLE, &cgrp->flags);
470 check_for_release(cgrp);
473 kfree(link);
476 write_unlock(&css_set_lock);
477 kfree_rcu(cset, rcu_head);
481 * refcounted get/put for css_set objects
483 static inline void get_css_set(struct css_set *cset)
485 atomic_inc(&cset->refcount);
488 static inline void put_css_set(struct css_set *cset)
490 __put_css_set(cset, 0);
493 static inline void put_css_set_taskexit(struct css_set *cset)
495 __put_css_set(cset, 1);
499 * compare_css_sets - helper function for find_existing_css_set().
500 * @cset: candidate css_set being tested
501 * @old_cset: existing css_set for a task
502 * @new_cgrp: cgroup that's being entered by the task
503 * @template: desired set of css pointers in css_set (pre-calculated)
505 * Returns true if "cset" matches "old_cset" except for the hierarchy
506 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
508 static bool compare_css_sets(struct css_set *cset,
509 struct css_set *old_cset,
510 struct cgroup *new_cgrp,
511 struct cgroup_subsys_state *template[])
513 struct list_head *l1, *l2;
515 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
516 /* Not all subsystems matched */
517 return false;
521 * Compare cgroup pointers in order to distinguish between
522 * different cgroups in heirarchies with no subsystems. We
523 * could get by with just this check alone (and skip the
524 * memcmp above) but on most setups the memcmp check will
525 * avoid the need for this more expensive check on almost all
526 * candidates.
529 l1 = &cset->cgrp_links;
530 l2 = &old_cset->cgrp_links;
531 while (1) {
532 struct cgrp_cset_link *link1, *link2;
533 struct cgroup *cgrp1, *cgrp2;
535 l1 = l1->next;
536 l2 = l2->next;
537 /* See if we reached the end - both lists are equal length. */
538 if (l1 == &cset->cgrp_links) {
539 BUG_ON(l2 != &old_cset->cgrp_links);
540 break;
541 } else {
542 BUG_ON(l2 == &old_cset->cgrp_links);
544 /* Locate the cgroups associated with these links. */
545 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
546 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
547 cgrp1 = link1->cgrp;
548 cgrp2 = link2->cgrp;
549 /* Hierarchies should be linked in the same order. */
550 BUG_ON(cgrp1->root != cgrp2->root);
553 * If this hierarchy is the hierarchy of the cgroup
554 * that's changing, then we need to check that this
555 * css_set points to the new cgroup; if it's any other
556 * hierarchy, then this css_set should point to the
557 * same cgroup as the old css_set.
559 if (cgrp1->root == new_cgrp->root) {
560 if (cgrp1 != new_cgrp)
561 return false;
562 } else {
563 if (cgrp1 != cgrp2)
564 return false;
567 return true;
571 * find_existing_css_set - init css array and find the matching css_set
572 * @old_cset: the css_set that we're using before the cgroup transition
573 * @cgrp: the cgroup that we're moving into
574 * @template: out param for the new set of csses, should be clear on entry
576 static struct css_set *find_existing_css_set(struct css_set *old_cset,
577 struct cgroup *cgrp,
578 struct cgroup_subsys_state *template[])
580 struct cgroupfs_root *root = cgrp->root;
581 struct cgroup_subsys *ss;
582 struct css_set *cset;
583 unsigned long key;
584 int i;
587 * Build the set of subsystem state objects that we want to see in the
588 * new css_set. while subsystems can change globally, the entries here
589 * won't change, so no need for locking.
591 for_each_subsys(ss, i) {
592 if (root->subsys_mask & (1UL << i)) {
593 /* Subsystem is in this hierarchy. So we want
594 * the subsystem state from the new
595 * cgroup */
596 template[i] = cgroup_css(cgrp, ss);
597 } else {
598 /* Subsystem is not in this hierarchy, so we
599 * don't want to change the subsystem state */
600 template[i] = old_cset->subsys[i];
604 key = css_set_hash(template);
605 hash_for_each_possible(css_set_table, cset, hlist, key) {
606 if (!compare_css_sets(cset, old_cset, cgrp, template))
607 continue;
609 /* This css_set matches what we need */
610 return cset;
613 /* No existing cgroup group matched */
614 return NULL;
617 static void free_cgrp_cset_links(struct list_head *links_to_free)
619 struct cgrp_cset_link *link, *tmp_link;
621 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
622 list_del(&link->cset_link);
623 kfree(link);
628 * allocate_cgrp_cset_links - allocate cgrp_cset_links
629 * @count: the number of links to allocate
630 * @tmp_links: list_head the allocated links are put on
632 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
633 * through ->cset_link. Returns 0 on success or -errno.
635 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
637 struct cgrp_cset_link *link;
638 int i;
640 INIT_LIST_HEAD(tmp_links);
642 for (i = 0; i < count; i++) {
643 link = kzalloc(sizeof(*link), GFP_KERNEL);
644 if (!link) {
645 free_cgrp_cset_links(tmp_links);
646 return -ENOMEM;
648 list_add(&link->cset_link, tmp_links);
650 return 0;
654 * link_css_set - a helper function to link a css_set to a cgroup
655 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
656 * @cset: the css_set to be linked
657 * @cgrp: the destination cgroup
659 static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
660 struct cgroup *cgrp)
662 struct cgrp_cset_link *link;
664 BUG_ON(list_empty(tmp_links));
665 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
666 link->cset = cset;
667 link->cgrp = cgrp;
668 list_move(&link->cset_link, &cgrp->cset_links);
670 * Always add links to the tail of the list so that the list
671 * is sorted by order of hierarchy creation
673 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
677 * find_css_set - return a new css_set with one cgroup updated
678 * @old_cset: the baseline css_set
679 * @cgrp: the cgroup to be updated
681 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
682 * substituted into the appropriate hierarchy.
684 static struct css_set *find_css_set(struct css_set *old_cset,
685 struct cgroup *cgrp)
687 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
688 struct css_set *cset;
689 struct list_head tmp_links;
690 struct cgrp_cset_link *link;
691 unsigned long key;
693 lockdep_assert_held(&cgroup_mutex);
695 /* First see if we already have a cgroup group that matches
696 * the desired set */
697 read_lock(&css_set_lock);
698 cset = find_existing_css_set(old_cset, cgrp, template);
699 if (cset)
700 get_css_set(cset);
701 read_unlock(&css_set_lock);
703 if (cset)
704 return cset;
706 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
707 if (!cset)
708 return NULL;
710 /* Allocate all the cgrp_cset_link objects that we'll need */
711 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
712 kfree(cset);
713 return NULL;
716 atomic_set(&cset->refcount, 1);
717 INIT_LIST_HEAD(&cset->cgrp_links);
718 INIT_LIST_HEAD(&cset->tasks);
719 INIT_HLIST_NODE(&cset->hlist);
721 /* Copy the set of subsystem state objects generated in
722 * find_existing_css_set() */
723 memcpy(cset->subsys, template, sizeof(cset->subsys));
725 write_lock(&css_set_lock);
726 /* Add reference counts and links from the new css_set. */
727 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
728 struct cgroup *c = link->cgrp;
730 if (c->root == cgrp->root)
731 c = cgrp;
732 link_css_set(&tmp_links, cset, c);
735 BUG_ON(!list_empty(&tmp_links));
737 css_set_count++;
739 /* Add this cgroup group to the hash table */
740 key = css_set_hash(cset->subsys);
741 hash_add(css_set_table, &cset->hlist, key);
743 write_unlock(&css_set_lock);
745 return cset;
749 * Return the cgroup for "task" from the given hierarchy. Must be
750 * called with cgroup_mutex held.
752 static struct cgroup *task_cgroup_from_root(struct task_struct *task,
753 struct cgroupfs_root *root)
755 struct css_set *cset;
756 struct cgroup *res = NULL;
758 BUG_ON(!mutex_is_locked(&cgroup_mutex));
759 read_lock(&css_set_lock);
761 * No need to lock the task - since we hold cgroup_mutex the
762 * task can't change groups, so the only thing that can happen
763 * is that it exits and its css is set back to init_css_set.
765 cset = task_css_set(task);
766 if (cset == &init_css_set) {
767 res = &root->top_cgroup;
768 } else {
769 struct cgrp_cset_link *link;
771 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
772 struct cgroup *c = link->cgrp;
774 if (c->root == root) {
775 res = c;
776 break;
780 read_unlock(&css_set_lock);
781 BUG_ON(!res);
782 return res;
786 * There is one global cgroup mutex. We also require taking
787 * task_lock() when dereferencing a task's cgroup subsys pointers.
788 * See "The task_lock() exception", at the end of this comment.
790 * A task must hold cgroup_mutex to modify cgroups.
792 * Any task can increment and decrement the count field without lock.
793 * So in general, code holding cgroup_mutex can't rely on the count
794 * field not changing. However, if the count goes to zero, then only
795 * cgroup_attach_task() can increment it again. Because a count of zero
796 * means that no tasks are currently attached, therefore there is no
797 * way a task attached to that cgroup can fork (the other way to
798 * increment the count). So code holding cgroup_mutex can safely
799 * assume that if the count is zero, it will stay zero. Similarly, if
800 * a task holds cgroup_mutex on a cgroup with zero count, it
801 * knows that the cgroup won't be removed, as cgroup_rmdir()
802 * needs that mutex.
804 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
805 * (usually) take cgroup_mutex. These are the two most performance
806 * critical pieces of code here. The exception occurs on cgroup_exit(),
807 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
808 * is taken, and if the cgroup count is zero, a usermode call made
809 * to the release agent with the name of the cgroup (path relative to
810 * the root of cgroup file system) as the argument.
812 * A cgroup can only be deleted if both its 'count' of using tasks
813 * is zero, and its list of 'children' cgroups is empty. Since all
814 * tasks in the system use _some_ cgroup, and since there is always at
815 * least one task in the system (init, pid == 1), therefore, top_cgroup
816 * always has either children cgroups and/or using tasks. So we don't
817 * need a special hack to ensure that top_cgroup cannot be deleted.
819 * The task_lock() exception
821 * The need for this exception arises from the action of
822 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
823 * another. It does so using cgroup_mutex, however there are
824 * several performance critical places that need to reference
825 * task->cgroup without the expense of grabbing a system global
826 * mutex. Therefore except as noted below, when dereferencing or, as
827 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
828 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
829 * the task_struct routinely used for such matters.
831 * P.S. One more locking exception. RCU is used to guard the
832 * update of a tasks cgroup pointer by cgroup_attach_task()
836 * A couple of forward declarations required, due to cyclic reference loop:
837 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
838 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
839 * -> cgroup_mkdir.
842 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
843 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
844 static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
845 static const struct inode_operations cgroup_dir_inode_operations;
846 static const struct file_operations proc_cgroupstats_operations;
848 static struct backing_dev_info cgroup_backing_dev_info = {
849 .name = "cgroup",
850 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
853 static int alloc_css_id(struct cgroup_subsys_state *child_css);
855 static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
857 struct inode *inode = new_inode(sb);
859 if (inode) {
860 inode->i_ino = get_next_ino();
861 inode->i_mode = mode;
862 inode->i_uid = current_fsuid();
863 inode->i_gid = current_fsgid();
864 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
865 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
867 return inode;
870 static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
872 struct cgroup_name *name;
874 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
875 if (!name)
876 return NULL;
877 strcpy(name->name, dentry->d_name.name);
878 return name;
881 static void cgroup_free_fn(struct work_struct *work)
883 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
885 mutex_lock(&cgroup_mutex);
886 cgrp->root->number_of_cgroups--;
887 mutex_unlock(&cgroup_mutex);
890 * We get a ref to the parent's dentry, and put the ref when
891 * this cgroup is being freed, so it's guaranteed that the
892 * parent won't be destroyed before its children.
894 dput(cgrp->parent->dentry);
897 * Drop the active superblock reference that we took when we
898 * created the cgroup. This will free cgrp->root, if we are
899 * holding the last reference to @sb.
901 deactivate_super(cgrp->root->sb);
904 * if we're getting rid of the cgroup, refcount should ensure
905 * that there are no pidlists left.
907 BUG_ON(!list_empty(&cgrp->pidlists));
909 simple_xattrs_free(&cgrp->xattrs);
911 kfree(rcu_dereference_raw(cgrp->name));
912 kfree(cgrp);
915 static void cgroup_free_rcu(struct rcu_head *head)
917 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
919 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
920 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
923 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
925 /* is dentry a directory ? if so, kfree() associated cgroup */
926 if (S_ISDIR(inode->i_mode)) {
927 struct cgroup *cgrp = dentry->d_fsdata;
929 BUG_ON(!(cgroup_is_dead(cgrp)));
930 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
931 } else {
932 struct cfent *cfe = __d_cfe(dentry);
933 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
935 WARN_ONCE(!list_empty(&cfe->node) &&
936 cgrp != &cgrp->root->top_cgroup,
937 "cfe still linked for %s\n", cfe->type->name);
938 simple_xattrs_free(&cfe->xattrs);
939 kfree(cfe);
941 iput(inode);
944 static int cgroup_delete(const struct dentry *d)
946 return 1;
949 static void remove_dir(struct dentry *d)
951 struct dentry *parent = dget(d->d_parent);
953 d_delete(d);
954 simple_rmdir(parent->d_inode, d);
955 dput(parent);
958 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
960 struct cfent *cfe;
962 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
963 lockdep_assert_held(&cgroup_mutex);
966 * If we're doing cleanup due to failure of cgroup_create(),
967 * the corresponding @cfe may not exist.
969 list_for_each_entry(cfe, &cgrp->files, node) {
970 struct dentry *d = cfe->dentry;
972 if (cft && cfe->type != cft)
973 continue;
975 dget(d);
976 d_delete(d);
977 simple_unlink(cgrp->dentry->d_inode, d);
978 list_del_init(&cfe->node);
979 dput(d);
981 break;
986 * cgroup_clear_dir - remove subsys files in a cgroup directory
987 * @cgrp: target cgroup
988 * @subsys_mask: mask of the subsystem ids whose files should be removed
990 static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
992 struct cgroup_subsys *ss;
993 int i;
995 for_each_subsys(ss, i) {
996 struct cftype_set *set;
998 if (!test_bit(i, &subsys_mask))
999 continue;
1000 list_for_each_entry(set, &ss->cftsets, node)
1001 cgroup_addrm_files(cgrp, set->cfts, false);
1006 * NOTE : the dentry must have been dget()'ed
1008 static void cgroup_d_remove_dir(struct dentry *dentry)
1010 struct dentry *parent;
1012 parent = dentry->d_parent;
1013 spin_lock(&parent->d_lock);
1014 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1015 list_del_init(&dentry->d_u.d_child);
1016 spin_unlock(&dentry->d_lock);
1017 spin_unlock(&parent->d_lock);
1018 remove_dir(dentry);
1022 * Call with cgroup_mutex held. Drops reference counts on modules, including
1023 * any duplicate ones that parse_cgroupfs_options took. If this function
1024 * returns an error, no reference counts are touched.
1026 static int rebind_subsystems(struct cgroupfs_root *root,
1027 unsigned long added_mask, unsigned removed_mask)
1029 struct cgroup *cgrp = &root->top_cgroup;
1030 struct cgroup_subsys *ss;
1031 unsigned long pinned = 0;
1032 int i, ret;
1034 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1035 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
1037 /* Check that any added subsystems are currently free */
1038 for_each_subsys(ss, i) {
1039 if (!(added_mask & (1 << i)))
1040 continue;
1042 /* is the subsystem mounted elsewhere? */
1043 if (ss->root != &cgroup_dummy_root) {
1044 ret = -EBUSY;
1045 goto out_put;
1048 /* pin the module */
1049 if (!try_module_get(ss->module)) {
1050 ret = -ENOENT;
1051 goto out_put;
1053 pinned |= 1 << i;
1056 /* subsys could be missing if unloaded between parsing and here */
1057 if (added_mask != pinned) {
1058 ret = -ENOENT;
1059 goto out_put;
1062 ret = cgroup_populate_dir(cgrp, added_mask);
1063 if (ret)
1064 goto out_put;
1067 * Nothing can fail from this point on. Remove files for the
1068 * removed subsystems and rebind each subsystem.
1070 cgroup_clear_dir(cgrp, removed_mask);
1072 for_each_subsys(ss, i) {
1073 unsigned long bit = 1UL << i;
1075 if (bit & added_mask) {
1076 /* We're binding this subsystem to this hierarchy */
1077 BUG_ON(cgroup_css(cgrp, ss));
1078 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1079 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
1081 rcu_assign_pointer(cgrp->subsys[i],
1082 cgroup_css(cgroup_dummy_top, ss));
1083 cgroup_css(cgrp, ss)->cgroup = cgrp;
1085 list_move(&ss->sibling, &root->subsys_list);
1086 ss->root = root;
1087 if (ss->bind)
1088 ss->bind(cgroup_css(cgrp, ss));
1090 /* refcount was already taken, and we're keeping it */
1091 root->subsys_mask |= bit;
1092 } else if (bit & removed_mask) {
1093 /* We're removing this subsystem */
1094 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1095 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
1097 if (ss->bind)
1098 ss->bind(cgroup_css(cgroup_dummy_top, ss));
1100 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
1101 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1103 cgroup_subsys[i]->root = &cgroup_dummy_root;
1104 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
1106 /* subsystem is now free - drop reference on module */
1107 module_put(ss->module);
1108 root->subsys_mask &= ~bit;
1113 * Mark @root has finished binding subsystems. @root->subsys_mask
1114 * now matches the bound subsystems.
1116 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1118 return 0;
1120 out_put:
1121 for_each_subsys(ss, i)
1122 if (pinned & (1 << i))
1123 module_put(ss->module);
1124 return ret;
1127 static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
1129 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
1130 struct cgroup_subsys *ss;
1132 mutex_lock(&cgroup_root_mutex);
1133 for_each_root_subsys(root, ss)
1134 seq_printf(seq, ",%s", ss->name);
1135 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1136 seq_puts(seq, ",sane_behavior");
1137 if (root->flags & CGRP_ROOT_NOPREFIX)
1138 seq_puts(seq, ",noprefix");
1139 if (root->flags & CGRP_ROOT_XATTR)
1140 seq_puts(seq, ",xattr");
1141 if (strlen(root->release_agent_path))
1142 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1143 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
1144 seq_puts(seq, ",clone_children");
1145 if (strlen(root->name))
1146 seq_printf(seq, ",name=%s", root->name);
1147 mutex_unlock(&cgroup_root_mutex);
1148 return 0;
1151 struct cgroup_sb_opts {
1152 unsigned long subsys_mask;
1153 unsigned long flags;
1154 char *release_agent;
1155 bool cpuset_clone_children;
1156 char *name;
1157 /* User explicitly requested empty subsystem */
1158 bool none;
1160 struct cgroupfs_root *new_root;
1165 * Convert a hierarchy specifier into a bitmask of subsystems and
1166 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1167 * array. This function takes refcounts on subsystems to be used, unless it
1168 * returns error, in which case no refcounts are taken.
1170 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1172 char *token, *o = data;
1173 bool all_ss = false, one_ss = false;
1174 unsigned long mask = (unsigned long)-1;
1175 struct cgroup_subsys *ss;
1176 int i;
1178 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1180 #ifdef CONFIG_CPUSETS
1181 mask = ~(1UL << cpuset_subsys_id);
1182 #endif
1184 memset(opts, 0, sizeof(*opts));
1186 while ((token = strsep(&o, ",")) != NULL) {
1187 if (!*token)
1188 return -EINVAL;
1189 if (!strcmp(token, "none")) {
1190 /* Explicitly have no subsystems */
1191 opts->none = true;
1192 continue;
1194 if (!strcmp(token, "all")) {
1195 /* Mutually exclusive option 'all' + subsystem name */
1196 if (one_ss)
1197 return -EINVAL;
1198 all_ss = true;
1199 continue;
1201 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1202 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1203 continue;
1205 if (!strcmp(token, "noprefix")) {
1206 opts->flags |= CGRP_ROOT_NOPREFIX;
1207 continue;
1209 if (!strcmp(token, "clone_children")) {
1210 opts->cpuset_clone_children = true;
1211 continue;
1213 if (!strcmp(token, "xattr")) {
1214 opts->flags |= CGRP_ROOT_XATTR;
1215 continue;
1217 if (!strncmp(token, "release_agent=", 14)) {
1218 /* Specifying two release agents is forbidden */
1219 if (opts->release_agent)
1220 return -EINVAL;
1221 opts->release_agent =
1222 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
1223 if (!opts->release_agent)
1224 return -ENOMEM;
1225 continue;
1227 if (!strncmp(token, "name=", 5)) {
1228 const char *name = token + 5;
1229 /* Can't specify an empty name */
1230 if (!strlen(name))
1231 return -EINVAL;
1232 /* Must match [\w.-]+ */
1233 for (i = 0; i < strlen(name); i++) {
1234 char c = name[i];
1235 if (isalnum(c))
1236 continue;
1237 if ((c == '.') || (c == '-') || (c == '_'))
1238 continue;
1239 return -EINVAL;
1241 /* Specifying two names is forbidden */
1242 if (opts->name)
1243 return -EINVAL;
1244 opts->name = kstrndup(name,
1245 MAX_CGROUP_ROOT_NAMELEN - 1,
1246 GFP_KERNEL);
1247 if (!opts->name)
1248 return -ENOMEM;
1250 continue;
1253 for_each_subsys(ss, i) {
1254 if (strcmp(token, ss->name))
1255 continue;
1256 if (ss->disabled)
1257 continue;
1259 /* Mutually exclusive option 'all' + subsystem name */
1260 if (all_ss)
1261 return -EINVAL;
1262 set_bit(i, &opts->subsys_mask);
1263 one_ss = true;
1265 break;
1267 if (i == CGROUP_SUBSYS_COUNT)
1268 return -ENOENT;
1272 * If the 'all' option was specified select all the subsystems,
1273 * otherwise if 'none', 'name=' and a subsystem name options
1274 * were not specified, let's default to 'all'
1276 if (all_ss || (!one_ss && !opts->none && !opts->name))
1277 for_each_subsys(ss, i)
1278 if (!ss->disabled)
1279 set_bit(i, &opts->subsys_mask);
1281 /* Consistency checks */
1283 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1284 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1286 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1287 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1288 return -EINVAL;
1291 if (opts->cpuset_clone_children) {
1292 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1293 return -EINVAL;
1298 * Option noprefix was introduced just for backward compatibility
1299 * with the old cpuset, so we allow noprefix only if mounting just
1300 * the cpuset subsystem.
1302 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
1303 return -EINVAL;
1306 /* Can't specify "none" and some subsystems */
1307 if (opts->subsys_mask && opts->none)
1308 return -EINVAL;
1311 * We either have to specify by name or by subsystems. (So all
1312 * empty hierarchies must have a name).
1314 if (!opts->subsys_mask && !opts->name)
1315 return -EINVAL;
1317 return 0;
1320 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1322 int ret = 0;
1323 struct cgroupfs_root *root = sb->s_fs_info;
1324 struct cgroup *cgrp = &root->top_cgroup;
1325 struct cgroup_sb_opts opts;
1326 unsigned long added_mask, removed_mask;
1328 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1329 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1330 return -EINVAL;
1333 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1334 mutex_lock(&cgroup_mutex);
1335 mutex_lock(&cgroup_root_mutex);
1337 /* See what subsystems are wanted */
1338 ret = parse_cgroupfs_options(data, &opts);
1339 if (ret)
1340 goto out_unlock;
1342 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
1343 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1344 task_tgid_nr(current), current->comm);
1346 added_mask = opts.subsys_mask & ~root->subsys_mask;
1347 removed_mask = root->subsys_mask & ~opts.subsys_mask;
1349 /* Don't allow flags or name to change at remount */
1350 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
1351 (opts.name && strcmp(opts.name, root->name))) {
1352 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1353 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1354 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
1355 ret = -EINVAL;
1356 goto out_unlock;
1359 /* remounting is not allowed for populated hierarchies */
1360 if (root->number_of_cgroups > 1) {
1361 ret = -EBUSY;
1362 goto out_unlock;
1365 ret = rebind_subsystems(root, added_mask, removed_mask);
1366 if (ret)
1367 goto out_unlock;
1369 if (opts.release_agent)
1370 strcpy(root->release_agent_path, opts.release_agent);
1371 out_unlock:
1372 kfree(opts.release_agent);
1373 kfree(opts.name);
1374 mutex_unlock(&cgroup_root_mutex);
1375 mutex_unlock(&cgroup_mutex);
1376 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1377 return ret;
1380 static const struct super_operations cgroup_ops = {
1381 .statfs = simple_statfs,
1382 .drop_inode = generic_delete_inode,
1383 .show_options = cgroup_show_options,
1384 .remount_fs = cgroup_remount,
1387 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1389 INIT_LIST_HEAD(&cgrp->sibling);
1390 INIT_LIST_HEAD(&cgrp->children);
1391 INIT_LIST_HEAD(&cgrp->files);
1392 INIT_LIST_HEAD(&cgrp->cset_links);
1393 INIT_LIST_HEAD(&cgrp->release_list);
1394 INIT_LIST_HEAD(&cgrp->pidlists);
1395 mutex_init(&cgrp->pidlist_mutex);
1396 cgrp->dummy_css.cgroup = cgrp;
1397 INIT_LIST_HEAD(&cgrp->event_list);
1398 spin_lock_init(&cgrp->event_list_lock);
1399 simple_xattrs_init(&cgrp->xattrs);
1402 static void init_cgroup_root(struct cgroupfs_root *root)
1404 struct cgroup *cgrp = &root->top_cgroup;
1406 INIT_LIST_HEAD(&root->subsys_list);
1407 INIT_LIST_HEAD(&root->root_list);
1408 root->number_of_cgroups = 1;
1409 cgrp->root = root;
1410 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
1411 init_cgroup_housekeeping(cgrp);
1412 idr_init(&root->cgroup_idr);
1415 static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
1417 int id;
1419 lockdep_assert_held(&cgroup_mutex);
1420 lockdep_assert_held(&cgroup_root_mutex);
1422 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1423 GFP_KERNEL);
1424 if (id < 0)
1425 return id;
1427 root->hierarchy_id = id;
1428 return 0;
1431 static void cgroup_exit_root_id(struct cgroupfs_root *root)
1433 lockdep_assert_held(&cgroup_mutex);
1434 lockdep_assert_held(&cgroup_root_mutex);
1436 if (root->hierarchy_id) {
1437 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1438 root->hierarchy_id = 0;
1442 static int cgroup_test_super(struct super_block *sb, void *data)
1444 struct cgroup_sb_opts *opts = data;
1445 struct cgroupfs_root *root = sb->s_fs_info;
1447 /* If we asked for a name then it must match */
1448 if (opts->name && strcmp(opts->name, root->name))
1449 return 0;
1452 * If we asked for subsystems (or explicitly for no
1453 * subsystems) then they must match
1455 if ((opts->subsys_mask || opts->none)
1456 && (opts->subsys_mask != root->subsys_mask))
1457 return 0;
1459 return 1;
1462 static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1464 struct cgroupfs_root *root;
1466 if (!opts->subsys_mask && !opts->none)
1467 return NULL;
1469 root = kzalloc(sizeof(*root), GFP_KERNEL);
1470 if (!root)
1471 return ERR_PTR(-ENOMEM);
1473 init_cgroup_root(root);
1476 * We need to set @root->subsys_mask now so that @root can be
1477 * matched by cgroup_test_super() before it finishes
1478 * initialization; otherwise, competing mounts with the same
1479 * options may try to bind the same subsystems instead of waiting
1480 * for the first one leading to unexpected mount errors.
1481 * SUBSYS_BOUND will be set once actual binding is complete.
1483 root->subsys_mask = opts->subsys_mask;
1484 root->flags = opts->flags;
1485 if (opts->release_agent)
1486 strcpy(root->release_agent_path, opts->release_agent);
1487 if (opts->name)
1488 strcpy(root->name, opts->name);
1489 if (opts->cpuset_clone_children)
1490 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
1491 return root;
1494 static void cgroup_free_root(struct cgroupfs_root *root)
1496 if (root) {
1497 /* hierarhcy ID shoulid already have been released */
1498 WARN_ON_ONCE(root->hierarchy_id);
1500 idr_destroy(&root->cgroup_idr);
1501 kfree(root);
1505 static int cgroup_set_super(struct super_block *sb, void *data)
1507 int ret;
1508 struct cgroup_sb_opts *opts = data;
1510 /* If we don't have a new root, we can't set up a new sb */
1511 if (!opts->new_root)
1512 return -EINVAL;
1514 BUG_ON(!opts->subsys_mask && !opts->none);
1516 ret = set_anon_super(sb, NULL);
1517 if (ret)
1518 return ret;
1520 sb->s_fs_info = opts->new_root;
1521 opts->new_root->sb = sb;
1523 sb->s_blocksize = PAGE_CACHE_SIZE;
1524 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1525 sb->s_magic = CGROUP_SUPER_MAGIC;
1526 sb->s_op = &cgroup_ops;
1528 return 0;
1531 static int cgroup_get_rootdir(struct super_block *sb)
1533 static const struct dentry_operations cgroup_dops = {
1534 .d_iput = cgroup_diput,
1535 .d_delete = cgroup_delete,
1538 struct inode *inode =
1539 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1541 if (!inode)
1542 return -ENOMEM;
1544 inode->i_fop = &simple_dir_operations;
1545 inode->i_op = &cgroup_dir_inode_operations;
1546 /* directories start off with i_nlink == 2 (for "." entry) */
1547 inc_nlink(inode);
1548 sb->s_root = d_make_root(inode);
1549 if (!sb->s_root)
1550 return -ENOMEM;
1551 /* for everything else we want ->d_op set */
1552 sb->s_d_op = &cgroup_dops;
1553 return 0;
1556 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1557 int flags, const char *unused_dev_name,
1558 void *data)
1560 struct cgroup_sb_opts opts;
1561 struct cgroupfs_root *root;
1562 int ret = 0;
1563 struct super_block *sb;
1564 struct cgroupfs_root *new_root;
1565 struct list_head tmp_links;
1566 struct inode *inode;
1567 const struct cred *cred;
1569 /* First find the desired set of subsystems */
1570 mutex_lock(&cgroup_mutex);
1571 ret = parse_cgroupfs_options(data, &opts);
1572 mutex_unlock(&cgroup_mutex);
1573 if (ret)
1574 goto out_err;
1577 * Allocate a new cgroup root. We may not need it if we're
1578 * reusing an existing hierarchy.
1580 new_root = cgroup_root_from_opts(&opts);
1581 if (IS_ERR(new_root)) {
1582 ret = PTR_ERR(new_root);
1583 goto out_err;
1585 opts.new_root = new_root;
1587 /* Locate an existing or new sb for this hierarchy */
1588 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
1589 if (IS_ERR(sb)) {
1590 ret = PTR_ERR(sb);
1591 cgroup_free_root(opts.new_root);
1592 goto out_err;
1595 root = sb->s_fs_info;
1596 BUG_ON(!root);
1597 if (root == opts.new_root) {
1598 /* We used the new root structure, so this is a new hierarchy */
1599 struct cgroup *root_cgrp = &root->top_cgroup;
1600 struct cgroupfs_root *existing_root;
1601 int i;
1602 struct css_set *cset;
1604 BUG_ON(sb->s_root != NULL);
1606 ret = cgroup_get_rootdir(sb);
1607 if (ret)
1608 goto drop_new_super;
1609 inode = sb->s_root->d_inode;
1611 mutex_lock(&inode->i_mutex);
1612 mutex_lock(&cgroup_mutex);
1613 mutex_lock(&cgroup_root_mutex);
1615 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1616 if (ret < 0)
1617 goto unlock_drop;
1618 root_cgrp->id = ret;
1620 /* Check for name clashes with existing mounts */
1621 ret = -EBUSY;
1622 if (strlen(root->name))
1623 for_each_active_root(existing_root)
1624 if (!strcmp(existing_root->name, root->name))
1625 goto unlock_drop;
1628 * We're accessing css_set_count without locking
1629 * css_set_lock here, but that's OK - it can only be
1630 * increased by someone holding cgroup_lock, and
1631 * that's us. The worst that can happen is that we
1632 * have some link structures left over
1634 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1635 if (ret)
1636 goto unlock_drop;
1638 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1639 ret = cgroup_init_root_id(root, 2, 0);
1640 if (ret)
1641 goto unlock_drop;
1643 sb->s_root->d_fsdata = root_cgrp;
1644 root_cgrp->dentry = sb->s_root;
1647 * We're inside get_sb() and will call lookup_one_len() to
1648 * create the root files, which doesn't work if SELinux is
1649 * in use. The following cred dancing somehow works around
1650 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1651 * populating new cgroupfs mount") for more details.
1653 cred = override_creds(&init_cred);
1655 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
1656 if (ret)
1657 goto rm_base_files;
1659 ret = rebind_subsystems(root, root->subsys_mask, 0);
1660 if (ret)
1661 goto rm_base_files;
1663 revert_creds(cred);
1666 * There must be no failure case after here, since rebinding
1667 * takes care of subsystems' refcounts, which are explicitly
1668 * dropped in the failure exit path.
1671 list_add(&root->root_list, &cgroup_roots);
1672 cgroup_root_count++;
1674 /* Link the top cgroup in this hierarchy into all
1675 * the css_set objects */
1676 write_lock(&css_set_lock);
1677 hash_for_each(css_set_table, i, cset, hlist)
1678 link_css_set(&tmp_links, cset, root_cgrp);
1679 write_unlock(&css_set_lock);
1681 free_cgrp_cset_links(&tmp_links);
1683 BUG_ON(!list_empty(&root_cgrp->children));
1684 BUG_ON(root->number_of_cgroups != 1);
1686 mutex_unlock(&cgroup_root_mutex);
1687 mutex_unlock(&cgroup_mutex);
1688 mutex_unlock(&inode->i_mutex);
1689 } else {
1691 * We re-used an existing hierarchy - the new root (if
1692 * any) is not needed
1694 cgroup_free_root(opts.new_root);
1696 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
1697 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1698 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1699 ret = -EINVAL;
1700 goto drop_new_super;
1701 } else {
1702 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1707 kfree(opts.release_agent);
1708 kfree(opts.name);
1709 return dget(sb->s_root);
1711 rm_base_files:
1712 free_cgrp_cset_links(&tmp_links);
1713 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
1714 revert_creds(cred);
1715 unlock_drop:
1716 cgroup_exit_root_id(root);
1717 mutex_unlock(&cgroup_root_mutex);
1718 mutex_unlock(&cgroup_mutex);
1719 mutex_unlock(&inode->i_mutex);
1720 drop_new_super:
1721 deactivate_locked_super(sb);
1722 out_err:
1723 kfree(opts.release_agent);
1724 kfree(opts.name);
1725 return ERR_PTR(ret);
1728 static void cgroup_kill_sb(struct super_block *sb) {
1729 struct cgroupfs_root *root = sb->s_fs_info;
1730 struct cgroup *cgrp = &root->top_cgroup;
1731 struct cgrp_cset_link *link, *tmp_link;
1732 int ret;
1734 BUG_ON(!root);
1736 BUG_ON(root->number_of_cgroups != 1);
1737 BUG_ON(!list_empty(&cgrp->children));
1739 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1740 mutex_lock(&cgroup_mutex);
1741 mutex_lock(&cgroup_root_mutex);
1743 /* Rebind all subsystems back to the default hierarchy */
1744 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1745 ret = rebind_subsystems(root, 0, root->subsys_mask);
1746 /* Shouldn't be able to fail ... */
1747 BUG_ON(ret);
1751 * Release all the links from cset_links to this hierarchy's
1752 * root cgroup
1754 write_lock(&css_set_lock);
1756 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1757 list_del(&link->cset_link);
1758 list_del(&link->cgrp_link);
1759 kfree(link);
1761 write_unlock(&css_set_lock);
1763 if (!list_empty(&root->root_list)) {
1764 list_del(&root->root_list);
1765 cgroup_root_count--;
1768 cgroup_exit_root_id(root);
1770 mutex_unlock(&cgroup_root_mutex);
1771 mutex_unlock(&cgroup_mutex);
1772 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1774 simple_xattrs_free(&cgrp->xattrs);
1776 kill_litter_super(sb);
1777 cgroup_free_root(root);
1780 static struct file_system_type cgroup_fs_type = {
1781 .name = "cgroup",
1782 .mount = cgroup_mount,
1783 .kill_sb = cgroup_kill_sb,
1786 static struct kobject *cgroup_kobj;
1789 * cgroup_path - generate the path of a cgroup
1790 * @cgrp: the cgroup in question
1791 * @buf: the buffer to write the path into
1792 * @buflen: the length of the buffer
1794 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1796 * We can't generate cgroup path using dentry->d_name, as accessing
1797 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1798 * inode's i_mutex, while on the other hand cgroup_path() can be called
1799 * with some irq-safe spinlocks held.
1801 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1803 int ret = -ENAMETOOLONG;
1804 char *start;
1806 if (!cgrp->parent) {
1807 if (strlcpy(buf, "/", buflen) >= buflen)
1808 return -ENAMETOOLONG;
1809 return 0;
1812 start = buf + buflen - 1;
1813 *start = '\0';
1815 rcu_read_lock();
1816 do {
1817 const char *name = cgroup_name(cgrp);
1818 int len;
1820 len = strlen(name);
1821 if ((start -= len) < buf)
1822 goto out;
1823 memcpy(start, name, len);
1825 if (--start < buf)
1826 goto out;
1827 *start = '/';
1829 cgrp = cgrp->parent;
1830 } while (cgrp->parent);
1831 ret = 0;
1832 memmove(buf, start, buf + buflen - start);
1833 out:
1834 rcu_read_unlock();
1835 return ret;
1837 EXPORT_SYMBOL_GPL(cgroup_path);
1840 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1841 * @task: target task
1842 * @buf: the buffer to write the path into
1843 * @buflen: the length of the buffer
1845 * Determine @task's cgroup on the first (the one with the lowest non-zero
1846 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1847 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1848 * cgroup controller callbacks.
1850 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1852 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
1854 struct cgroupfs_root *root;
1855 struct cgroup *cgrp;
1856 int hierarchy_id = 1, ret = 0;
1858 if (buflen < 2)
1859 return -ENAMETOOLONG;
1861 mutex_lock(&cgroup_mutex);
1863 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1865 if (root) {
1866 cgrp = task_cgroup_from_root(task, root);
1867 ret = cgroup_path(cgrp, buf, buflen);
1868 } else {
1869 /* if no hierarchy exists, everyone is in "/" */
1870 memcpy(buf, "/", 2);
1873 mutex_unlock(&cgroup_mutex);
1874 return ret;
1876 EXPORT_SYMBOL_GPL(task_cgroup_path);
1879 * Control Group taskset
1881 struct task_and_cgroup {
1882 struct task_struct *task;
1883 struct cgroup *cgrp;
1884 struct css_set *cset;
1887 struct cgroup_taskset {
1888 struct task_and_cgroup single;
1889 struct flex_array *tc_array;
1890 int tc_array_len;
1891 int idx;
1892 struct cgroup *cur_cgrp;
1896 * cgroup_taskset_first - reset taskset and return the first task
1897 * @tset: taskset of interest
1899 * @tset iteration is initialized and the first task is returned.
1901 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1903 if (tset->tc_array) {
1904 tset->idx = 0;
1905 return cgroup_taskset_next(tset);
1906 } else {
1907 tset->cur_cgrp = tset->single.cgrp;
1908 return tset->single.task;
1911 EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1914 * cgroup_taskset_next - iterate to the next task in taskset
1915 * @tset: taskset of interest
1917 * Return the next task in @tset. Iteration must have been initialized
1918 * with cgroup_taskset_first().
1920 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1922 struct task_and_cgroup *tc;
1924 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1925 return NULL;
1927 tc = flex_array_get(tset->tc_array, tset->idx++);
1928 tset->cur_cgrp = tc->cgrp;
1929 return tc->task;
1931 EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1934 * cgroup_taskset_cur_css - return the matching css for the current task
1935 * @tset: taskset of interest
1936 * @subsys_id: the ID of the target subsystem
1938 * Return the css for the current (last returned) task of @tset for
1939 * subsystem specified by @subsys_id. This function must be preceded by
1940 * either cgroup_taskset_first() or cgroup_taskset_next().
1942 struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1943 int subsys_id)
1945 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
1947 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
1950 * cgroup_taskset_size - return the number of tasks in taskset
1951 * @tset: taskset of interest
1953 int cgroup_taskset_size(struct cgroup_taskset *tset)
1955 return tset->tc_array ? tset->tc_array_len : 1;
1957 EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1961 * cgroup_task_migrate - move a task from one cgroup to another.
1963 * Must be called with cgroup_mutex and threadgroup locked.
1965 static void cgroup_task_migrate(struct cgroup *old_cgrp,
1966 struct task_struct *tsk,
1967 struct css_set *new_cset)
1969 struct css_set *old_cset;
1972 * We are synchronized through threadgroup_lock() against PF_EXITING
1973 * setting such that we can't race against cgroup_exit() changing the
1974 * css_set to init_css_set and dropping the old one.
1976 WARN_ON_ONCE(tsk->flags & PF_EXITING);
1977 old_cset = task_css_set(tsk);
1979 task_lock(tsk);
1980 rcu_assign_pointer(tsk->cgroups, new_cset);
1981 task_unlock(tsk);
1983 /* Update the css_set linked lists if we're using them */
1984 write_lock(&css_set_lock);
1985 if (!list_empty(&tsk->cg_list))
1986 list_move(&tsk->cg_list, &new_cset->tasks);
1987 write_unlock(&css_set_lock);
1990 * We just gained a reference on old_cset by taking it from the
1991 * task. As trading it for new_cset is protected by cgroup_mutex,
1992 * we're safe to drop it here; it will be freed under RCU.
1994 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1995 put_css_set(old_cset);
1999 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2000 * @cgrp: the cgroup to attach to
2001 * @tsk: the task or the leader of the threadgroup to be attached
2002 * @threadgroup: attach the whole threadgroup?
2004 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2005 * task_lock of @tsk or each thread in the threadgroup individually in turn.
2007 static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2008 bool threadgroup)
2010 int retval, i, group_size;
2011 struct cgroup_subsys *ss, *failed_ss = NULL;
2012 struct cgroupfs_root *root = cgrp->root;
2013 /* threadgroup list cursor and array */
2014 struct task_struct *leader = tsk;
2015 struct task_and_cgroup *tc;
2016 struct flex_array *group;
2017 struct cgroup_taskset tset = { };
2020 * step 0: in order to do expensive, possibly blocking operations for
2021 * every thread, we cannot iterate the thread group list, since it needs
2022 * rcu or tasklist locked. instead, build an array of all threads in the
2023 * group - group_rwsem prevents new threads from appearing, and if
2024 * threads exit, this will just be an over-estimate.
2026 if (threadgroup)
2027 group_size = get_nr_threads(tsk);
2028 else
2029 group_size = 1;
2030 /* flex_array supports very large thread-groups better than kmalloc. */
2031 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
2032 if (!group)
2033 return -ENOMEM;
2034 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2035 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
2036 if (retval)
2037 goto out_free_group_list;
2039 i = 0;
2041 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2042 * already PF_EXITING could be freed from underneath us unless we
2043 * take an rcu_read_lock.
2045 rcu_read_lock();
2046 do {
2047 struct task_and_cgroup ent;
2049 /* @tsk either already exited or can't exit until the end */
2050 if (tsk->flags & PF_EXITING)
2051 goto next;
2053 /* as per above, nr_threads may decrease, but not increase. */
2054 BUG_ON(i >= group_size);
2055 ent.task = tsk;
2056 ent.cgrp = task_cgroup_from_root(tsk, root);
2057 /* nothing to do if this task is already in the cgroup */
2058 if (ent.cgrp == cgrp)
2059 goto next;
2061 * saying GFP_ATOMIC has no effect here because we did prealloc
2062 * earlier, but it's good form to communicate our expectations.
2064 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
2065 BUG_ON(retval != 0);
2066 i++;
2067 next:
2068 if (!threadgroup)
2069 break;
2070 } while_each_thread(leader, tsk);
2071 rcu_read_unlock();
2072 /* remember the number of threads in the array for later. */
2073 group_size = i;
2074 tset.tc_array = group;
2075 tset.tc_array_len = group_size;
2077 /* methods shouldn't be called if no task is actually migrating */
2078 retval = 0;
2079 if (!group_size)
2080 goto out_free_group_list;
2083 * step 1: check that we can legitimately attach to the cgroup.
2085 for_each_root_subsys(root, ss) {
2086 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
2088 if (ss->can_attach) {
2089 retval = ss->can_attach(css, &tset);
2090 if (retval) {
2091 failed_ss = ss;
2092 goto out_cancel_attach;
2098 * step 2: make sure css_sets exist for all threads to be migrated.
2099 * we use find_css_set, which allocates a new one if necessary.
2101 for (i = 0; i < group_size; i++) {
2102 struct css_set *old_cset;
2104 tc = flex_array_get(group, i);
2105 old_cset = task_css_set(tc->task);
2106 tc->cset = find_css_set(old_cset, cgrp);
2107 if (!tc->cset) {
2108 retval = -ENOMEM;
2109 goto out_put_css_set_refs;
2114 * step 3: now that we're guaranteed success wrt the css_sets,
2115 * proceed to move all tasks to the new cgroup. There are no
2116 * failure cases after here, so this is the commit point.
2118 for (i = 0; i < group_size; i++) {
2119 tc = flex_array_get(group, i);
2120 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
2122 /* nothing is sensitive to fork() after this point. */
2125 * step 4: do subsystem attach callbacks.
2127 for_each_root_subsys(root, ss) {
2128 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
2130 if (ss->attach)
2131 ss->attach(css, &tset);
2135 * step 5: success! and cleanup
2137 retval = 0;
2138 out_put_css_set_refs:
2139 if (retval) {
2140 for (i = 0; i < group_size; i++) {
2141 tc = flex_array_get(group, i);
2142 if (!tc->cset)
2143 break;
2144 put_css_set(tc->cset);
2147 out_cancel_attach:
2148 if (retval) {
2149 for_each_root_subsys(root, ss) {
2150 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
2152 if (ss == failed_ss)
2153 break;
2154 if (ss->cancel_attach)
2155 ss->cancel_attach(css, &tset);
2158 out_free_group_list:
2159 flex_array_free(group);
2160 return retval;
2164 * Find the task_struct of the task to attach by vpid and pass it along to the
2165 * function to attach either it or all tasks in its threadgroup. Will lock
2166 * cgroup_mutex and threadgroup; may take task_lock of task.
2168 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
2170 struct task_struct *tsk;
2171 const struct cred *cred = current_cred(), *tcred;
2172 int ret;
2174 if (!cgroup_lock_live_group(cgrp))
2175 return -ENODEV;
2177 retry_find_task:
2178 rcu_read_lock();
2179 if (pid) {
2180 tsk = find_task_by_vpid(pid);
2181 if (!tsk) {
2182 rcu_read_unlock();
2183 ret= -ESRCH;
2184 goto out_unlock_cgroup;
2187 * even if we're attaching all tasks in the thread group, we
2188 * only need to check permissions on one of them.
2190 tcred = __task_cred(tsk);
2191 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2192 !uid_eq(cred->euid, tcred->uid) &&
2193 !uid_eq(cred->euid, tcred->suid)) {
2194 rcu_read_unlock();
2195 ret = -EACCES;
2196 goto out_unlock_cgroup;
2198 } else
2199 tsk = current;
2201 if (threadgroup)
2202 tsk = tsk->group_leader;
2205 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2206 * trapped in a cpuset, or RT worker may be born in a cgroup
2207 * with no rt_runtime allocated. Just say no.
2209 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
2210 ret = -EINVAL;
2211 rcu_read_unlock();
2212 goto out_unlock_cgroup;
2215 get_task_struct(tsk);
2216 rcu_read_unlock();
2218 threadgroup_lock(tsk);
2219 if (threadgroup) {
2220 if (!thread_group_leader(tsk)) {
2222 * a race with de_thread from another thread's exec()
2223 * may strip us of our leadership, if this happens,
2224 * there is no choice but to throw this task away and
2225 * try again; this is
2226 * "double-double-toil-and-trouble-check locking".
2228 threadgroup_unlock(tsk);
2229 put_task_struct(tsk);
2230 goto retry_find_task;
2234 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2236 threadgroup_unlock(tsk);
2238 put_task_struct(tsk);
2239 out_unlock_cgroup:
2240 mutex_unlock(&cgroup_mutex);
2241 return ret;
2245 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2246 * @from: attach to all cgroups of a given task
2247 * @tsk: the task to be attached
2249 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2251 struct cgroupfs_root *root;
2252 int retval = 0;
2254 mutex_lock(&cgroup_mutex);
2255 for_each_active_root(root) {
2256 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
2258 retval = cgroup_attach_task(from_cgrp, tsk, false);
2259 if (retval)
2260 break;
2262 mutex_unlock(&cgroup_mutex);
2264 return retval;
2266 EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2268 static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2269 struct cftype *cft, u64 pid)
2271 return attach_task_by_pid(css->cgroup, pid, false);
2274 static int cgroup_procs_write(struct cgroup_subsys_state *css,
2275 struct cftype *cft, u64 tgid)
2277 return attach_task_by_pid(css->cgroup, tgid, true);
2280 static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2281 struct cftype *cft, const char *buffer)
2283 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
2284 if (strlen(buffer) >= PATH_MAX)
2285 return -EINVAL;
2286 if (!cgroup_lock_live_group(css->cgroup))
2287 return -ENODEV;
2288 mutex_lock(&cgroup_root_mutex);
2289 strcpy(css->cgroup->root->release_agent_path, buffer);
2290 mutex_unlock(&cgroup_root_mutex);
2291 mutex_unlock(&cgroup_mutex);
2292 return 0;
2295 static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2296 struct cftype *cft, struct seq_file *seq)
2298 struct cgroup *cgrp = css->cgroup;
2300 if (!cgroup_lock_live_group(cgrp))
2301 return -ENODEV;
2302 seq_puts(seq, cgrp->root->release_agent_path);
2303 seq_putc(seq, '\n');
2304 mutex_unlock(&cgroup_mutex);
2305 return 0;
2308 static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2309 struct cftype *cft, struct seq_file *seq)
2311 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
2312 return 0;
2315 /* A buffer size big enough for numbers or short strings */
2316 #define CGROUP_LOCAL_BUFFER_SIZE 64
2318 static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2319 struct cftype *cft, struct file *file,
2320 const char __user *userbuf, size_t nbytes,
2321 loff_t *unused_ppos)
2323 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
2324 int retval = 0;
2325 char *end;
2327 if (!nbytes)
2328 return -EINVAL;
2329 if (nbytes >= sizeof(buffer))
2330 return -E2BIG;
2331 if (copy_from_user(buffer, userbuf, nbytes))
2332 return -EFAULT;
2334 buffer[nbytes] = 0; /* nul-terminate */
2335 if (cft->write_u64) {
2336 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
2337 if (*end)
2338 return -EINVAL;
2339 retval = cft->write_u64(css, cft, val);
2340 } else {
2341 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
2342 if (*end)
2343 return -EINVAL;
2344 retval = cft->write_s64(css, cft, val);
2346 if (!retval)
2347 retval = nbytes;
2348 return retval;
2351 static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2352 struct cftype *cft, struct file *file,
2353 const char __user *userbuf, size_t nbytes,
2354 loff_t *unused_ppos)
2356 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
2357 int retval = 0;
2358 size_t max_bytes = cft->max_write_len;
2359 char *buffer = local_buffer;
2361 if (!max_bytes)
2362 max_bytes = sizeof(local_buffer) - 1;
2363 if (nbytes >= max_bytes)
2364 return -E2BIG;
2365 /* Allocate a dynamic buffer if we need one */
2366 if (nbytes >= sizeof(local_buffer)) {
2367 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2368 if (buffer == NULL)
2369 return -ENOMEM;
2371 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2372 retval = -EFAULT;
2373 goto out;
2376 buffer[nbytes] = 0; /* nul-terminate */
2377 retval = cft->write_string(css, cft, strstrip(buffer));
2378 if (!retval)
2379 retval = nbytes;
2380 out:
2381 if (buffer != local_buffer)
2382 kfree(buffer);
2383 return retval;
2386 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2387 size_t nbytes, loff_t *ppos)
2389 struct cfent *cfe = __d_cfe(file->f_dentry);
2390 struct cftype *cft = __d_cft(file->f_dentry);
2391 struct cgroup_subsys_state *css = cfe->css;
2393 if (cft->write)
2394 return cft->write(css, cft, file, buf, nbytes, ppos);
2395 if (cft->write_u64 || cft->write_s64)
2396 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
2397 if (cft->write_string)
2398 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
2399 if (cft->trigger) {
2400 int ret = cft->trigger(css, (unsigned int)cft->private);
2401 return ret ? ret : nbytes;
2403 return -EINVAL;
2406 static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2407 struct cftype *cft, struct file *file,
2408 char __user *buf, size_t nbytes, loff_t *ppos)
2410 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2411 u64 val = cft->read_u64(css, cft);
2412 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2414 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2417 static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2418 struct cftype *cft, struct file *file,
2419 char __user *buf, size_t nbytes, loff_t *ppos)
2421 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2422 s64 val = cft->read_s64(css, cft);
2423 int len = sprintf(tmp, "%lld\n", (long long) val);
2425 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2428 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2429 size_t nbytes, loff_t *ppos)
2431 struct cfent *cfe = __d_cfe(file->f_dentry);
2432 struct cftype *cft = __d_cft(file->f_dentry);
2433 struct cgroup_subsys_state *css = cfe->css;
2435 if (cft->read)
2436 return cft->read(css, cft, file, buf, nbytes, ppos);
2437 if (cft->read_u64)
2438 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
2439 if (cft->read_s64)
2440 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
2441 return -EINVAL;
2445 * seqfile ops/methods for returning structured data. Currently just
2446 * supports string->u64 maps, but can be extended in future.
2449 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2451 struct seq_file *sf = cb->state;
2452 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2455 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2457 struct cfent *cfe = m->private;
2458 struct cftype *cft = cfe->type;
2459 struct cgroup_subsys_state *css = cfe->css;
2461 if (cft->read_map) {
2462 struct cgroup_map_cb cb = {
2463 .fill = cgroup_map_add,
2464 .state = m,
2466 return cft->read_map(css, cft, &cb);
2468 return cft->read_seq_string(css, cft, m);
2471 static const struct file_operations cgroup_seqfile_operations = {
2472 .read = seq_read,
2473 .write = cgroup_file_write,
2474 .llseek = seq_lseek,
2475 .release = cgroup_file_release,
2478 static int cgroup_file_open(struct inode *inode, struct file *file)
2480 struct cfent *cfe = __d_cfe(file->f_dentry);
2481 struct cftype *cft = __d_cft(file->f_dentry);
2482 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2483 struct cgroup_subsys_state *css;
2484 int err;
2486 err = generic_file_open(inode, file);
2487 if (err)
2488 return err;
2491 * If the file belongs to a subsystem, pin the css. Will be
2492 * unpinned either on open failure or release. This ensures that
2493 * @css stays alive for all file operations.
2495 rcu_read_lock();
2496 css = cgroup_css(cgrp, cft->ss);
2497 if (cft->ss && !css_tryget(css))
2498 css = NULL;
2499 rcu_read_unlock();
2501 if (!css)
2502 return -ENODEV;
2505 * @cfe->css is used by read/write/close to determine the
2506 * associated css. @file->private_data would be a better place but
2507 * that's already used by seqfile. Multiple accessors may use it
2508 * simultaneously which is okay as the association never changes.
2510 WARN_ON_ONCE(cfe->css && cfe->css != css);
2511 cfe->css = css;
2513 if (cft->read_map || cft->read_seq_string) {
2514 file->f_op = &cgroup_seqfile_operations;
2515 err = single_open(file, cgroup_seqfile_show, cfe);
2516 } else if (cft->open) {
2517 err = cft->open(inode, file);
2520 if (css->ss && err)
2521 css_put(css);
2522 return err;
2525 static int cgroup_file_release(struct inode *inode, struct file *file)
2527 struct cfent *cfe = __d_cfe(file->f_dentry);
2528 struct cftype *cft = __d_cft(file->f_dentry);
2529 struct cgroup_subsys_state *css = cfe->css;
2530 int ret = 0;
2532 if (cft->release)
2533 ret = cft->release(inode, file);
2534 if (css->ss)
2535 css_put(css);
2536 if (file->f_op == &cgroup_seqfile_operations)
2537 single_release(inode, file);
2538 return ret;
2542 * cgroup_rename - Only allow simple rename of directories in place.
2544 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2545 struct inode *new_dir, struct dentry *new_dentry)
2547 int ret;
2548 struct cgroup_name *name, *old_name;
2549 struct cgroup *cgrp;
2552 * It's convinient to use parent dir's i_mutex to protected
2553 * cgrp->name.
2555 lockdep_assert_held(&old_dir->i_mutex);
2557 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2558 return -ENOTDIR;
2559 if (new_dentry->d_inode)
2560 return -EEXIST;
2561 if (old_dir != new_dir)
2562 return -EIO;
2564 cgrp = __d_cgrp(old_dentry);
2567 * This isn't a proper migration and its usefulness is very
2568 * limited. Disallow if sane_behavior.
2570 if (cgroup_sane_behavior(cgrp))
2571 return -EPERM;
2573 name = cgroup_alloc_name(new_dentry);
2574 if (!name)
2575 return -ENOMEM;
2577 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2578 if (ret) {
2579 kfree(name);
2580 return ret;
2583 old_name = rcu_dereference_protected(cgrp->name, true);
2584 rcu_assign_pointer(cgrp->name, name);
2586 kfree_rcu(old_name, rcu_head);
2587 return 0;
2590 static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2592 if (S_ISDIR(dentry->d_inode->i_mode))
2593 return &__d_cgrp(dentry)->xattrs;
2594 else
2595 return &__d_cfe(dentry)->xattrs;
2598 static inline int xattr_enabled(struct dentry *dentry)
2600 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2601 return root->flags & CGRP_ROOT_XATTR;
2604 static bool is_valid_xattr(const char *name)
2606 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2607 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2608 return true;
2609 return false;
2612 static int cgroup_setxattr(struct dentry *dentry, const char *name,
2613 const void *val, size_t size, int flags)
2615 if (!xattr_enabled(dentry))
2616 return -EOPNOTSUPP;
2617 if (!is_valid_xattr(name))
2618 return -EINVAL;
2619 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2622 static int cgroup_removexattr(struct dentry *dentry, const char *name)
2624 if (!xattr_enabled(dentry))
2625 return -EOPNOTSUPP;
2626 if (!is_valid_xattr(name))
2627 return -EINVAL;
2628 return simple_xattr_remove(__d_xattrs(dentry), name);
2631 static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2632 void *buf, size_t size)
2634 if (!xattr_enabled(dentry))
2635 return -EOPNOTSUPP;
2636 if (!is_valid_xattr(name))
2637 return -EINVAL;
2638 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2641 static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2643 if (!xattr_enabled(dentry))
2644 return -EOPNOTSUPP;
2645 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2648 static const struct file_operations cgroup_file_operations = {
2649 .read = cgroup_file_read,
2650 .write = cgroup_file_write,
2651 .llseek = generic_file_llseek,
2652 .open = cgroup_file_open,
2653 .release = cgroup_file_release,
2656 static const struct inode_operations cgroup_file_inode_operations = {
2657 .setxattr = cgroup_setxattr,
2658 .getxattr = cgroup_getxattr,
2659 .listxattr = cgroup_listxattr,
2660 .removexattr = cgroup_removexattr,
2663 static const struct inode_operations cgroup_dir_inode_operations = {
2664 .lookup = simple_lookup,
2665 .mkdir = cgroup_mkdir,
2666 .rmdir = cgroup_rmdir,
2667 .rename = cgroup_rename,
2668 .setxattr = cgroup_setxattr,
2669 .getxattr = cgroup_getxattr,
2670 .listxattr = cgroup_listxattr,
2671 .removexattr = cgroup_removexattr,
2675 * Check if a file is a control file
2677 static inline struct cftype *__file_cft(struct file *file)
2679 if (file_inode(file)->i_fop != &cgroup_file_operations)
2680 return ERR_PTR(-EINVAL);
2681 return __d_cft(file->f_dentry);
2684 static int cgroup_create_file(struct dentry *dentry, umode_t mode,
2685 struct super_block *sb)
2687 struct inode *inode;
2689 if (!dentry)
2690 return -ENOENT;
2691 if (dentry->d_inode)
2692 return -EEXIST;
2694 inode = cgroup_new_inode(mode, sb);
2695 if (!inode)
2696 return -ENOMEM;
2698 if (S_ISDIR(mode)) {
2699 inode->i_op = &cgroup_dir_inode_operations;
2700 inode->i_fop = &simple_dir_operations;
2702 /* start off with i_nlink == 2 (for "." entry) */
2703 inc_nlink(inode);
2704 inc_nlink(dentry->d_parent->d_inode);
2707 * Control reaches here with cgroup_mutex held.
2708 * @inode->i_mutex should nest outside cgroup_mutex but we
2709 * want to populate it immediately without releasing
2710 * cgroup_mutex. As @inode isn't visible to anyone else
2711 * yet, trylock will always succeed without affecting
2712 * lockdep checks.
2714 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
2715 } else if (S_ISREG(mode)) {
2716 inode->i_size = 0;
2717 inode->i_fop = &cgroup_file_operations;
2718 inode->i_op = &cgroup_file_inode_operations;
2720 d_instantiate(dentry, inode);
2721 dget(dentry); /* Extra count - pin the dentry in core */
2722 return 0;
2726 * cgroup_file_mode - deduce file mode of a control file
2727 * @cft: the control file in question
2729 * returns cft->mode if ->mode is not 0
2730 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2731 * returns S_IRUGO if it has only a read handler
2732 * returns S_IWUSR if it has only a write hander
2734 static umode_t cgroup_file_mode(const struct cftype *cft)
2736 umode_t mode = 0;
2738 if (cft->mode)
2739 return cft->mode;
2741 if (cft->read || cft->read_u64 || cft->read_s64 ||
2742 cft->read_map || cft->read_seq_string)
2743 mode |= S_IRUGO;
2745 if (cft->write || cft->write_u64 || cft->write_s64 ||
2746 cft->write_string || cft->trigger)
2747 mode |= S_IWUSR;
2749 return mode;
2752 static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
2754 struct dentry *dir = cgrp->dentry;
2755 struct cgroup *parent = __d_cgrp(dir);
2756 struct dentry *dentry;
2757 struct cfent *cfe;
2758 int error;
2759 umode_t mode;
2760 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2762 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2763 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2764 strcpy(name, cft->ss->name);
2765 strcat(name, ".");
2767 strcat(name, cft->name);
2769 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2771 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2772 if (!cfe)
2773 return -ENOMEM;
2775 dentry = lookup_one_len(name, dir, strlen(name));
2776 if (IS_ERR(dentry)) {
2777 error = PTR_ERR(dentry);
2778 goto out;
2781 cfe->type = (void *)cft;
2782 cfe->dentry = dentry;
2783 dentry->d_fsdata = cfe;
2784 simple_xattrs_init(&cfe->xattrs);
2786 mode = cgroup_file_mode(cft);
2787 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2788 if (!error) {
2789 list_add_tail(&cfe->node, &parent->files);
2790 cfe = NULL;
2792 dput(dentry);
2793 out:
2794 kfree(cfe);
2795 return error;
2799 * cgroup_addrm_files - add or remove files to a cgroup directory
2800 * @cgrp: the target cgroup
2801 * @cfts: array of cftypes to be added
2802 * @is_add: whether to add or remove
2804 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2805 * For removals, this function never fails. If addition fails, this
2806 * function doesn't remove files already added. The caller is responsible
2807 * for cleaning up.
2809 static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2810 bool is_add)
2812 struct cftype *cft;
2813 int ret;
2815 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2816 lockdep_assert_held(&cgroup_mutex);
2818 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2819 /* does cft->flags tell us to skip this file on @cgrp? */
2820 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2821 continue;
2822 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2823 continue;
2824 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2825 continue;
2827 if (is_add) {
2828 ret = cgroup_add_file(cgrp, cft);
2829 if (ret) {
2830 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2831 cft->name, ret);
2832 return ret;
2834 } else {
2835 cgroup_rm_file(cgrp, cft);
2838 return 0;
2841 static void cgroup_cfts_prepare(void)
2842 __acquires(&cgroup_mutex)
2845 * Thanks to the entanglement with vfs inode locking, we can't walk
2846 * the existing cgroups under cgroup_mutex and create files.
2847 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2848 * lock before calling cgroup_addrm_files().
2850 mutex_lock(&cgroup_mutex);
2853 static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
2854 __releases(&cgroup_mutex)
2856 LIST_HEAD(pending);
2857 struct cgroup_subsys *ss = cfts[0].ss;
2858 struct cgroup *root = &ss->root->top_cgroup;
2859 struct super_block *sb = ss->root->sb;
2860 struct dentry *prev = NULL;
2861 struct inode *inode;
2862 struct cgroup_subsys_state *css;
2863 u64 update_before;
2864 int ret = 0;
2866 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2867 if (!cfts || ss->root == &cgroup_dummy_root ||
2868 !atomic_inc_not_zero(&sb->s_active)) {
2869 mutex_unlock(&cgroup_mutex);
2870 return 0;
2874 * All cgroups which are created after we drop cgroup_mutex will
2875 * have the updated set of files, so we only need to update the
2876 * cgroups created before the current @cgroup_serial_nr_next.
2878 update_before = cgroup_serial_nr_next;
2880 /* add/rm files for all cgroups created before */
2881 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
2882 struct cgroup *cgrp = css->cgroup;
2884 if (cgroup_is_dead(cgrp))
2885 continue;
2887 inode = cgrp->dentry->d_inode;
2888 dget(cgrp->dentry);
2889 dput(prev);
2890 prev = cgrp->dentry;
2892 mutex_unlock(&cgroup_mutex);
2893 mutex_lock(&inode->i_mutex);
2894 mutex_lock(&cgroup_mutex);
2895 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
2896 ret = cgroup_addrm_files(cgrp, cfts, is_add);
2897 mutex_unlock(&inode->i_mutex);
2898 if (ret)
2899 break;
2901 mutex_unlock(&cgroup_mutex);
2902 dput(prev);
2903 deactivate_super(sb);
2904 return ret;
2908 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2909 * @ss: target cgroup subsystem
2910 * @cfts: zero-length name terminated array of cftypes
2912 * Register @cfts to @ss. Files described by @cfts are created for all
2913 * existing cgroups to which @ss is attached and all future cgroups will
2914 * have them too. This function can be called anytime whether @ss is
2915 * attached or not.
2917 * Returns 0 on successful registration, -errno on failure. Note that this
2918 * function currently returns 0 as long as @cfts registration is successful
2919 * even if some file creation attempts on existing cgroups fail.
2921 int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2923 struct cftype_set *set;
2924 struct cftype *cft;
2925 int ret;
2927 set = kzalloc(sizeof(*set), GFP_KERNEL);
2928 if (!set)
2929 return -ENOMEM;
2931 for (cft = cfts; cft->name[0] != '\0'; cft++)
2932 cft->ss = ss;
2934 cgroup_cfts_prepare();
2935 set->cfts = cfts;
2936 list_add_tail(&set->node, &ss->cftsets);
2937 ret = cgroup_cfts_commit(cfts, true);
2938 if (ret)
2939 cgroup_rm_cftypes(cfts);
2940 return ret;
2942 EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2945 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2946 * @cfts: zero-length name terminated array of cftypes
2948 * Unregister @cfts. Files described by @cfts are removed from all
2949 * existing cgroups and all future cgroups won't have them either. This
2950 * function can be called anytime whether @cfts' subsys is attached or not.
2952 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2953 * registered.
2955 int cgroup_rm_cftypes(struct cftype *cfts)
2957 struct cftype_set *set;
2959 if (!cfts || !cfts[0].ss)
2960 return -ENOENT;
2962 cgroup_cfts_prepare();
2964 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
2965 if (set->cfts == cfts) {
2966 list_del(&set->node);
2967 kfree(set);
2968 cgroup_cfts_commit(cfts, false);
2969 return 0;
2973 cgroup_cfts_commit(NULL, false);
2974 return -ENOENT;
2978 * cgroup_task_count - count the number of tasks in a cgroup.
2979 * @cgrp: the cgroup in question
2981 * Return the number of tasks in the cgroup.
2983 int cgroup_task_count(const struct cgroup *cgrp)
2985 int count = 0;
2986 struct cgrp_cset_link *link;
2988 read_lock(&css_set_lock);
2989 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2990 count += atomic_read(&link->cset->refcount);
2991 read_unlock(&css_set_lock);
2992 return count;
2996 * To reduce the fork() overhead for systems that are not actually using
2997 * their cgroups capability, we don't maintain the lists running through
2998 * each css_set to its tasks until we see the list actually used - in other
2999 * words after the first call to css_task_iter_start().
3001 static void cgroup_enable_task_cg_lists(void)
3003 struct task_struct *p, *g;
3004 write_lock(&css_set_lock);
3005 use_task_css_set_links = 1;
3007 * We need tasklist_lock because RCU is not safe against
3008 * while_each_thread(). Besides, a forking task that has passed
3009 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3010 * is not guaranteed to have its child immediately visible in the
3011 * tasklist if we walk through it with RCU.
3013 read_lock(&tasklist_lock);
3014 do_each_thread(g, p) {
3015 task_lock(p);
3017 * We should check if the process is exiting, otherwise
3018 * it will race with cgroup_exit() in that the list
3019 * entry won't be deleted though the process has exited.
3020 * Do it while holding siglock so that we don't end up
3021 * racing against cgroup_exit().
3023 spin_lock_irq(&p->sighand->siglock);
3024 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
3025 list_add(&p->cg_list, &task_css_set(p)->tasks);
3026 spin_unlock_irq(&p->sighand->siglock);
3028 task_unlock(p);
3029 } while_each_thread(g, p);
3030 read_unlock(&tasklist_lock);
3031 write_unlock(&css_set_lock);
3035 * css_next_child - find the next child of a given css
3036 * @pos_css: the current position (%NULL to initiate traversal)
3037 * @parent_css: css whose children to walk
3039 * This function returns the next child of @parent_css and should be called
3040 * under RCU read lock. The only requirement is that @parent_css and
3041 * @pos_css are accessible. The next sibling is guaranteed to be returned
3042 * regardless of their states.
3044 struct cgroup_subsys_state *
3045 css_next_child(struct cgroup_subsys_state *pos_css,
3046 struct cgroup_subsys_state *parent_css)
3048 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3049 struct cgroup *cgrp = parent_css->cgroup;
3050 struct cgroup *next;
3052 WARN_ON_ONCE(!rcu_read_lock_held());
3055 * @pos could already have been removed. Once a cgroup is removed,
3056 * its ->sibling.next is no longer updated when its next sibling
3057 * changes. As CGRP_DEAD assertion is serialized and happens
3058 * before the cgroup is taken off the ->sibling list, if we see it
3059 * unasserted, it's guaranteed that the next sibling hasn't
3060 * finished its grace period even if it's already removed, and thus
3061 * safe to dereference from this RCU critical section. If
3062 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3063 * to be visible as %true here.
3065 * If @pos is dead, its next pointer can't be dereferenced;
3066 * however, as each cgroup is given a monotonically increasing
3067 * unique serial number and always appended to the sibling list,
3068 * the next one can be found by walking the parent's children until
3069 * we see a cgroup with higher serial number than @pos's. While
3070 * this path can be slower, it's taken only when either the current
3071 * cgroup is removed or iteration and removal race.
3073 if (!pos) {
3074 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3075 } else if (likely(!cgroup_is_dead(pos))) {
3076 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3077 } else {
3078 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3079 if (next->serial_nr > pos->serial_nr)
3080 break;
3083 if (&next->sibling == &cgrp->children)
3084 return NULL;
3086 return cgroup_css(next, parent_css->ss);
3088 EXPORT_SYMBOL_GPL(css_next_child);
3091 * css_next_descendant_pre - find the next descendant for pre-order walk
3092 * @pos: the current position (%NULL to initiate traversal)
3093 * @root: css whose descendants to walk
3095 * To be used by css_for_each_descendant_pre(). Find the next descendant
3096 * to visit for pre-order traversal of @root's descendants. @root is
3097 * included in the iteration and the first node to be visited.
3099 * While this function requires RCU read locking, it doesn't require the
3100 * whole traversal to be contained in a single RCU critical section. This
3101 * function will return the correct next descendant as long as both @pos
3102 * and @root are accessible and @pos is a descendant of @root.
3104 struct cgroup_subsys_state *
3105 css_next_descendant_pre(struct cgroup_subsys_state *pos,
3106 struct cgroup_subsys_state *root)
3108 struct cgroup_subsys_state *next;
3110 WARN_ON_ONCE(!rcu_read_lock_held());
3112 /* if first iteration, visit @root */
3113 if (!pos)
3114 return root;
3116 /* visit the first child if exists */
3117 next = css_next_child(NULL, pos);
3118 if (next)
3119 return next;
3121 /* no child, visit my or the closest ancestor's next sibling */
3122 while (pos != root) {
3123 next = css_next_child(pos, css_parent(pos));
3124 if (next)
3125 return next;
3126 pos = css_parent(pos);
3129 return NULL;
3131 EXPORT_SYMBOL_GPL(css_next_descendant_pre);
3134 * css_rightmost_descendant - return the rightmost descendant of a css
3135 * @pos: css of interest
3137 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3138 * is returned. This can be used during pre-order traversal to skip
3139 * subtree of @pos.
3141 * While this function requires RCU read locking, it doesn't require the
3142 * whole traversal to be contained in a single RCU critical section. This
3143 * function will return the correct rightmost descendant as long as @pos is
3144 * accessible.
3146 struct cgroup_subsys_state *
3147 css_rightmost_descendant(struct cgroup_subsys_state *pos)
3149 struct cgroup_subsys_state *last, *tmp;
3151 WARN_ON_ONCE(!rcu_read_lock_held());
3153 do {
3154 last = pos;
3155 /* ->prev isn't RCU safe, walk ->next till the end */
3156 pos = NULL;
3157 css_for_each_child(tmp, last)
3158 pos = tmp;
3159 } while (pos);
3161 return last;
3163 EXPORT_SYMBOL_GPL(css_rightmost_descendant);
3165 static struct cgroup_subsys_state *
3166 css_leftmost_descendant(struct cgroup_subsys_state *pos)
3168 struct cgroup_subsys_state *last;
3170 do {
3171 last = pos;
3172 pos = css_next_child(NULL, pos);
3173 } while (pos);
3175 return last;
3179 * css_next_descendant_post - find the next descendant for post-order walk
3180 * @pos: the current position (%NULL to initiate traversal)
3181 * @root: css whose descendants to walk
3183 * To be used by css_for_each_descendant_post(). Find the next descendant
3184 * to visit for post-order traversal of @root's descendants. @root is
3185 * included in the iteration and the last node to be visited.
3187 * While this function requires RCU read locking, it doesn't require the
3188 * whole traversal to be contained in a single RCU critical section. This
3189 * function will return the correct next descendant as long as both @pos
3190 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3192 struct cgroup_subsys_state *
3193 css_next_descendant_post(struct cgroup_subsys_state *pos,
3194 struct cgroup_subsys_state *root)
3196 struct cgroup_subsys_state *next;
3198 WARN_ON_ONCE(!rcu_read_lock_held());
3200 /* if first iteration, visit leftmost descendant which may be @root */
3201 if (!pos)
3202 return css_leftmost_descendant(root);
3204 /* if we visited @root, we're done */
3205 if (pos == root)
3206 return NULL;
3208 /* if there's an unvisited sibling, visit its leftmost descendant */
3209 next = css_next_child(pos, css_parent(pos));
3210 if (next)
3211 return css_leftmost_descendant(next);
3213 /* no sibling left, visit parent */
3214 return css_parent(pos);
3216 EXPORT_SYMBOL_GPL(css_next_descendant_post);
3219 * css_advance_task_iter - advance a task itererator to the next css_set
3220 * @it: the iterator to advance
3222 * Advance @it to the next css_set to walk.
3224 static void css_advance_task_iter(struct css_task_iter *it)
3226 struct list_head *l = it->cset_link;
3227 struct cgrp_cset_link *link;
3228 struct css_set *cset;
3230 /* Advance to the next non-empty css_set */
3231 do {
3232 l = l->next;
3233 if (l == &it->origin_css->cgroup->cset_links) {
3234 it->cset_link = NULL;
3235 return;
3237 link = list_entry(l, struct cgrp_cset_link, cset_link);
3238 cset = link->cset;
3239 } while (list_empty(&cset->tasks));
3240 it->cset_link = l;
3241 it->task = cset->tasks.next;
3245 * css_task_iter_start - initiate task iteration
3246 * @css: the css to walk tasks of
3247 * @it: the task iterator to use
3249 * Initiate iteration through the tasks of @css. The caller can call
3250 * css_task_iter_next() to walk through the tasks until the function
3251 * returns NULL. On completion of iteration, css_task_iter_end() must be
3252 * called.
3254 * Note that this function acquires a lock which is released when the
3255 * iteration finishes. The caller can't sleep while iteration is in
3256 * progress.
3258 void css_task_iter_start(struct cgroup_subsys_state *css,
3259 struct css_task_iter *it)
3260 __acquires(css_set_lock)
3263 * The first time anyone tries to iterate across a css, we need to
3264 * enable the list linking each css_set to its tasks, and fix up
3265 * all existing tasks.
3267 if (!use_task_css_set_links)
3268 cgroup_enable_task_cg_lists();
3270 read_lock(&css_set_lock);
3272 it->origin_css = css;
3273 it->cset_link = &css->cgroup->cset_links;
3275 css_advance_task_iter(it);
3279 * css_task_iter_next - return the next task for the iterator
3280 * @it: the task iterator being iterated
3282 * The "next" function for task iteration. @it should have been
3283 * initialized via css_task_iter_start(). Returns NULL when the iteration
3284 * reaches the end.
3286 struct task_struct *css_task_iter_next(struct css_task_iter *it)
3288 struct task_struct *res;
3289 struct list_head *l = it->task;
3290 struct cgrp_cset_link *link;
3292 /* If the iterator cg is NULL, we have no tasks */
3293 if (!it->cset_link)
3294 return NULL;
3295 res = list_entry(l, struct task_struct, cg_list);
3296 /* Advance iterator to find next entry */
3297 l = l->next;
3298 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3299 if (l == &link->cset->tasks) {
3301 * We reached the end of this task list - move on to the
3302 * next cgrp_cset_link.
3304 css_advance_task_iter(it);
3305 } else {
3306 it->task = l;
3308 return res;
3312 * css_task_iter_end - finish task iteration
3313 * @it: the task iterator to finish
3315 * Finish task iteration started by css_task_iter_start().
3317 void css_task_iter_end(struct css_task_iter *it)
3318 __releases(css_set_lock)
3320 read_unlock(&css_set_lock);
3323 static inline int started_after_time(struct task_struct *t1,
3324 struct timespec *time,
3325 struct task_struct *t2)
3327 int start_diff = timespec_compare(&t1->start_time, time);
3328 if (start_diff > 0) {
3329 return 1;
3330 } else if (start_diff < 0) {
3331 return 0;
3332 } else {
3334 * Arbitrarily, if two processes started at the same
3335 * time, we'll say that the lower pointer value
3336 * started first. Note that t2 may have exited by now
3337 * so this may not be a valid pointer any longer, but
3338 * that's fine - it still serves to distinguish
3339 * between two tasks started (effectively) simultaneously.
3341 return t1 > t2;
3346 * This function is a callback from heap_insert() and is used to order
3347 * the heap.
3348 * In this case we order the heap in descending task start time.
3350 static inline int started_after(void *p1, void *p2)
3352 struct task_struct *t1 = p1;
3353 struct task_struct *t2 = p2;
3354 return started_after_time(t1, &t2->start_time, t2);
3358 * css_scan_tasks - iterate though all the tasks in a css
3359 * @css: the css to iterate tasks of
3360 * @test: optional test callback
3361 * @process: process callback
3362 * @data: data passed to @test and @process
3363 * @heap: optional pre-allocated heap used for task iteration
3365 * Iterate through all the tasks in @css, calling @test for each, and if it
3366 * returns %true, call @process for it also.
3368 * @test may be NULL, meaning always true (select all tasks), which
3369 * effectively duplicates css_task_iter_{start,next,end}() but does not
3370 * lock css_set_lock for the call to @process.
3372 * It is guaranteed that @process will act on every task that is a member
3373 * of @css for the duration of this call. This function may or may not
3374 * call @process for tasks that exit or move to a different css during the
3375 * call, or are forked or move into the css during the call.
3377 * Note that @test may be called with locks held, and may in some
3378 * situations be called multiple times for the same task, so it should be
3379 * cheap.
3381 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3382 * heap operations (and its "gt" member will be overwritten), else a
3383 * temporary heap will be used (allocation of which may cause this function
3384 * to fail).
3386 int css_scan_tasks(struct cgroup_subsys_state *css,
3387 bool (*test)(struct task_struct *, void *),
3388 void (*process)(struct task_struct *, void *),
3389 void *data, struct ptr_heap *heap)
3391 int retval, i;
3392 struct css_task_iter it;
3393 struct task_struct *p, *dropped;
3394 /* Never dereference latest_task, since it's not refcounted */
3395 struct task_struct *latest_task = NULL;
3396 struct ptr_heap tmp_heap;
3397 struct timespec latest_time = { 0, 0 };
3399 if (heap) {
3400 /* The caller supplied our heap and pre-allocated its memory */
3401 heap->gt = &started_after;
3402 } else {
3403 /* We need to allocate our own heap memory */
3404 heap = &tmp_heap;
3405 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3406 if (retval)
3407 /* cannot allocate the heap */
3408 return retval;
3411 again:
3413 * Scan tasks in the css, using the @test callback to determine
3414 * which are of interest, and invoking @process callback on the
3415 * ones which need an update. Since we don't want to hold any
3416 * locks during the task updates, gather tasks to be processed in a
3417 * heap structure. The heap is sorted by descending task start
3418 * time. If the statically-sized heap fills up, we overflow tasks
3419 * that started later, and in future iterations only consider tasks
3420 * that started after the latest task in the previous pass. This
3421 * guarantees forward progress and that we don't miss any tasks.
3423 heap->size = 0;
3424 css_task_iter_start(css, &it);
3425 while ((p = css_task_iter_next(&it))) {
3427 * Only affect tasks that qualify per the caller's callback,
3428 * if he provided one
3430 if (test && !test(p, data))
3431 continue;
3433 * Only process tasks that started after the last task
3434 * we processed
3436 if (!started_after_time(p, &latest_time, latest_task))
3437 continue;
3438 dropped = heap_insert(heap, p);
3439 if (dropped == NULL) {
3441 * The new task was inserted; the heap wasn't
3442 * previously full
3444 get_task_struct(p);
3445 } else if (dropped != p) {
3447 * The new task was inserted, and pushed out a
3448 * different task
3450 get_task_struct(p);
3451 put_task_struct(dropped);
3454 * Else the new task was newer than anything already in
3455 * the heap and wasn't inserted
3458 css_task_iter_end(&it);
3460 if (heap->size) {
3461 for (i = 0; i < heap->size; i++) {
3462 struct task_struct *q = heap->ptrs[i];
3463 if (i == 0) {
3464 latest_time = q->start_time;
3465 latest_task = q;
3467 /* Process the task per the caller's callback */
3468 process(q, data);
3469 put_task_struct(q);
3472 * If we had to process any tasks at all, scan again
3473 * in case some of them were in the middle of forking
3474 * children that didn't get processed.
3475 * Not the most efficient way to do it, but it avoids
3476 * having to take callback_mutex in the fork path
3478 goto again;
3480 if (heap == &tmp_heap)
3481 heap_free(&tmp_heap);
3482 return 0;
3485 static void cgroup_transfer_one_task(struct task_struct *task, void *data)
3487 struct cgroup *new_cgroup = data;
3489 mutex_lock(&cgroup_mutex);
3490 cgroup_attach_task(new_cgroup, task, false);
3491 mutex_unlock(&cgroup_mutex);
3495 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3496 * @to: cgroup to which the tasks will be moved
3497 * @from: cgroup in which the tasks currently reside
3499 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3501 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3502 to, NULL);
3506 * Stuff for reading the 'tasks'/'procs' files.
3508 * Reading this file can return large amounts of data if a cgroup has
3509 * *lots* of attached tasks. So it may need several calls to read(),
3510 * but we cannot guarantee that the information we produce is correct
3511 * unless we produce it entirely atomically.
3515 /* which pidlist file are we talking about? */
3516 enum cgroup_filetype {
3517 CGROUP_FILE_PROCS,
3518 CGROUP_FILE_TASKS,
3522 * A pidlist is a list of pids that virtually represents the contents of one
3523 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3524 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3525 * to the cgroup.
3527 struct cgroup_pidlist {
3529 * used to find which pidlist is wanted. doesn't change as long as
3530 * this particular list stays in the list.
3532 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3533 /* array of xids */
3534 pid_t *list;
3535 /* how many elements the above list has */
3536 int length;
3537 /* how many files are using the current array */
3538 int use_count;
3539 /* each of these stored in a list by its cgroup */
3540 struct list_head links;
3541 /* pointer to the cgroup we belong to, for list removal purposes */
3542 struct cgroup *owner;
3543 /* protects the other fields */
3544 struct rw_semaphore rwsem;
3548 * The following two functions "fix" the issue where there are more pids
3549 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3550 * TODO: replace with a kernel-wide solution to this problem
3552 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3553 static void *pidlist_allocate(int count)
3555 if (PIDLIST_TOO_LARGE(count))
3556 return vmalloc(count * sizeof(pid_t));
3557 else
3558 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3560 static void pidlist_free(void *p)
3562 if (is_vmalloc_addr(p))
3563 vfree(p);
3564 else
3565 kfree(p);
3569 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3570 * Returns the number of unique elements.
3572 static int pidlist_uniq(pid_t *list, int length)
3574 int src, dest = 1;
3577 * we presume the 0th element is unique, so i starts at 1. trivial
3578 * edge cases first; no work needs to be done for either
3580 if (length == 0 || length == 1)
3581 return length;
3582 /* src and dest walk down the list; dest counts unique elements */
3583 for (src = 1; src < length; src++) {
3584 /* find next unique element */
3585 while (list[src] == list[src-1]) {
3586 src++;
3587 if (src == length)
3588 goto after;
3590 /* dest always points to where the next unique element goes */
3591 list[dest] = list[src];
3592 dest++;
3594 after:
3595 return dest;
3598 static int cmppid(const void *a, const void *b)
3600 return *(pid_t *)a - *(pid_t *)b;
3604 * find the appropriate pidlist for our purpose (given procs vs tasks)
3605 * returns with the lock on that pidlist already held, and takes care
3606 * of the use count, or returns NULL with no locks held if we're out of
3607 * memory.
3609 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3610 enum cgroup_filetype type)
3612 struct cgroup_pidlist *l;
3613 /* don't need task_nsproxy() if we're looking at ourself */
3614 struct pid_namespace *ns = task_active_pid_ns(current);
3617 * We can't drop the pidlist_mutex before taking the l->rwsem in case
3618 * the last ref-holder is trying to remove l from the list at the same
3619 * time. Holding the pidlist_mutex precludes somebody taking whichever
3620 * list we find out from under us - compare release_pid_array().
3622 mutex_lock(&cgrp->pidlist_mutex);
3623 list_for_each_entry(l, &cgrp->pidlists, links) {
3624 if (l->key.type == type && l->key.ns == ns) {
3625 /* make sure l doesn't vanish out from under us */
3626 down_write(&l->rwsem);
3627 mutex_unlock(&cgrp->pidlist_mutex);
3628 return l;
3631 /* entry not found; create a new one */
3632 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3633 if (!l) {
3634 mutex_unlock(&cgrp->pidlist_mutex);
3635 return l;
3637 init_rwsem(&l->rwsem);
3638 down_write(&l->rwsem);
3639 l->key.type = type;
3640 l->key.ns = get_pid_ns(ns);
3641 l->owner = cgrp;
3642 list_add(&l->links, &cgrp->pidlists);
3643 mutex_unlock(&cgrp->pidlist_mutex);
3644 return l;
3648 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3650 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3651 struct cgroup_pidlist **lp)
3653 pid_t *array;
3654 int length;
3655 int pid, n = 0; /* used for populating the array */
3656 struct css_task_iter it;
3657 struct task_struct *tsk;
3658 struct cgroup_pidlist *l;
3661 * If cgroup gets more users after we read count, we won't have
3662 * enough space - tough. This race is indistinguishable to the
3663 * caller from the case that the additional cgroup users didn't
3664 * show up until sometime later on.
3666 length = cgroup_task_count(cgrp);
3667 array = pidlist_allocate(length);
3668 if (!array)
3669 return -ENOMEM;
3670 /* now, populate the array */
3671 css_task_iter_start(&cgrp->dummy_css, &it);
3672 while ((tsk = css_task_iter_next(&it))) {
3673 if (unlikely(n == length))
3674 break;
3675 /* get tgid or pid for procs or tasks file respectively */
3676 if (type == CGROUP_FILE_PROCS)
3677 pid = task_tgid_vnr(tsk);
3678 else
3679 pid = task_pid_vnr(tsk);
3680 if (pid > 0) /* make sure to only use valid results */
3681 array[n++] = pid;
3683 css_task_iter_end(&it);
3684 length = n;
3685 /* now sort & (if procs) strip out duplicates */
3686 sort(array, length, sizeof(pid_t), cmppid, NULL);
3687 if (type == CGROUP_FILE_PROCS)
3688 length = pidlist_uniq(array, length);
3689 l = cgroup_pidlist_find(cgrp, type);
3690 if (!l) {
3691 pidlist_free(array);
3692 return -ENOMEM;
3694 /* store array, freeing old if necessary - lock already held */
3695 pidlist_free(l->list);
3696 l->list = array;
3697 l->length = length;
3698 l->use_count++;
3699 up_write(&l->rwsem);
3700 *lp = l;
3701 return 0;
3705 * cgroupstats_build - build and fill cgroupstats
3706 * @stats: cgroupstats to fill information into
3707 * @dentry: A dentry entry belonging to the cgroup for which stats have
3708 * been requested.
3710 * Build and fill cgroupstats so that taskstats can export it to user
3711 * space.
3713 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3715 int ret = -EINVAL;
3716 struct cgroup *cgrp;
3717 struct css_task_iter it;
3718 struct task_struct *tsk;
3721 * Validate dentry by checking the superblock operations,
3722 * and make sure it's a directory.
3724 if (dentry->d_sb->s_op != &cgroup_ops ||
3725 !S_ISDIR(dentry->d_inode->i_mode))
3726 goto err;
3728 ret = 0;
3729 cgrp = dentry->d_fsdata;
3731 css_task_iter_start(&cgrp->dummy_css, &it);
3732 while ((tsk = css_task_iter_next(&it))) {
3733 switch (tsk->state) {
3734 case TASK_RUNNING:
3735 stats->nr_running++;
3736 break;
3737 case TASK_INTERRUPTIBLE:
3738 stats->nr_sleeping++;
3739 break;
3740 case TASK_UNINTERRUPTIBLE:
3741 stats->nr_uninterruptible++;
3742 break;
3743 case TASK_STOPPED:
3744 stats->nr_stopped++;
3745 break;
3746 default:
3747 if (delayacct_is_task_waiting_on_io(tsk))
3748 stats->nr_io_wait++;
3749 break;
3752 css_task_iter_end(&it);
3754 err:
3755 return ret;
3760 * seq_file methods for the tasks/procs files. The seq_file position is the
3761 * next pid to display; the seq_file iterator is a pointer to the pid
3762 * in the cgroup->l->list array.
3765 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3768 * Initially we receive a position value that corresponds to
3769 * one more than the last pid shown (or 0 on the first call or
3770 * after a seek to the start). Use a binary-search to find the
3771 * next pid to display, if any
3773 struct cgroup_pidlist *l = s->private;
3774 int index = 0, pid = *pos;
3775 int *iter;
3777 down_read(&l->rwsem);
3778 if (pid) {
3779 int end = l->length;
3781 while (index < end) {
3782 int mid = (index + end) / 2;
3783 if (l->list[mid] == pid) {
3784 index = mid;
3785 break;
3786 } else if (l->list[mid] <= pid)
3787 index = mid + 1;
3788 else
3789 end = mid;
3792 /* If we're off the end of the array, we're done */
3793 if (index >= l->length)
3794 return NULL;
3795 /* Update the abstract position to be the actual pid that we found */
3796 iter = l->list + index;
3797 *pos = *iter;
3798 return iter;
3801 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3803 struct cgroup_pidlist *l = s->private;
3804 up_read(&l->rwsem);
3807 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
3809 struct cgroup_pidlist *l = s->private;
3810 pid_t *p = v;
3811 pid_t *end = l->list + l->length;
3813 * Advance to the next pid in the array. If this goes off the
3814 * end, we're done
3816 p++;
3817 if (p >= end) {
3818 return NULL;
3819 } else {
3820 *pos = *p;
3821 return p;
3825 static int cgroup_pidlist_show(struct seq_file *s, void *v)
3827 return seq_printf(s, "%d\n", *(int *)v);
3831 * seq_operations functions for iterating on pidlists through seq_file -
3832 * independent of whether it's tasks or procs
3834 static const struct seq_operations cgroup_pidlist_seq_operations = {
3835 .start = cgroup_pidlist_start,
3836 .stop = cgroup_pidlist_stop,
3837 .next = cgroup_pidlist_next,
3838 .show = cgroup_pidlist_show,
3841 static void cgroup_release_pid_array(struct cgroup_pidlist *l)
3844 * the case where we're the last user of this particular pidlist will
3845 * have us remove it from the cgroup's list, which entails taking the
3846 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3847 * pidlist_mutex, we have to take pidlist_mutex first.
3849 mutex_lock(&l->owner->pidlist_mutex);
3850 down_write(&l->rwsem);
3851 BUG_ON(!l->use_count);
3852 if (!--l->use_count) {
3853 /* we're the last user if refcount is 0; remove and free */
3854 list_del(&l->links);
3855 mutex_unlock(&l->owner->pidlist_mutex);
3856 pidlist_free(l->list);
3857 put_pid_ns(l->key.ns);
3858 up_write(&l->rwsem);
3859 kfree(l);
3860 return;
3862 mutex_unlock(&l->owner->pidlist_mutex);
3863 up_write(&l->rwsem);
3866 static int cgroup_pidlist_release(struct inode *inode, struct file *file)
3868 struct cgroup_pidlist *l;
3869 if (!(file->f_mode & FMODE_READ))
3870 return 0;
3872 * the seq_file will only be initialized if the file was opened for
3873 * reading; hence we check if it's not null only in that case.
3875 l = ((struct seq_file *)file->private_data)->private;
3876 cgroup_release_pid_array(l);
3877 return seq_release(inode, file);
3880 static const struct file_operations cgroup_pidlist_operations = {
3881 .read = seq_read,
3882 .llseek = seq_lseek,
3883 .write = cgroup_file_write,
3884 .release = cgroup_pidlist_release,
3888 * The following functions handle opens on a file that displays a pidlist
3889 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3890 * in the cgroup.
3892 /* helper function for the two below it */
3893 static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
3895 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
3896 struct cgroup_pidlist *l;
3897 int retval;
3899 /* Nothing to do for write-only files */
3900 if (!(file->f_mode & FMODE_READ))
3901 return 0;
3903 /* have the array populated */
3904 retval = pidlist_array_load(cgrp, type, &l);
3905 if (retval)
3906 return retval;
3907 /* configure file information */
3908 file->f_op = &cgroup_pidlist_operations;
3910 retval = seq_open(file, &cgroup_pidlist_seq_operations);
3911 if (retval) {
3912 cgroup_release_pid_array(l);
3913 return retval;
3915 ((struct seq_file *)file->private_data)->private = l;
3916 return 0;
3918 static int cgroup_tasks_open(struct inode *unused, struct file *file)
3920 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
3922 static int cgroup_procs_open(struct inode *unused, struct file *file)
3924 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
3927 static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3928 struct cftype *cft)
3930 return notify_on_release(css->cgroup);
3933 static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3934 struct cftype *cft, u64 val)
3936 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
3937 if (val)
3938 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
3939 else
3940 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
3941 return 0;
3945 * When dput() is called asynchronously, if umount has been done and
3946 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3947 * there's a small window that vfs will see the root dentry with non-zero
3948 * refcnt and trigger BUG().
3950 * That's why we hold a reference before dput() and drop it right after.
3952 static void cgroup_dput(struct cgroup *cgrp)
3954 struct super_block *sb = cgrp->root->sb;
3956 atomic_inc(&sb->s_active);
3957 dput(cgrp->dentry);
3958 deactivate_super(sb);
3962 * Unregister event and free resources.
3964 * Gets called from workqueue.
3966 static void cgroup_event_remove(struct work_struct *work)
3968 struct cgroup_event *event = container_of(work, struct cgroup_event,
3969 remove);
3970 struct cgroup_subsys_state *css = event->css;
3972 remove_wait_queue(event->wqh, &event->wait);
3974 event->cft->unregister_event(css, event->cft, event->eventfd);
3976 /* Notify userspace the event is going away. */
3977 eventfd_signal(event->eventfd, 1);
3979 eventfd_ctx_put(event->eventfd);
3980 kfree(event);
3981 css_put(css);
3985 * Gets called on POLLHUP on eventfd when user closes it.
3987 * Called with wqh->lock held and interrupts disabled.
3989 static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3990 int sync, void *key)
3992 struct cgroup_event *event = container_of(wait,
3993 struct cgroup_event, wait);
3994 struct cgroup *cgrp = event->css->cgroup;
3995 unsigned long flags = (unsigned long)key;
3997 if (flags & POLLHUP) {
3999 * If the event has been detached at cgroup removal, we
4000 * can simply return knowing the other side will cleanup
4001 * for us.
4003 * We can't race against event freeing since the other
4004 * side will require wqh->lock via remove_wait_queue(),
4005 * which we hold.
4007 spin_lock(&cgrp->event_list_lock);
4008 if (!list_empty(&event->list)) {
4009 list_del_init(&event->list);
4011 * We are in atomic context, but cgroup_event_remove()
4012 * may sleep, so we have to call it in workqueue.
4014 schedule_work(&event->remove);
4016 spin_unlock(&cgrp->event_list_lock);
4019 return 0;
4022 static void cgroup_event_ptable_queue_proc(struct file *file,
4023 wait_queue_head_t *wqh, poll_table *pt)
4025 struct cgroup_event *event = container_of(pt,
4026 struct cgroup_event, pt);
4028 event->wqh = wqh;
4029 add_wait_queue(wqh, &event->wait);
4033 * Parse input and register new cgroup event handler.
4035 * Input must be in format '<event_fd> <control_fd> <args>'.
4036 * Interpretation of args is defined by control file implementation.
4038 static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
4039 struct cftype *cft, const char *buffer)
4041 struct cgroup *cgrp = dummy_css->cgroup;
4042 struct cgroup_event *event;
4043 struct cgroup_subsys_state *cfile_css;
4044 unsigned int efd, cfd;
4045 struct fd efile;
4046 struct fd cfile;
4047 char *endp;
4048 int ret;
4050 efd = simple_strtoul(buffer, &endp, 10);
4051 if (*endp != ' ')
4052 return -EINVAL;
4053 buffer = endp + 1;
4055 cfd = simple_strtoul(buffer, &endp, 10);
4056 if ((*endp != ' ') && (*endp != '\0'))
4057 return -EINVAL;
4058 buffer = endp + 1;
4060 event = kzalloc(sizeof(*event), GFP_KERNEL);
4061 if (!event)
4062 return -ENOMEM;
4064 INIT_LIST_HEAD(&event->list);
4065 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4066 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4067 INIT_WORK(&event->remove, cgroup_event_remove);
4069 efile = fdget(efd);
4070 if (!efile.file) {
4071 ret = -EBADF;
4072 goto out_kfree;
4075 event->eventfd = eventfd_ctx_fileget(efile.file);
4076 if (IS_ERR(event->eventfd)) {
4077 ret = PTR_ERR(event->eventfd);
4078 goto out_put_efile;
4081 cfile = fdget(cfd);
4082 if (!cfile.file) {
4083 ret = -EBADF;
4084 goto out_put_eventfd;
4087 /* the process need read permission on control file */
4088 /* AV: shouldn't we check that it's been opened for read instead? */
4089 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4090 if (ret < 0)
4091 goto out_put_cfile;
4093 event->cft = __file_cft(cfile.file);
4094 if (IS_ERR(event->cft)) {
4095 ret = PTR_ERR(event->cft);
4096 goto out_put_cfile;
4099 if (!event->cft->ss) {
4100 ret = -EBADF;
4101 goto out_put_cfile;
4105 * Determine the css of @cfile, verify it belongs to the same
4106 * cgroup as cgroup.event_control, and associate @event with it.
4107 * Remaining events are automatically removed on cgroup destruction
4108 * but the removal is asynchronous, so take an extra ref.
4110 rcu_read_lock();
4112 ret = -EINVAL;
4113 event->css = cgroup_css(cgrp, event->cft->ss);
4114 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
4115 if (event->css && event->css == cfile_css && css_tryget(event->css))
4116 ret = 0;
4118 rcu_read_unlock();
4119 if (ret)
4120 goto out_put_cfile;
4122 if (!event->cft->register_event || !event->cft->unregister_event) {
4123 ret = -EINVAL;
4124 goto out_put_css;
4127 ret = event->cft->register_event(event->css, event->cft,
4128 event->eventfd, buffer);
4129 if (ret)
4130 goto out_put_css;
4132 efile.file->f_op->poll(efile.file, &event->pt);
4134 spin_lock(&cgrp->event_list_lock);
4135 list_add(&event->list, &cgrp->event_list);
4136 spin_unlock(&cgrp->event_list_lock);
4138 fdput(cfile);
4139 fdput(efile);
4141 return 0;
4143 out_put_css:
4144 css_put(event->css);
4145 out_put_cfile:
4146 fdput(cfile);
4147 out_put_eventfd:
4148 eventfd_ctx_put(event->eventfd);
4149 out_put_efile:
4150 fdput(efile);
4151 out_kfree:
4152 kfree(event);
4154 return ret;
4157 static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4158 struct cftype *cft)
4160 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
4163 static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4164 struct cftype *cft, u64 val)
4166 if (val)
4167 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
4168 else
4169 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
4170 return 0;
4173 static struct cftype cgroup_base_files[] = {
4175 .name = "cgroup.procs",
4176 .open = cgroup_procs_open,
4177 .write_u64 = cgroup_procs_write,
4178 .release = cgroup_pidlist_release,
4179 .mode = S_IRUGO | S_IWUSR,
4182 .name = "cgroup.event_control",
4183 .write_string = cgroup_write_event_control,
4184 .mode = S_IWUGO,
4187 .name = "cgroup.clone_children",
4188 .flags = CFTYPE_INSANE,
4189 .read_u64 = cgroup_clone_children_read,
4190 .write_u64 = cgroup_clone_children_write,
4193 .name = "cgroup.sane_behavior",
4194 .flags = CFTYPE_ONLY_ON_ROOT,
4195 .read_seq_string = cgroup_sane_behavior_show,
4199 * Historical crazy stuff. These don't have "cgroup." prefix and
4200 * don't exist if sane_behavior. If you're depending on these, be
4201 * prepared to be burned.
4204 .name = "tasks",
4205 .flags = CFTYPE_INSANE, /* use "procs" instead */
4206 .open = cgroup_tasks_open,
4207 .write_u64 = cgroup_tasks_write,
4208 .release = cgroup_pidlist_release,
4209 .mode = S_IRUGO | S_IWUSR,
4212 .name = "notify_on_release",
4213 .flags = CFTYPE_INSANE,
4214 .read_u64 = cgroup_read_notify_on_release,
4215 .write_u64 = cgroup_write_notify_on_release,
4218 .name = "release_agent",
4219 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
4220 .read_seq_string = cgroup_release_agent_show,
4221 .write_string = cgroup_release_agent_write,
4222 .max_write_len = PATH_MAX,
4224 { } /* terminate */
4228 * cgroup_populate_dir - create subsys files in a cgroup directory
4229 * @cgrp: target cgroup
4230 * @subsys_mask: mask of the subsystem ids whose files should be added
4232 * On failure, no file is added.
4234 static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
4236 struct cgroup_subsys *ss;
4237 int i, ret = 0;
4239 /* process cftsets of each subsystem */
4240 for_each_subsys(ss, i) {
4241 struct cftype_set *set;
4243 if (!test_bit(i, &subsys_mask))
4244 continue;
4246 list_for_each_entry(set, &ss->cftsets, node) {
4247 ret = cgroup_addrm_files(cgrp, set->cfts, true);
4248 if (ret < 0)
4249 goto err;
4253 /* This cgroup is ready now */
4254 for_each_root_subsys(cgrp->root, ss) {
4255 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
4256 struct css_id *id = rcu_dereference_protected(css->id, true);
4259 * Update id->css pointer and make this css visible from
4260 * CSS ID functions. This pointer will be dereferened
4261 * from RCU-read-side without locks.
4263 if (id)
4264 rcu_assign_pointer(id->css, css);
4267 return 0;
4268 err:
4269 cgroup_clear_dir(cgrp, subsys_mask);
4270 return ret;
4274 * css destruction is four-stage process.
4276 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4277 * Implemented in kill_css().
4279 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4280 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4281 * by invoking offline_css(). After offlining, the base ref is put.
4282 * Implemented in css_killed_work_fn().
4284 * 3. When the percpu_ref reaches zero, the only possible remaining
4285 * accessors are inside RCU read sections. css_release() schedules the
4286 * RCU callback.
4288 * 4. After the grace period, the css can be freed. Implemented in
4289 * css_free_work_fn().
4291 * It is actually hairier because both step 2 and 4 require process context
4292 * and thus involve punting to css->destroy_work adding two additional
4293 * steps to the already complex sequence.
4295 static void css_free_work_fn(struct work_struct *work)
4297 struct cgroup_subsys_state *css =
4298 container_of(work, struct cgroup_subsys_state, destroy_work);
4299 struct cgroup *cgrp = css->cgroup;
4301 if (css->parent)
4302 css_put(css->parent);
4304 css->ss->css_free(css);
4305 cgroup_dput(cgrp);
4308 static void css_free_rcu_fn(struct rcu_head *rcu_head)
4310 struct cgroup_subsys_state *css =
4311 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4314 * css holds an extra ref to @cgrp->dentry which is put on the last
4315 * css_put(). dput() requires process context which we don't have.
4317 INIT_WORK(&css->destroy_work, css_free_work_fn);
4318 queue_work(cgroup_destroy_wq, &css->destroy_work);
4321 static void css_release(struct percpu_ref *ref)
4323 struct cgroup_subsys_state *css =
4324 container_of(ref, struct cgroup_subsys_state, refcnt);
4326 call_rcu(&css->rcu_head, css_free_rcu_fn);
4329 static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4330 struct cgroup *cgrp)
4332 css->cgroup = cgrp;
4333 css->ss = ss;
4334 css->flags = 0;
4335 css->id = NULL;
4337 if (cgrp->parent)
4338 css->parent = cgroup_css(cgrp->parent, ss);
4339 else
4340 css->flags |= CSS_ROOT;
4342 BUG_ON(cgroup_css(cgrp, ss));
4345 /* invoke ->css_online() on a new CSS and mark it online if successful */
4346 static int online_css(struct cgroup_subsys_state *css)
4348 struct cgroup_subsys *ss = css->ss;
4349 int ret = 0;
4351 lockdep_assert_held(&cgroup_mutex);
4353 if (ss->css_online)
4354 ret = ss->css_online(css);
4355 if (!ret) {
4356 css->flags |= CSS_ONLINE;
4357 css->cgroup->nr_css++;
4358 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4360 return ret;
4363 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4364 static void offline_css(struct cgroup_subsys_state *css)
4366 struct cgroup_subsys *ss = css->ss;
4368 lockdep_assert_held(&cgroup_mutex);
4370 if (!(css->flags & CSS_ONLINE))
4371 return;
4373 if (ss->css_offline)
4374 ss->css_offline(css);
4376 css->flags &= ~CSS_ONLINE;
4377 css->cgroup->nr_css--;
4378 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
4382 * cgroup_create - create a cgroup
4383 * @parent: cgroup that will be parent of the new cgroup
4384 * @dentry: dentry of the new cgroup
4385 * @mode: mode to set on new inode
4387 * Must be called with the mutex on the parent inode held
4389 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4390 umode_t mode)
4392 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
4393 struct cgroup *cgrp;
4394 struct cgroup_name *name;
4395 struct cgroupfs_root *root = parent->root;
4396 int err;
4397 struct cgroup_subsys *ss;
4398 struct super_block *sb = root->sb;
4400 /* allocate the cgroup and its ID, 0 is reserved for the root */
4401 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4402 if (!cgrp)
4403 return -ENOMEM;
4405 name = cgroup_alloc_name(dentry);
4406 if (!name) {
4407 err = -ENOMEM;
4408 goto err_free_cgrp;
4410 rcu_assign_pointer(cgrp->name, name);
4413 * Only live parents can have children. Note that the liveliness
4414 * check isn't strictly necessary because cgroup_mkdir() and
4415 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4416 * anyway so that locking is contained inside cgroup proper and we
4417 * don't get nasty surprises if we ever grow another caller.
4419 if (!cgroup_lock_live_group(parent)) {
4420 err = -ENODEV;
4421 goto err_free_name;
4424 /* Grab a reference on the superblock so the hierarchy doesn't
4425 * get deleted on unmount if there are child cgroups. This
4426 * can be done outside cgroup_mutex, since the sb can't
4427 * disappear while someone has an open control file on the
4428 * fs */
4429 atomic_inc(&sb->s_active);
4432 * Temporarily set the pointer to NULL, so idr_find() won't return
4433 * a half-baked cgroup.
4435 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4436 if (cgrp->id < 0) {
4437 err = -ENOMEM;
4438 goto err_unlock;
4441 init_cgroup_housekeeping(cgrp);
4443 dentry->d_fsdata = cgrp;
4444 cgrp->dentry = dentry;
4446 cgrp->parent = parent;
4447 cgrp->dummy_css.parent = &parent->dummy_css;
4448 cgrp->root = parent->root;
4450 if (notify_on_release(parent))
4451 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4453 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4454 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4456 for_each_root_subsys(root, ss) {
4457 struct cgroup_subsys_state *css;
4459 css = ss->css_alloc(cgroup_css(parent, ss));
4460 if (IS_ERR(css)) {
4461 err = PTR_ERR(css);
4462 goto err_free_all;
4464 css_ar[ss->subsys_id] = css;
4466 err = percpu_ref_init(&css->refcnt, css_release);
4467 if (err)
4468 goto err_free_all;
4470 init_css(css, ss, cgrp);
4472 if (ss->use_id) {
4473 err = alloc_css_id(css);
4474 if (err)
4475 goto err_free_all;
4480 * Create directory. cgroup_create_file() returns with the new
4481 * directory locked on success so that it can be populated without
4482 * dropping cgroup_mutex.
4484 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
4485 if (err < 0)
4486 goto err_free_all;
4487 lockdep_assert_held(&dentry->d_inode->i_mutex);
4489 cgrp->serial_nr = cgroup_serial_nr_next++;
4491 /* allocation complete, commit to creation */
4492 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4493 root->number_of_cgroups++;
4495 /* hold a ref to the parent's dentry */
4496 dget(parent->dentry);
4498 /* creation succeeded, notify subsystems */
4499 for_each_root_subsys(root, ss) {
4500 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
4502 err = online_css(css);
4503 if (err)
4504 goto err_destroy;
4506 /* each css holds a ref to the cgroup's dentry and parent css */
4507 dget(dentry);
4508 css_get(css->parent);
4510 /* mark it consumed for error path */
4511 css_ar[ss->subsys_id] = NULL;
4513 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4514 parent->parent) {
4515 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4516 current->comm, current->pid, ss->name);
4517 if (!strcmp(ss->name, "memory"))
4518 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4519 ss->warned_broken_hierarchy = true;
4523 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4525 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4526 if (err)
4527 goto err_destroy;
4529 err = cgroup_populate_dir(cgrp, root->subsys_mask);
4530 if (err)
4531 goto err_destroy;
4533 mutex_unlock(&cgroup_mutex);
4534 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
4536 return 0;
4538 err_free_all:
4539 for_each_root_subsys(root, ss) {
4540 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
4542 if (css) {
4543 percpu_ref_cancel_init(&css->refcnt);
4544 ss->css_free(css);
4547 idr_remove(&root->cgroup_idr, cgrp->id);
4548 err_unlock:
4549 mutex_unlock(&cgroup_mutex);
4550 /* Release the reference count that we took on the superblock */
4551 deactivate_super(sb);
4552 err_free_name:
4553 kfree(rcu_dereference_raw(cgrp->name));
4554 err_free_cgrp:
4555 kfree(cgrp);
4556 return err;
4558 err_destroy:
4559 for_each_root_subsys(root, ss) {
4560 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
4562 if (css) {
4563 percpu_ref_cancel_init(&css->refcnt);
4564 ss->css_free(css);
4567 cgroup_destroy_locked(cgrp);
4568 mutex_unlock(&cgroup_mutex);
4569 mutex_unlock(&dentry->d_inode->i_mutex);
4570 return err;
4573 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4575 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4577 /* the vfs holds inode->i_mutex already */
4578 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4582 * This is called when the refcnt of a css is confirmed to be killed.
4583 * css_tryget() is now guaranteed to fail.
4585 static void css_killed_work_fn(struct work_struct *work)
4587 struct cgroup_subsys_state *css =
4588 container_of(work, struct cgroup_subsys_state, destroy_work);
4589 struct cgroup *cgrp = css->cgroup;
4591 mutex_lock(&cgroup_mutex);
4594 * css_tryget() is guaranteed to fail now. Tell subsystems to
4595 * initate destruction.
4597 offline_css(css);
4600 * If @cgrp is marked dead, it's waiting for refs of all css's to
4601 * be disabled before proceeding to the second phase of cgroup
4602 * destruction. If we are the last one, kick it off.
4604 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
4605 cgroup_destroy_css_killed(cgrp);
4607 mutex_unlock(&cgroup_mutex);
4610 * Put the css refs from kill_css(). Each css holds an extra
4611 * reference to the cgroup's dentry and cgroup removal proceeds
4612 * regardless of css refs. On the last put of each css, whenever
4613 * that may be, the extra dentry ref is put so that dentry
4614 * destruction happens only after all css's are released.
4616 css_put(css);
4619 /* css kill confirmation processing requires process context, bounce */
4620 static void css_killed_ref_fn(struct percpu_ref *ref)
4622 struct cgroup_subsys_state *css =
4623 container_of(ref, struct cgroup_subsys_state, refcnt);
4625 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4626 queue_work(cgroup_destroy_wq, &css->destroy_work);
4630 * kill_css - destroy a css
4631 * @css: css to destroy
4633 * This function initiates destruction of @css by removing cgroup interface
4634 * files and putting its base reference. ->css_offline() will be invoked
4635 * asynchronously once css_tryget() is guaranteed to fail and when the
4636 * reference count reaches zero, @css will be released.
4638 static void kill_css(struct cgroup_subsys_state *css)
4640 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4643 * Killing would put the base ref, but we need to keep it alive
4644 * until after ->css_offline().
4646 css_get(css);
4649 * cgroup core guarantees that, by the time ->css_offline() is
4650 * invoked, no new css reference will be given out via
4651 * css_tryget(). We can't simply call percpu_ref_kill() and
4652 * proceed to offlining css's because percpu_ref_kill() doesn't
4653 * guarantee that the ref is seen as killed on all CPUs on return.
4655 * Use percpu_ref_kill_and_confirm() to get notifications as each
4656 * css is confirmed to be seen as killed on all CPUs.
4658 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
4662 * cgroup_destroy_locked - the first stage of cgroup destruction
4663 * @cgrp: cgroup to be destroyed
4665 * css's make use of percpu refcnts whose killing latency shouldn't be
4666 * exposed to userland and are RCU protected. Also, cgroup core needs to
4667 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4668 * invoked. To satisfy all the requirements, destruction is implemented in
4669 * the following two steps.
4671 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4672 * userland visible parts and start killing the percpu refcnts of
4673 * css's. Set up so that the next stage will be kicked off once all
4674 * the percpu refcnts are confirmed to be killed.
4676 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4677 * rest of destruction. Once all cgroup references are gone, the
4678 * cgroup is RCU-freed.
4680 * This function implements s1. After this step, @cgrp is gone as far as
4681 * the userland is concerned and a new cgroup with the same name may be
4682 * created. As cgroup doesn't care about the names internally, this
4683 * doesn't cause any problem.
4685 static int cgroup_destroy_locked(struct cgroup *cgrp)
4686 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4688 struct dentry *d = cgrp->dentry;
4689 struct cgroup_event *event, *tmp;
4690 struct cgroup_subsys *ss;
4691 struct cgroup *child;
4692 bool empty;
4694 lockdep_assert_held(&d->d_inode->i_mutex);
4695 lockdep_assert_held(&cgroup_mutex);
4698 * css_set_lock synchronizes access to ->cset_links and prevents
4699 * @cgrp from being removed while __put_css_set() is in progress.
4701 read_lock(&css_set_lock);
4702 empty = list_empty(&cgrp->cset_links);
4703 read_unlock(&css_set_lock);
4704 if (!empty)
4705 return -EBUSY;
4708 * Make sure there's no live children. We can't test ->children
4709 * emptiness as dead children linger on it while being destroyed;
4710 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4712 empty = true;
4713 rcu_read_lock();
4714 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4715 empty = cgroup_is_dead(child);
4716 if (!empty)
4717 break;
4719 rcu_read_unlock();
4720 if (!empty)
4721 return -EBUSY;
4724 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4725 * will be invoked to perform the rest of destruction once the
4726 * percpu refs of all css's are confirmed to be killed.
4728 for_each_root_subsys(cgrp->root, ss) {
4729 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
4731 if (css)
4732 kill_css(css);
4736 * Mark @cgrp dead. This prevents further task migration and child
4737 * creation by disabling cgroup_lock_live_group(). Note that
4738 * CGRP_DEAD assertion is depended upon by css_next_child() to
4739 * resume iteration after dropping RCU read lock. See
4740 * css_next_child() for details.
4742 set_bit(CGRP_DEAD, &cgrp->flags);
4744 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4745 raw_spin_lock(&release_list_lock);
4746 if (!list_empty(&cgrp->release_list))
4747 list_del_init(&cgrp->release_list);
4748 raw_spin_unlock(&release_list_lock);
4751 * If @cgrp has css's attached, the second stage of cgroup
4752 * destruction is kicked off from css_killed_work_fn() after the
4753 * refs of all attached css's are killed. If @cgrp doesn't have
4754 * any css, we kick it off here.
4756 if (!cgrp->nr_css)
4757 cgroup_destroy_css_killed(cgrp);
4760 * Clear the base files and remove @cgrp directory. The removal
4761 * puts the base ref but we aren't quite done with @cgrp yet, so
4762 * hold onto it.
4764 cgroup_addrm_files(cgrp, cgroup_base_files, false);
4765 dget(d);
4766 cgroup_d_remove_dir(d);
4769 * Unregister events and notify userspace.
4770 * Notify userspace about cgroup removing only after rmdir of cgroup
4771 * directory to avoid race between userspace and kernelspace.
4773 spin_lock(&cgrp->event_list_lock);
4774 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4775 list_del_init(&event->list);
4776 schedule_work(&event->remove);
4778 spin_unlock(&cgrp->event_list_lock);
4780 return 0;
4784 * cgroup_destroy_css_killed - the second step of cgroup destruction
4785 * @work: cgroup->destroy_free_work
4787 * This function is invoked from a work item for a cgroup which is being
4788 * destroyed after all css's are offlined and performs the rest of
4789 * destruction. This is the second step of destruction described in the
4790 * comment above cgroup_destroy_locked().
4792 static void cgroup_destroy_css_killed(struct cgroup *cgrp)
4794 struct cgroup *parent = cgrp->parent;
4795 struct dentry *d = cgrp->dentry;
4797 lockdep_assert_held(&cgroup_mutex);
4799 /* delete this cgroup from parent->children */
4800 list_del_rcu(&cgrp->sibling);
4803 * We should remove the cgroup object from idr before its grace
4804 * period starts, so we won't be looking up a cgroup while the
4805 * cgroup is being freed.
4807 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4808 cgrp->id = -1;
4810 dput(d);
4812 set_bit(CGRP_RELEASABLE, &parent->flags);
4813 check_for_release(parent);
4816 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4818 int ret;
4820 mutex_lock(&cgroup_mutex);
4821 ret = cgroup_destroy_locked(dentry->d_fsdata);
4822 mutex_unlock(&cgroup_mutex);
4824 return ret;
4827 static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4829 INIT_LIST_HEAD(&ss->cftsets);
4832 * base_cftset is embedded in subsys itself, no need to worry about
4833 * deregistration.
4835 if (ss->base_cftypes) {
4836 struct cftype *cft;
4838 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4839 cft->ss = ss;
4841 ss->base_cftset.cfts = ss->base_cftypes;
4842 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4846 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4848 struct cgroup_subsys_state *css;
4850 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
4852 mutex_lock(&cgroup_mutex);
4854 /* init base cftset */
4855 cgroup_init_cftsets(ss);
4857 /* Create the top cgroup state for this subsystem */
4858 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4859 ss->root = &cgroup_dummy_root;
4860 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
4861 /* We don't handle early failures gracefully */
4862 BUG_ON(IS_ERR(css));
4863 init_css(css, ss, cgroup_dummy_top);
4865 /* Update the init_css_set to contain a subsys
4866 * pointer to this state - since the subsystem is
4867 * newly registered, all tasks and hence the
4868 * init_css_set is in the subsystem's top cgroup. */
4869 init_css_set.subsys[ss->subsys_id] = css;
4871 need_forkexit_callback |= ss->fork || ss->exit;
4873 /* At system boot, before all subsystems have been
4874 * registered, no tasks have been forked, so we don't
4875 * need to invoke fork callbacks here. */
4876 BUG_ON(!list_empty(&init_task.tasks));
4878 BUG_ON(online_css(css));
4880 mutex_unlock(&cgroup_mutex);
4882 /* this function shouldn't be used with modular subsystems, since they
4883 * need to register a subsys_id, among other things */
4884 BUG_ON(ss->module);
4888 * cgroup_load_subsys: load and register a modular subsystem at runtime
4889 * @ss: the subsystem to load
4891 * This function should be called in a modular subsystem's initcall. If the
4892 * subsystem is built as a module, it will be assigned a new subsys_id and set
4893 * up for use. If the subsystem is built-in anyway, work is delegated to the
4894 * simpler cgroup_init_subsys.
4896 int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4898 struct cgroup_subsys_state *css;
4899 int i, ret;
4900 struct hlist_node *tmp;
4901 struct css_set *cset;
4902 unsigned long key;
4904 /* check name and function validity */
4905 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4906 ss->css_alloc == NULL || ss->css_free == NULL)
4907 return -EINVAL;
4910 * we don't support callbacks in modular subsystems. this check is
4911 * before the ss->module check for consistency; a subsystem that could
4912 * be a module should still have no callbacks even if the user isn't
4913 * compiling it as one.
4915 if (ss->fork || ss->exit)
4916 return -EINVAL;
4919 * an optionally modular subsystem is built-in: we want to do nothing,
4920 * since cgroup_init_subsys will have already taken care of it.
4922 if (ss->module == NULL) {
4923 /* a sanity check */
4924 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
4925 return 0;
4928 /* init base cftset */
4929 cgroup_init_cftsets(ss);
4931 mutex_lock(&cgroup_mutex);
4932 cgroup_subsys[ss->subsys_id] = ss;
4935 * no ss->css_alloc seems to need anything important in the ss
4936 * struct, so this can happen first (i.e. before the dummy root
4937 * attachment).
4939 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
4940 if (IS_ERR(css)) {
4941 /* failure case - need to deassign the cgroup_subsys[] slot. */
4942 cgroup_subsys[ss->subsys_id] = NULL;
4943 mutex_unlock(&cgroup_mutex);
4944 return PTR_ERR(css);
4947 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4948 ss->root = &cgroup_dummy_root;
4950 /* our new subsystem will be attached to the dummy hierarchy. */
4951 init_css(css, ss, cgroup_dummy_top);
4952 /* init_idr must be after init_css() because it sets css->id. */
4953 if (ss->use_id) {
4954 ret = cgroup_init_idr(ss, css);
4955 if (ret)
4956 goto err_unload;
4960 * Now we need to entangle the css into the existing css_sets. unlike
4961 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4962 * will need a new pointer to it; done by iterating the css_set_table.
4963 * furthermore, modifying the existing css_sets will corrupt the hash
4964 * table state, so each changed css_set will need its hash recomputed.
4965 * this is all done under the css_set_lock.
4967 write_lock(&css_set_lock);
4968 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
4969 /* skip entries that we already rehashed */
4970 if (cset->subsys[ss->subsys_id])
4971 continue;
4972 /* remove existing entry */
4973 hash_del(&cset->hlist);
4974 /* set new value */
4975 cset->subsys[ss->subsys_id] = css;
4976 /* recompute hash and restore entry */
4977 key = css_set_hash(cset->subsys);
4978 hash_add(css_set_table, &cset->hlist, key);
4980 write_unlock(&css_set_lock);
4982 ret = online_css(css);
4983 if (ret)
4984 goto err_unload;
4986 /* success! */
4987 mutex_unlock(&cgroup_mutex);
4988 return 0;
4990 err_unload:
4991 mutex_unlock(&cgroup_mutex);
4992 /* @ss can't be mounted here as try_module_get() would fail */
4993 cgroup_unload_subsys(ss);
4994 return ret;
4996 EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4999 * cgroup_unload_subsys: unload a modular subsystem
5000 * @ss: the subsystem to unload
5002 * This function should be called in a modular subsystem's exitcall. When this
5003 * function is invoked, the refcount on the subsystem's module will be 0, so
5004 * the subsystem will not be attached to any hierarchy.
5006 void cgroup_unload_subsys(struct cgroup_subsys *ss)
5008 struct cgrp_cset_link *link;
5010 BUG_ON(ss->module == NULL);
5013 * we shouldn't be called if the subsystem is in use, and the use of
5014 * try_module_get() in rebind_subsystems() should ensure that it
5015 * doesn't start being used while we're killing it off.
5017 BUG_ON(ss->root != &cgroup_dummy_root);
5019 mutex_lock(&cgroup_mutex);
5021 offline_css(cgroup_css(cgroup_dummy_top, ss));
5023 if (ss->use_id)
5024 idr_destroy(&ss->idr);
5026 /* deassign the subsys_id */
5027 cgroup_subsys[ss->subsys_id] = NULL;
5029 /* remove subsystem from the dummy root's list of subsystems */
5030 list_del_init(&ss->sibling);
5033 * disentangle the css from all css_sets attached to the dummy
5034 * top. as in loading, we need to pay our respects to the hashtable
5035 * gods.
5037 write_lock(&css_set_lock);
5038 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
5039 struct css_set *cset = link->cset;
5040 unsigned long key;
5042 hash_del(&cset->hlist);
5043 cset->subsys[ss->subsys_id] = NULL;
5044 key = css_set_hash(cset->subsys);
5045 hash_add(css_set_table, &cset->hlist, key);
5047 write_unlock(&css_set_lock);
5050 * remove subsystem's css from the cgroup_dummy_top and free it -
5051 * need to free before marking as null because ss->css_free needs
5052 * the cgrp->subsys pointer to find their state. note that this
5053 * also takes care of freeing the css_id.
5055 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
5056 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
5058 mutex_unlock(&cgroup_mutex);
5060 EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
5063 * cgroup_init_early - cgroup initialization at system boot
5065 * Initialize cgroups at system boot, and initialize any
5066 * subsystems that request early init.
5068 int __init cgroup_init_early(void)
5070 struct cgroup_subsys *ss;
5071 int i;
5073 atomic_set(&init_css_set.refcount, 1);
5074 INIT_LIST_HEAD(&init_css_set.cgrp_links);
5075 INIT_LIST_HEAD(&init_css_set.tasks);
5076 INIT_HLIST_NODE(&init_css_set.hlist);
5077 css_set_count = 1;
5078 init_cgroup_root(&cgroup_dummy_root);
5079 cgroup_root_count = 1;
5080 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
5082 init_cgrp_cset_link.cset = &init_css_set;
5083 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5084 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
5085 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
5087 /* at bootup time, we don't worry about modular subsystems */
5088 for_each_builtin_subsys(ss, i) {
5089 BUG_ON(!ss->name);
5090 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
5091 BUG_ON(!ss->css_alloc);
5092 BUG_ON(!ss->css_free);
5093 if (ss->subsys_id != i) {
5094 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
5095 ss->name, ss->subsys_id);
5096 BUG();
5099 if (ss->early_init)
5100 cgroup_init_subsys(ss);
5102 return 0;
5106 * cgroup_init - cgroup initialization
5108 * Register cgroup filesystem and /proc file, and initialize
5109 * any subsystems that didn't request early init.
5111 int __init cgroup_init(void)
5113 struct cgroup_subsys *ss;
5114 unsigned long key;
5115 int i, err;
5117 err = bdi_init(&cgroup_backing_dev_info);
5118 if (err)
5119 return err;
5121 for_each_builtin_subsys(ss, i) {
5122 if (!ss->early_init)
5123 cgroup_init_subsys(ss);
5124 if (ss->use_id)
5125 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
5128 /* allocate id for the dummy hierarchy */
5129 mutex_lock(&cgroup_mutex);
5130 mutex_lock(&cgroup_root_mutex);
5132 /* Add init_css_set to the hash table */
5133 key = css_set_hash(init_css_set.subsys);
5134 hash_add(css_set_table, &init_css_set.hlist, key);
5136 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
5138 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5139 0, 1, GFP_KERNEL);
5140 BUG_ON(err < 0);
5142 mutex_unlock(&cgroup_root_mutex);
5143 mutex_unlock(&cgroup_mutex);
5145 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5146 if (!cgroup_kobj) {
5147 err = -ENOMEM;
5148 goto out;
5151 err = register_filesystem(&cgroup_fs_type);
5152 if (err < 0) {
5153 kobject_put(cgroup_kobj);
5154 goto out;
5157 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
5159 out:
5160 if (err)
5161 bdi_destroy(&cgroup_backing_dev_info);
5163 return err;
5166 static int __init cgroup_wq_init(void)
5169 * There isn't much point in executing destruction path in
5170 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5171 * Use 1 for @max_active.
5173 * We would prefer to do this in cgroup_init() above, but that
5174 * is called before init_workqueues(): so leave this until after.
5176 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
5177 BUG_ON(!cgroup_destroy_wq);
5178 return 0;
5180 core_initcall(cgroup_wq_init);
5183 * proc_cgroup_show()
5184 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5185 * - Used for /proc/<pid>/cgroup.
5186 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5187 * doesn't really matter if tsk->cgroup changes after we read it,
5188 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
5189 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5190 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5191 * cgroup to top_cgroup.
5194 /* TODO: Use a proper seq_file iterator */
5195 int proc_cgroup_show(struct seq_file *m, void *v)
5197 struct pid *pid;
5198 struct task_struct *tsk;
5199 char *buf;
5200 int retval;
5201 struct cgroupfs_root *root;
5203 retval = -ENOMEM;
5204 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5205 if (!buf)
5206 goto out;
5208 retval = -ESRCH;
5209 pid = m->private;
5210 tsk = get_pid_task(pid, PIDTYPE_PID);
5211 if (!tsk)
5212 goto out_free;
5214 retval = 0;
5216 mutex_lock(&cgroup_mutex);
5218 for_each_active_root(root) {
5219 struct cgroup_subsys *ss;
5220 struct cgroup *cgrp;
5221 int count = 0;
5223 seq_printf(m, "%d:", root->hierarchy_id);
5224 for_each_root_subsys(root, ss)
5225 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
5226 if (strlen(root->name))
5227 seq_printf(m, "%sname=%s", count ? "," : "",
5228 root->name);
5229 seq_putc(m, ':');
5230 cgrp = task_cgroup_from_root(tsk, root);
5231 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
5232 if (retval < 0)
5233 goto out_unlock;
5234 seq_puts(m, buf);
5235 seq_putc(m, '\n');
5238 out_unlock:
5239 mutex_unlock(&cgroup_mutex);
5240 put_task_struct(tsk);
5241 out_free:
5242 kfree(buf);
5243 out:
5244 return retval;
5247 /* Display information about each subsystem and each hierarchy */
5248 static int proc_cgroupstats_show(struct seq_file *m, void *v)
5250 struct cgroup_subsys *ss;
5251 int i;
5253 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5255 * ideally we don't want subsystems moving around while we do this.
5256 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5257 * subsys/hierarchy state.
5259 mutex_lock(&cgroup_mutex);
5261 for_each_subsys(ss, i)
5262 seq_printf(m, "%s\t%d\t%d\t%d\n",
5263 ss->name, ss->root->hierarchy_id,
5264 ss->root->number_of_cgroups, !ss->disabled);
5266 mutex_unlock(&cgroup_mutex);
5267 return 0;
5270 static int cgroupstats_open(struct inode *inode, struct file *file)
5272 return single_open(file, proc_cgroupstats_show, NULL);
5275 static const struct file_operations proc_cgroupstats_operations = {
5276 .open = cgroupstats_open,
5277 .read = seq_read,
5278 .llseek = seq_lseek,
5279 .release = single_release,
5283 * cgroup_fork - attach newly forked task to its parents cgroup.
5284 * @child: pointer to task_struct of forking parent process.
5286 * Description: A task inherits its parent's cgroup at fork().
5288 * A pointer to the shared css_set was automatically copied in
5289 * fork.c by dup_task_struct(). However, we ignore that copy, since
5290 * it was not made under the protection of RCU or cgroup_mutex, so
5291 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5292 * have already changed current->cgroups, allowing the previously
5293 * referenced cgroup group to be removed and freed.
5295 * At the point that cgroup_fork() is called, 'current' is the parent
5296 * task, and the passed argument 'child' points to the child task.
5298 void cgroup_fork(struct task_struct *child)
5300 task_lock(current);
5301 get_css_set(task_css_set(current));
5302 child->cgroups = current->cgroups;
5303 task_unlock(current);
5304 INIT_LIST_HEAD(&child->cg_list);
5308 * cgroup_post_fork - called on a new task after adding it to the task list
5309 * @child: the task in question
5311 * Adds the task to the list running through its css_set if necessary and
5312 * call the subsystem fork() callbacks. Has to be after the task is
5313 * visible on the task list in case we race with the first call to
5314 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5315 * list.
5317 void cgroup_post_fork(struct task_struct *child)
5319 struct cgroup_subsys *ss;
5320 int i;
5323 * use_task_css_set_links is set to 1 before we walk the tasklist
5324 * under the tasklist_lock and we read it here after we added the child
5325 * to the tasklist under the tasklist_lock as well. If the child wasn't
5326 * yet in the tasklist when we walked through it from
5327 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5328 * should be visible now due to the paired locking and barriers implied
5329 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5330 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5331 * lock on fork.
5333 if (use_task_css_set_links) {
5334 write_lock(&css_set_lock);
5335 task_lock(child);
5336 if (list_empty(&child->cg_list))
5337 list_add(&child->cg_list, &task_css_set(child)->tasks);
5338 task_unlock(child);
5339 write_unlock(&css_set_lock);
5343 * Call ss->fork(). This must happen after @child is linked on
5344 * css_set; otherwise, @child might change state between ->fork()
5345 * and addition to css_set.
5347 if (need_forkexit_callback) {
5349 * fork/exit callbacks are supported only for builtin
5350 * subsystems, and the builtin section of the subsys
5351 * array is immutable, so we don't need to lock the
5352 * subsys array here. On the other hand, modular section
5353 * of the array can be freed at module unload, so we
5354 * can't touch that.
5356 for_each_builtin_subsys(ss, i)
5357 if (ss->fork)
5358 ss->fork(child);
5363 * cgroup_exit - detach cgroup from exiting task
5364 * @tsk: pointer to task_struct of exiting process
5365 * @run_callback: run exit callbacks?
5367 * Description: Detach cgroup from @tsk and release it.
5369 * Note that cgroups marked notify_on_release force every task in
5370 * them to take the global cgroup_mutex mutex when exiting.
5371 * This could impact scaling on very large systems. Be reluctant to
5372 * use notify_on_release cgroups where very high task exit scaling
5373 * is required on large systems.
5375 * the_top_cgroup_hack:
5377 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5379 * We call cgroup_exit() while the task is still competent to
5380 * handle notify_on_release(), then leave the task attached to the
5381 * root cgroup in each hierarchy for the remainder of its exit.
5383 * To do this properly, we would increment the reference count on
5384 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5385 * code we would add a second cgroup function call, to drop that
5386 * reference. This would just create an unnecessary hot spot on
5387 * the top_cgroup reference count, to no avail.
5389 * Normally, holding a reference to a cgroup without bumping its
5390 * count is unsafe. The cgroup could go away, or someone could
5391 * attach us to a different cgroup, decrementing the count on
5392 * the first cgroup that we never incremented. But in this case,
5393 * top_cgroup isn't going away, and either task has PF_EXITING set,
5394 * which wards off any cgroup_attach_task() attempts, or task is a failed
5395 * fork, never visible to cgroup_attach_task.
5397 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5399 struct cgroup_subsys *ss;
5400 struct css_set *cset;
5401 int i;
5404 * Unlink from the css_set task list if necessary.
5405 * Optimistically check cg_list before taking
5406 * css_set_lock
5408 if (!list_empty(&tsk->cg_list)) {
5409 write_lock(&css_set_lock);
5410 if (!list_empty(&tsk->cg_list))
5411 list_del_init(&tsk->cg_list);
5412 write_unlock(&css_set_lock);
5415 /* Reassign the task to the init_css_set. */
5416 task_lock(tsk);
5417 cset = task_css_set(tsk);
5418 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
5420 if (run_callbacks && need_forkexit_callback) {
5422 * fork/exit callbacks are supported only for builtin
5423 * subsystems, see cgroup_post_fork() for details.
5425 for_each_builtin_subsys(ss, i) {
5426 if (ss->exit) {
5427 struct cgroup_subsys_state *old_css = cset->subsys[i];
5428 struct cgroup_subsys_state *css = task_css(tsk, i);
5430 ss->exit(css, old_css, tsk);
5434 task_unlock(tsk);
5436 put_css_set_taskexit(cset);
5439 static void check_for_release(struct cgroup *cgrp)
5441 if (cgroup_is_releasable(cgrp) &&
5442 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
5444 * Control Group is currently removeable. If it's not
5445 * already queued for a userspace notification, queue
5446 * it now
5448 int need_schedule_work = 0;
5450 raw_spin_lock(&release_list_lock);
5451 if (!cgroup_is_dead(cgrp) &&
5452 list_empty(&cgrp->release_list)) {
5453 list_add(&cgrp->release_list, &release_list);
5454 need_schedule_work = 1;
5456 raw_spin_unlock(&release_list_lock);
5457 if (need_schedule_work)
5458 schedule_work(&release_agent_work);
5463 * Notify userspace when a cgroup is released, by running the
5464 * configured release agent with the name of the cgroup (path
5465 * relative to the root of cgroup file system) as the argument.
5467 * Most likely, this user command will try to rmdir this cgroup.
5469 * This races with the possibility that some other task will be
5470 * attached to this cgroup before it is removed, or that some other
5471 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5472 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5473 * unused, and this cgroup will be reprieved from its death sentence,
5474 * to continue to serve a useful existence. Next time it's released,
5475 * we will get notified again, if it still has 'notify_on_release' set.
5477 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5478 * means only wait until the task is successfully execve()'d. The
5479 * separate release agent task is forked by call_usermodehelper(),
5480 * then control in this thread returns here, without waiting for the
5481 * release agent task. We don't bother to wait because the caller of
5482 * this routine has no use for the exit status of the release agent
5483 * task, so no sense holding our caller up for that.
5485 static void cgroup_release_agent(struct work_struct *work)
5487 BUG_ON(work != &release_agent_work);
5488 mutex_lock(&cgroup_mutex);
5489 raw_spin_lock(&release_list_lock);
5490 while (!list_empty(&release_list)) {
5491 char *argv[3], *envp[3];
5492 int i;
5493 char *pathbuf = NULL, *agentbuf = NULL;
5494 struct cgroup *cgrp = list_entry(release_list.next,
5495 struct cgroup,
5496 release_list);
5497 list_del_init(&cgrp->release_list);
5498 raw_spin_unlock(&release_list_lock);
5499 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5500 if (!pathbuf)
5501 goto continue_free;
5502 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5503 goto continue_free;
5504 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5505 if (!agentbuf)
5506 goto continue_free;
5508 i = 0;
5509 argv[i++] = agentbuf;
5510 argv[i++] = pathbuf;
5511 argv[i] = NULL;
5513 i = 0;
5514 /* minimal command environment */
5515 envp[i++] = "HOME=/";
5516 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5517 envp[i] = NULL;
5519 /* Drop the lock while we invoke the usermode helper,
5520 * since the exec could involve hitting disk and hence
5521 * be a slow process */
5522 mutex_unlock(&cgroup_mutex);
5523 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
5524 mutex_lock(&cgroup_mutex);
5525 continue_free:
5526 kfree(pathbuf);
5527 kfree(agentbuf);
5528 raw_spin_lock(&release_list_lock);
5530 raw_spin_unlock(&release_list_lock);
5531 mutex_unlock(&cgroup_mutex);
5534 static int __init cgroup_disable(char *str)
5536 struct cgroup_subsys *ss;
5537 char *token;
5538 int i;
5540 while ((token = strsep(&str, ",")) != NULL) {
5541 if (!*token)
5542 continue;
5545 * cgroup_disable, being at boot time, can't know about
5546 * module subsystems, so we don't worry about them.
5548 for_each_builtin_subsys(ss, i) {
5549 if (!strcmp(token, ss->name)) {
5550 ss->disabled = 1;
5551 printk(KERN_INFO "Disabling %s control group"
5552 " subsystem\n", ss->name);
5553 break;
5557 return 1;
5559 __setup("cgroup_disable=", cgroup_disable);
5562 * Functons for CSS ID.
5565 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5566 unsigned short css_id(struct cgroup_subsys_state *css)
5568 struct css_id *cssid;
5571 * This css_id() can return correct value when somone has refcnt
5572 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5573 * it's unchanged until freed.
5575 cssid = rcu_dereference_raw(css->id);
5577 if (cssid)
5578 return cssid->id;
5579 return 0;
5581 EXPORT_SYMBOL_GPL(css_id);
5584 * css_is_ancestor - test "root" css is an ancestor of "child"
5585 * @child: the css to be tested.
5586 * @root: the css supporsed to be an ancestor of the child.
5588 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5589 * this function reads css->id, the caller must hold rcu_read_lock().
5590 * But, considering usual usage, the csses should be valid objects after test.
5591 * Assuming that the caller will do some action to the child if this returns
5592 * returns true, the caller must take "child";s reference count.
5593 * If "child" is valid object and this returns true, "root" is valid, too.
5596 bool css_is_ancestor(struct cgroup_subsys_state *child,
5597 const struct cgroup_subsys_state *root)
5599 struct css_id *child_id;
5600 struct css_id *root_id;
5602 child_id = rcu_dereference(child->id);
5603 if (!child_id)
5604 return false;
5605 root_id = rcu_dereference(root->id);
5606 if (!root_id)
5607 return false;
5608 if (child_id->depth < root_id->depth)
5609 return false;
5610 if (child_id->stack[root_id->depth] != root_id->id)
5611 return false;
5612 return true;
5615 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5617 struct css_id *id = rcu_dereference_protected(css->id, true);
5619 /* When this is called before css_id initialization, id can be NULL */
5620 if (!id)
5621 return;
5623 BUG_ON(!ss->use_id);
5625 rcu_assign_pointer(id->css, NULL);
5626 rcu_assign_pointer(css->id, NULL);
5627 spin_lock(&ss->id_lock);
5628 idr_remove(&ss->idr, id->id);
5629 spin_unlock(&ss->id_lock);
5630 kfree_rcu(id, rcu_head);
5632 EXPORT_SYMBOL_GPL(free_css_id);
5635 * This is called by init or create(). Then, calls to this function are
5636 * always serialized (By cgroup_mutex() at create()).
5639 static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5641 struct css_id *newid;
5642 int ret, size;
5644 BUG_ON(!ss->use_id);
5646 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5647 newid = kzalloc(size, GFP_KERNEL);
5648 if (!newid)
5649 return ERR_PTR(-ENOMEM);
5651 idr_preload(GFP_KERNEL);
5652 spin_lock(&ss->id_lock);
5653 /* Don't use 0. allocates an ID of 1-65535 */
5654 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
5655 spin_unlock(&ss->id_lock);
5656 idr_preload_end();
5658 /* Returns error when there are no free spaces for new ID.*/
5659 if (ret < 0)
5660 goto err_out;
5662 newid->id = ret;
5663 newid->depth = depth;
5664 return newid;
5665 err_out:
5666 kfree(newid);
5667 return ERR_PTR(ret);
5671 static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5672 struct cgroup_subsys_state *rootcss)
5674 struct css_id *newid;
5676 spin_lock_init(&ss->id_lock);
5677 idr_init(&ss->idr);
5679 newid = get_new_cssid(ss, 0);
5680 if (IS_ERR(newid))
5681 return PTR_ERR(newid);
5683 newid->stack[0] = newid->id;
5684 RCU_INIT_POINTER(newid->css, rootcss);
5685 RCU_INIT_POINTER(rootcss->id, newid);
5686 return 0;
5689 static int alloc_css_id(struct cgroup_subsys_state *child_css)
5691 struct cgroup_subsys_state *parent_css = css_parent(child_css);
5692 struct css_id *child_id, *parent_id;
5693 int i, depth;
5695 parent_id = rcu_dereference_protected(parent_css->id, true);
5696 depth = parent_id->depth + 1;
5698 child_id = get_new_cssid(child_css->ss, depth);
5699 if (IS_ERR(child_id))
5700 return PTR_ERR(child_id);
5702 for (i = 0; i < depth; i++)
5703 child_id->stack[i] = parent_id->stack[i];
5704 child_id->stack[depth] = child_id->id;
5706 * child_id->css pointer will be set after this cgroup is available
5707 * see cgroup_populate_dir()
5709 rcu_assign_pointer(child_css->id, child_id);
5711 return 0;
5715 * css_lookup - lookup css by id
5716 * @ss: cgroup subsys to be looked into.
5717 * @id: the id
5719 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5720 * NULL if not. Should be called under rcu_read_lock()
5722 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5724 struct css_id *cssid = NULL;
5726 BUG_ON(!ss->use_id);
5727 cssid = idr_find(&ss->idr, id);
5729 if (unlikely(!cssid))
5730 return NULL;
5732 return rcu_dereference(cssid->css);
5734 EXPORT_SYMBOL_GPL(css_lookup);
5737 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5738 * @dentry: directory dentry of interest
5739 * @ss: subsystem of interest
5741 * Must be called under RCU read lock. The caller is responsible for
5742 * pinning the returned css if it needs to be accessed outside the RCU
5743 * critical section.
5745 struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5746 struct cgroup_subsys *ss)
5748 struct cgroup *cgrp;
5750 WARN_ON_ONCE(!rcu_read_lock_held());
5752 /* is @dentry a cgroup dir? */
5753 if (!dentry->d_inode ||
5754 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
5755 return ERR_PTR(-EBADF);
5757 cgrp = __d_cgrp(dentry);
5758 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
5762 * css_from_id - lookup css by id
5763 * @id: the cgroup id
5764 * @ss: cgroup subsys to be looked into
5766 * Returns the css if there's valid one with @id, otherwise returns NULL.
5767 * Should be called under rcu_read_lock().
5769 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5771 struct cgroup *cgrp;
5773 rcu_lockdep_assert(rcu_read_lock_held() ||
5774 lockdep_is_held(&cgroup_mutex),
5775 "css_from_id() needs proper protection");
5777 cgrp = idr_find(&ss->root->cgroup_idr, id);
5778 if (cgrp)
5779 return cgroup_css(cgrp, ss);
5780 return NULL;
5783 #ifdef CONFIG_CGROUP_DEBUG
5784 static struct cgroup_subsys_state *
5785 debug_css_alloc(struct cgroup_subsys_state *parent_css)
5787 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5789 if (!css)
5790 return ERR_PTR(-ENOMEM);
5792 return css;
5795 static void debug_css_free(struct cgroup_subsys_state *css)
5797 kfree(css);
5800 static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5801 struct cftype *cft)
5803 return cgroup_task_count(css->cgroup);
5806 static u64 current_css_set_read(struct cgroup_subsys_state *css,
5807 struct cftype *cft)
5809 return (u64)(unsigned long)current->cgroups;
5812 static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
5813 struct cftype *cft)
5815 u64 count;
5817 rcu_read_lock();
5818 count = atomic_read(&task_css_set(current)->refcount);
5819 rcu_read_unlock();
5820 return count;
5823 static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
5824 struct cftype *cft,
5825 struct seq_file *seq)
5827 struct cgrp_cset_link *link;
5828 struct css_set *cset;
5830 read_lock(&css_set_lock);
5831 rcu_read_lock();
5832 cset = rcu_dereference(current->cgroups);
5833 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
5834 struct cgroup *c = link->cgrp;
5835 const char *name;
5837 if (c->dentry)
5838 name = c->dentry->d_name.name;
5839 else
5840 name = "?";
5841 seq_printf(seq, "Root %d group %s\n",
5842 c->root->hierarchy_id, name);
5844 rcu_read_unlock();
5845 read_unlock(&css_set_lock);
5846 return 0;
5849 #define MAX_TASKS_SHOWN_PER_CSS 25
5850 static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5851 struct cftype *cft, struct seq_file *seq)
5853 struct cgrp_cset_link *link;
5855 read_lock(&css_set_lock);
5856 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
5857 struct css_set *cset = link->cset;
5858 struct task_struct *task;
5859 int count = 0;
5860 seq_printf(seq, "css_set %p\n", cset);
5861 list_for_each_entry(task, &cset->tasks, cg_list) {
5862 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5863 seq_puts(seq, " ...\n");
5864 break;
5865 } else {
5866 seq_printf(seq, " task %d\n",
5867 task_pid_vnr(task));
5871 read_unlock(&css_set_lock);
5872 return 0;
5875 static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
5877 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
5880 static struct cftype debug_files[] = {
5882 .name = "taskcount",
5883 .read_u64 = debug_taskcount_read,
5887 .name = "current_css_set",
5888 .read_u64 = current_css_set_read,
5892 .name = "current_css_set_refcount",
5893 .read_u64 = current_css_set_refcount_read,
5897 .name = "current_css_set_cg_links",
5898 .read_seq_string = current_css_set_cg_links_read,
5902 .name = "cgroup_css_links",
5903 .read_seq_string = cgroup_css_links_read,
5907 .name = "releasable",
5908 .read_u64 = releasable_read,
5911 { } /* terminate */
5914 struct cgroup_subsys debug_subsys = {
5915 .name = "debug",
5916 .css_alloc = debug_css_alloc,
5917 .css_free = debug_css_free,
5918 .subsys_id = debug_subsys_id,
5919 .base_cftypes = debug_files,
5921 #endif /* CONFIG_CGROUP_DEBUG */