/* * Generic process-grouping system. * * Based originally on the cpuset system, extracted by Paul Menage * Copyright (C) 2006 Google, Inc * * Notifications support * Copyright (C) 2009 Nokia Corporation * Author: Kirill A. Shutemov * * Copyright notices from the original cpuset code: * -------------------------------------------------- * Copyright (C) 2003 BULL SA. * Copyright (C) 2004-2006 Silicon Graphics, Inc. * * Portions derived from Patrick Mochel's sysfs code. * sysfs is Copyright (c) 2001-3 Patrick Mochel * * 2003-10-10 Written by Simon Derr. * 2003-10-22 Updates by Stephen Hemminger. * 2004 May-July Rework by Paul Jackson. * --------------------------------------------------- * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of the Linux * distribution for more details. */ #include <linux/cgroup.h>#include <linux/cred.h>#include <linux/ctype.h>#include <linux/errno.h>#include <linux/init_task.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/mm.h>#include <linux/mutex.h>#include <linux/mount.h>#include <linux/pagemap.h>#include <linux/proc_fs.h>#include <linux/rcupdate.h>#include <linux/sched.h>#include <linux/backing-dev.h>#include <linux/seq_file.h>#include <linux/slab.h>#include <linux/magic.h>#include <linux/spinlock.h>#include <linux/string.h>#include <linux/sort.h>#include <linux/kmod.h>#include <linux/module.h>#include <linux/delayacct.h>#include <linux/cgroupstats.h>#include <linux/hashtable.h>#include <linux/namei.h>#include <linux/pid_namespace.h>#include <linux/idr.h>#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */#include <linux/eventfd.h>#include <linux/poll.h>#include <linux/flex_array.h> /* used in cgroup_attach_task */#include <linux/kthread.h> #include <linux/atomic.h> /* * cgroup_mutex is the master lock. Any modification to cgroup or its * hierarchy must be performed while holding it. * * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify * cgroupfs_root of any cgroup hierarchy - subsys list, flags, * release_agent_path and so on. Modifying requires both cgroup_mutex and * cgroup_root_mutex. Readers can acquire either of the two. This is to * break the following locking order cycle. * * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem * B. namespace_sem -> cgroup_mutex * * B happens only through cgroup_show_options() and using cgroup_root_mutex * breaks it. */#ifdef CONFIG_PROVE_RCUDEFINE_MUTEX(cgroup_mutex);EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */#elsestatic DEFINE_MUTEX(cgroup_mutex);#endif static DEFINE_MUTEX(cgroup_root_mutex); /* * Generate an array of cgroup subsystem pointers. At boot time, this is * populated with the built in subsystems, and modular subsystems are * registered after that. The mutable section of this array is protected by * cgroup_mutex. */#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {#include <linux/cgroup_subsys.h>}; /* * The dummy hierarchy, reserved for the subsystems that are otherwise * unattached - it never has more than a single cgroup, and all tasks are * part of that cgroup. */static struct cgroupfs_root cgroup_dummy_root; /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; /* * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. */struct cfent { struct list_head node; struct dentry *dentry; struct cftype *type; /* file xattrs */ struct simple_xattrs xattrs;}; /* * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when * cgroup_subsys->use_id != 0. */#define CSS_ID_MAX (65535)struct css_id { /* * The css to which this ID points. This pointer is set to valid value * after cgroup is populated. If cgroup is removed, this will be NULL. * This pointer is expected to be RCU-safe because destroy() * is called after synchronize_rcu(). But for safe use, css_tryget() * should be used for avoiding race. */ struct cgroup_subsys_state __rcu *css; /* * ID of this css. */ unsigned short id; /* * Depth in hierarchy which this ID belongs to. */ unsigned short depth; /* * ID is freed by RCU. (and lookup routine is RCU safe.) */ struct rcu_head rcu_head; /* * Hierarchy of CSS ID belongs to. */ unsigned short stack[0]; /* Array of Length (depth+1) */}; /* * cgroup_event represents events which userspace want to receive. */struct cgroup_event { /* * Cgroup which the event belongs to. */ struct cgroup *cgrp; /* * Control file which the event associated. */ struct cftype *cft; /* * eventfd to signal userspace about the event. */ struct eventfd_ctx *eventfd; /* * Each of these stored in a list by the cgroup. */ struct list_head list; /* * All fields below needed to unregister event when * userspace closes eventfd. */ poll_table pt; wait_queue_head_t *wqh; wait_queue_t wait; struct work_struct remove;}; /* The list of hierarchy roots */ static LIST_HEAD(cgroup_roots);static int cgroup_root_count; /* * Hierarchy ID allocation and mapping. It follows the same exclusion * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for * writes, either for reads. */static DEFINE_IDR(cgroup_hierarchy_idr); static struct cgroup_name root_cgroup_name = { .name = "/" }; /* * Assign a monotonically increasing serial number to cgroups. It * guarantees cgroups with bigger numbers are newer than those with smaller * numbers. Also, as cgroups are always appended to the parent's * ->children list, it guarantees that sibling cgroups are always sorted in * the ascending serial number order on the list. Protected by * cgroup_mutex. */static u64 cgroup_serial_nr_next = 1; /* This flag indicates whether tasks in the fork and exit paths should * check for fork/exit handlers to call. This avoids us having to do * extra work in the fork/exit path if none of the subsystems need to * be called. */static int need_forkexit_callback __read_mostly; static void cgroup_offline_fn(struct work_struct *work);static int cgroup_destroy_locked(struct cgroup *cgrp);static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, struct cftype cfts[], bool is_add); /* convenient tests for these bits */static inline bool cgroup_is_dead(const struct cgroup *cgrp){ return test_bit(CGRP_DEAD, &cgrp->flags);} /** * cgroup_is_descendant - test ancestry * @cgrp: the cgroup to be tested * @ancestor: possible ancestor of @cgrp * * Test whether @cgrp is a descendant of @ancestor. It also returns %true * if @cgrp == @ancestor. This function is safe to call as long as @cgrp * and @ancestor are accessible. */bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor){ while (cgrp) { if (cgrp == ancestor) return true; cgrp = cgrp->parent; } return false;}EXPORT_SYMBOL_GPL(cgroup_is_descendant); static int cgroup_is_releasable(const struct cgroup *cgrp){ const int bits = (1 << CGRP_RELEASABLE) | (1 << CGRP_NOTIFY_ON_RELEASE); return (cgrp->flags & bits) == bits;} static int notify_on_release(const struct cgroup *cgrp){ return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);} /** * for_each_subsys - iterate all loaded cgroup subsystems * @ss: the iteration cursor * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end * * Should be called under cgroup_mutex. */#define for_each_subsys(ss, i) \ for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ if (({ lockdep_assert_held(&cgroup_mutex); \ !((ss) = cgroup_subsys[i]); })) { } \ else /** * for_each_builtin_subsys - iterate all built-in cgroup subsystems * @ss: the iteration cursor * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end * * Bulit-in subsystems are always present and iteration itself doesn't * require any synchronization. */#define for_each_builtin_subsys(ss, i) \ for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ (((ss) = cgroup_subsys[i]) || true); (i)++) /* iterate each subsystem attached to a hierarchy */#define for_each_root_subsys(root, ss) \ list_for_each_entry((ss), &(root)->subsys_list, sibling) /* iterate across the active hierarchies */#define for_each_active_root(root) \ list_for_each_entry((root), &cgroup_roots, root_list) static inline struct cgroup *__d_cgrp(struct dentry *dentry){ return dentry->d_fsdata;} static inline struct cfent *__d_cfe(struct dentry *dentry){ return dentry->d_fsdata;} static inline struct cftype *__d_cft(struct dentry *dentry){ return __d_cfe(dentry)->type;} /** * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. * @cgrp: the cgroup to be checked for liveness * * On success, returns true; the mutex should be later unlocked. On * failure returns false with no lock held. */static bool cgroup_lock_live_group(struct cgroup *cgrp){ mutex_lock(&cgroup_mutex); if (cgroup_is_dead(cgrp)) { mutex_unlock(&cgroup_mutex); return false; } return true;} /* the list of cgroups eligible for automatic release. Protected by * release_list_lock */static LIST_HEAD(release_list);static DEFINE_RAW_SPINLOCK(release_list_lock);static void cgroup_release_agent(struct work_struct *work);static DECLARE_WORK(release_agent_work, cgroup_release_agent);static void check_for_release(struct cgroup *cgrp); /* * A cgroup can be associated with multiple css_sets as different tasks may * belong to different cgroups on different hierarchies. In the other * direction, a css_set is naturally associated with multiple cgroups. * This M:N relationship is represented by the following link structure * which exists for each association and allows traversing the associations * from both sides. */struct cgrp_cset_link { /* the cgroup and css_set this link associates */ struct cgroup *cgrp; struct css_set *cset; /* list of cgrp_cset_links anchored at cgrp->cset_links */ struct list_head cset_link; /* list of cgrp_cset_links anchored at css_set->cgrp_links */ struct list_head cgrp_link;}; /* The default css_set - used by init and its children prior to any * hierarchies being mounted. It contains a pointer to the root state * for each subsystem. Also used to anchor the list of css_sets. Not * reference-counted, to improve performance when child cgroups * haven't been created. */ static struct css_set init_css_set;static struct cgrp_cset_link init_cgrp_cset_link; static int cgroup_init_idr(struct cgroup_subsys *ss, struct cgroup_subsys_state *css); /* css_set_lock protects the list of css_set objects, and the * chain of tasks off each css_set. Nests outside task->alloc_lock * due to cgroup_iter_start() */static DEFINE_RWLOCK(css_set_lock);static int css_set_count; /* * hash table for cgroup groups. This improves the performance to find * an existing css_set. This hash doesn't (currently) take into * account cgroups in empty hierarchies. */#define CSS_SET_HASH_BITS 7static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); static unsigned long css_set_hash(struct cgroup_subsys_state *css[]){ unsigned long key = 0UL; struct cgroup_subsys *ss; int i; for_each_subsys(ss, i) key += (unsigned long)css[i]; key = (key >> 16) ^ key; return key;} /* We don't maintain the lists running through each css_set to its * task until after the first call to cgroup_iter_start(). This * reduces the fork()/exit() overhead for people who have cgroups * compiled into their kernel but not actually in use */static int use_task_css_set_links __read_mostly; static void __put_css_set(struct css_set *cset, int taskexit){ struct cgrp_cset_link *link, *tmp_link; /* * Ensure that the refcount doesn't hit zero while any readers * can see it. Similar to atomic_dec_and_lock(), but for an * rwlock */ if (atomic_add_unless(&cset->refcount, -1, 1)) return; write_lock(&css_set_lock); if (!atomic_dec_and_test(&cset->refcount)) { write_unlock(&css_set_lock); return; } /* This css_set is dead. unlink it and release cgroup refcounts */ hash_del(&cset->hlist); css_set_count--; list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { struct cgroup *cgrp = link->cgrp; list_del(&link->cset_link); list_del(&link->cgrp_link); /* @cgrp can't go away while we're holding css_set_lock */ if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { if (taskexit) set_bit(CGRP_RELEASABLE, &cgrp->flags); check_for_release(cgrp); } kfree(link); } write_unlock(&css_set_lock); kfree_rcu(cset, rcu_head);} /* * refcounted get/put for css_set objects */static inline void get_css_set(struct css_set *cset){ atomic_inc(&cset->refcount);} static inline void put_css_set(struct css_set *cset){ __put_css_set(cset, 0);} static inline void put_css_set_taskexit(struct css_set *cset){ __put_css_set(cset, 1);} /** * compare_css_sets - helper function for find_existing_css_set(). * @cset: candidate css_set being tested * @old_cset: existing css_set for a task * @new_cgrp: cgroup that's being entered by the task * @template: desired set of css pointers in css_set (pre-calculated) * * Returns true if "cg" matches "old_cg" except for the hierarchy * which "new_cgrp" belongs to, for which it should match "new_cgrp". */static bool compare_css_sets(struct css_set *cset, struct css_set *old_cset, struct cgroup *new_cgrp, struct cgroup_subsys_state *template[]){ struct list_head *l1, *l2; if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { /* Not all subsystems matched */ return false; } /* * Compare cgroup pointers in order to distinguish between * different cgroups in heirarchies with no subsystems. We * could get by with just this check alone (and skip the * memcmp above) but on most setups the memcmp check will * avoid the need for this more expensive check on almost all * candidates. */ l1 = &cset->cgrp_links; l2 = &old_cset->cgrp_links; while (1) { struct cgrp_cset_link *link1, *link2; struct cgroup *cgrp1, *cgrp2; l1 = l1->next; l2 = l2->next; /* See if we reached the end - both lists are equal length. */ if (l1 == &cset->cgrp_links) {Lines 1–500 of 5687. Load more
/* * Generic process-grouping system. * * Based originally on the cpuset system, extracted by Paul Menage * Copyright (C) 2006 Google, Inc * * Notifications support * Copyright (C) 2009 Nokia Corporation * Author: Kirill A. Shutemov * * Copyright notices from the original cpuset code: * -------------------------------------------------- * Copyright (C) 2003 BULL SA. * Copyright (C) 2004-2006 Silicon Graphics, Inc. * * Portions derived from Patrick Mochel's sysfs code. * sysfs is Copyright (c) 2001-3 Patrick Mochel * * 2003-10-10 Written by Simon Derr. * 2003-10-22 Updates by Stephen Hemminger. * 2004 May-July Rework by Paul Jackson. * --------------------------------------------------- * * This file is subject to the terms and conditions of the GNU General Public * License. See the file COPYING in the main directory of the Linux * distribution for more details. */ #include <linux/cgroup.h> #include <linux/cred.h> #include <linux/ctype.h> #include <linux/errno.h> #include <linux/init_task.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/mm.h> #include <linux/mutex.h> #include <linux/mount.h> #include <linux/pagemap.h> #include <linux/proc_fs.h> #include <linux/rcupdate.h> #include <linux/sched.h> #include <linux/backing-dev.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/magic.h> #include <linux/spinlock.h> #include <linux/string.h> #include <linux/sort.h> #include <linux/kmod.h> #include <linux/module.h> #include <linux/delayacct.h> #include <linux/cgroupstats.h> #include <linux/hashtable.h> #include <linux/namei.h> #include <linux/pid_namespace.h> #include <linux/idr.h> #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ #include <linux/eventfd.h> #include <linux/poll.h> #include <linux/flex_array.h> /* used in cgroup_attach_task */ #include <linux/kthread.h> #include <linux/atomic.h> /* * cgroup_mutex is the master lock. Any modification to cgroup or its * hierarchy must be performed while holding it. * * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify * cgroupfs_root of any cgroup hierarchy - subsys list, flags, * release_agent_path and so on. Modifying requires both cgroup_mutex and * cgroup_root_mutex. Readers can acquire either of the two. This is to * break the following locking order cycle. * * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem * B. namespace_sem -> cgroup_mutex * * B happens only through cgroup_show_options() and using cgroup_root_mutex * breaks it. */ #ifdef CONFIG_PROVE_RCU DEFINE_MUTEX(cgroup_mutex); EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ #else static DEFINE_MUTEX(cgroup_mutex); #endif static DEFINE_MUTEX(cgroup_root_mutex); /* * Generate an array of cgroup subsystem pointers. At boot time, this is * populated with the built in subsystems, and modular subsystems are * registered after that. The mutable section of this array is protected by * cgroup_mutex. */ #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { #include <linux/cgroup_subsys.h> }; /* * The dummy hierarchy, reserved for the subsystems that are otherwise * unattached - it never has more than a single cgroup, and all tasks are * part of that cgroup. */ static struct cgroupfs_root cgroup_dummy_root; /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; /* * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. */ struct cfent { struct list_head node; struct dentry *dentry; struct cftype *type; /* file xattrs */ struct simple_xattrs xattrs; }; /* * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when * cgroup_subsys->use_id != 0. */ #define CSS_ID_MAX (65535) struct css_id { /* * The css to which this ID points. This pointer is set to valid value * after cgroup is populated. If cgroup is removed, this will be NULL. * This pointer is expected to be RCU-safe because destroy() * is called after synchronize_rcu(). But for safe use, css_tryget() * should be used for avoiding race. */ struct cgroup_subsys_state __rcu *css; /* * ID of this css. */ unsigned short id; /* * Depth in hierarchy which this ID belongs to. */ unsigned short depth; /* * ID is freed by RCU. (and lookup routine is RCU safe.) */ struct rcu_head rcu_head; /* * Hierarchy of CSS ID belongs to. */ unsigned short stack[0]; /* Array of Length (depth+1) */ }; /* * cgroup_event represents events which userspace want to receive. */ struct cgroup_event { /* * Cgroup which the event belongs to. */ struct cgroup *cgrp; /* * Control file which the event associated. */ struct cftype *cft; /* * eventfd to signal userspace about the event. */ struct eventfd_ctx *eventfd; /* * Each of these stored in a list by the cgroup. */ struct list_head list; /* * All fields below needed to unregister event when * userspace closes eventfd. */ poll_table pt; wait_queue_head_t *wqh; wait_queue_t wait; struct work_struct remove; }; /* The list of hierarchy roots */ static LIST_HEAD(cgroup_roots); static int cgroup_root_count; /* * Hierarchy ID allocation and mapping. It follows the same exclusion * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for * writes, either for reads. */ static DEFINE_IDR(cgroup_hierarchy_idr); static struct cgroup_name root_cgroup_name = { .name = "/" }; /* * Assign a monotonically increasing serial number to cgroups. It * guarantees cgroups with bigger numbers are newer than those with smaller * numbers. Also, as cgroups are always appended to the parent's * ->children list, it guarantees that sibling cgroups are always sorted in * the ascending serial number order on the list. Protected by * cgroup_mutex. */ static u64 cgroup_serial_nr_next = 1; /* This flag indicates whether tasks in the fork and exit paths should * check for fork/exit handlers to call. This avoids us having to do * extra work in the fork/exit path if none of the subsystems need to * be called. */ static int need_forkexit_callback __read_mostly; static void cgroup_offline_fn(struct work_struct *work); static int cgroup_destroy_locked(struct cgroup *cgrp); static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, struct cftype cfts[], bool is_add); /* convenient tests for these bits */ static inline bool cgroup_is_dead(const struct cgroup *cgrp) { return test_bit(CGRP_DEAD, &cgrp->flags); } /** * cgroup_is_descendant - test ancestry * @cgrp: the cgroup to be tested * @ancestor: possible ancestor of @cgrp * * Test whether @cgrp is a descendant of @ancestor. It also returns %true * if @cgrp == @ancestor. This function is safe to call as long as @cgrp * and @ancestor are accessible. */ bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) { while (cgrp) { if (cgrp == ancestor) return true; cgrp = cgrp->parent; } return false; } EXPORT_SYMBOL_GPL(cgroup_is_descendant); static int cgroup_is_releasable(const struct cgroup *cgrp) { const int bits = (1 << CGRP_RELEASABLE) | (1 << CGRP_NOTIFY_ON_RELEASE); return (cgrp->flags & bits) == bits; } static int notify_on_release(const struct cgroup *cgrp) { return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); } /** * for_each_subsys - iterate all loaded cgroup subsystems * @ss: the iteration cursor * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end * * Should be called under cgroup_mutex. */ #define for_each_subsys(ss, i) \ for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ if (({ lockdep_assert_held(&cgroup_mutex); \ !((ss) = cgroup_subsys[i]); })) { } \ else /** * for_each_builtin_subsys - iterate all built-in cgroup subsystems * @ss: the iteration cursor * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end * * Bulit-in subsystems are always present and iteration itself doesn't * require any synchronization. */ #define for_each_builtin_subsys(ss, i) \ for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ (((ss) = cgroup_subsys[i]) || true); (i)++) /* iterate each subsystem attached to a hierarchy */ #define for_each_root_subsys(root, ss) \ list_for_each_entry((ss), &(root)->subsys_list, sibling) /* iterate across the active hierarchies */ #define for_each_active_root(root) \ list_for_each_entry((root), &cgroup_roots, root_list) static inline struct cgroup *__d_cgrp(struct dentry *dentry) { return dentry->d_fsdata; } static inline struct cfent *__d_cfe(struct dentry *dentry) { return dentry->d_fsdata; } static inline struct cftype *__d_cft(struct dentry *dentry) { return __d_cfe(dentry)->type; } /** * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. * @cgrp: the cgroup to be checked for liveness * * On success, returns true; the mutex should be later unlocked. On * failure returns false with no lock held. */ static bool cgroup_lock_live_group(struct cgroup *cgrp) { mutex_lock(&cgroup_mutex); if (cgroup_is_dead(cgrp)) { mutex_unlock(&cgroup_mutex); return false; } return true; } /* the list of cgroups eligible for automatic release. Protected by * release_list_lock */ static LIST_HEAD(release_list); static DEFINE_RAW_SPINLOCK(release_list_lock); static void cgroup_release_agent(struct work_struct *work); static DECLARE_WORK(release_agent_work, cgroup_release_agent); static void check_for_release(struct cgroup *cgrp); /* * A cgroup can be associated with multiple css_sets as different tasks may * belong to different cgroups on different hierarchies. In the other * direction, a css_set is naturally associated with multiple cgroups. * This M:N relationship is represented by the following link structure * which exists for each association and allows traversing the associations * from both sides. */ struct cgrp_cset_link { /* the cgroup and css_set this link associates */ struct cgroup *cgrp; struct css_set *cset; /* list of cgrp_cset_links anchored at cgrp->cset_links */ struct list_head cset_link; /* list of cgrp_cset_links anchored at css_set->cgrp_links */ struct list_head cgrp_link; }; /* The default css_set - used by init and its children prior to any * hierarchies being mounted. It contains a pointer to the root state * for each subsystem. Also used to anchor the list of css_sets. Not * reference-counted, to improve performance when child cgroups * haven't been created. */ static struct css_set init_css_set; static struct cgrp_cset_link init_cgrp_cset_link; static int cgroup_init_idr(struct cgroup_subsys *ss, struct cgroup_subsys_state *css); /* css_set_lock protects the list of css_set objects, and the * chain of tasks off each css_set. Nests outside task->alloc_lock * due to cgroup_iter_start() */ static DEFINE_RWLOCK(css_set_lock); static int css_set_count; /* * hash table for cgroup groups. This improves the performance to find * an existing css_set. This hash doesn't (currently) take into * account cgroups in empty hierarchies. */ #define CSS_SET_HASH_BITS 7 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) { unsigned long key = 0UL; struct cgroup_subsys *ss; int i; for_each_subsys(ss, i) key += (unsigned long)css[i]; key = (key >> 16) ^ key; return key; } /* We don't maintain the lists running through each css_set to its * task until after the first call to cgroup_iter_start(). This * reduces the fork()/exit() overhead for people who have cgroups * compiled into their kernel but not actually in use */ static int use_task_css_set_links __read_mostly; static void __put_css_set(struct css_set *cset, int taskexit) { struct cgrp_cset_link *link, *tmp_link; /* * Ensure that the refcount doesn't hit zero while any readers * can see it. Similar to atomic_dec_and_lock(), but for an * rwlock */ if (atomic_add_unless(&cset->refcount, -1, 1)) return; write_lock(&css_set_lock); if (!atomic_dec_and_test(&cset->refcount)) { write_unlock(&css_set_lock); return; } /* This css_set is dead. unlink it and release cgroup refcounts */ hash_del(&cset->hlist); css_set_count--; list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { struct cgroup *cgrp = link->cgrp; list_del(&link->cset_link); list_del(&link->cgrp_link); /* @cgrp can't go away while we're holding css_set_lock */ if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { if (taskexit) set_bit(CGRP_RELEASABLE, &cgrp->flags); check_for_release(cgrp); } kfree(link); } write_unlock(&css_set_lock); kfree_rcu(cset, rcu_head); } /* * refcounted get/put for css_set objects */ static inline void get_css_set(struct css_set *cset) { atomic_inc(&cset->refcount); } static inline void put_css_set(struct css_set *cset) { __put_css_set(cset, 0); } static inline void put_css_set_taskexit(struct css_set *cset) { __put_css_set(cset, 1); } /** * compare_css_sets - helper function for find_existing_css_set(). * @cset: candidate css_set being tested * @old_cset: existing css_set for a task * @new_cgrp: cgroup that's being entered by the task * @template: desired set of css pointers in css_set (pre-calculated) * * Returns true if "cg" matches "old_cg" except for the hierarchy * which "new_cgrp" belongs to, for which it should match "new_cgrp". */ static bool compare_css_sets(struct css_set *cset, struct css_set *old_cset, struct cgroup *new_cgrp, struct cgroup_subsys_state *template[]) { struct list_head *l1, *l2; if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { /* Not all subsystems matched */ return false; } /* * Compare cgroup pointers in order to distinguish between * different cgroups in heirarchies with no subsystems. We * could get by with just this check alone (and skip the * memcmp above) but on most setups the memcmp check will * avoid the need for this more expensive check on almost all * candidates. */ l1 = &cset->cgrp_links; l2 = &old_cset->cgrp_links; while (1) { struct cgrp_cset_link *link1, *link2; struct cgroup *cgrp1, *cgrp2; l1 = l1->next; l2 = l2->next; /* See if we reached the end - both lists are equal length. */ if (l1 == &cset->cgrp_links) {