5636 lines · 147.9 KB · C
RawDownload
/* *  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/fs.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_proc */#include <linux/kthread.h> #include <linux/atomic.h> /* css deactivation bias, makes css->refcnt negative to deny new trygets */#define CSS_DEACT_BIAS		INT_MIN /* * 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. */static DEFINE_MUTEX(cgroup_mutex);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 *subsys[CGROUP_SUBSYS_COUNT] = {#include <linux/cgroup_subsys.h>}; #define MAX_CGROUP_ROOT_NAMELEN 64 /* * A cgroupfs_root represents the root of a cgroup hierarchy, * and may be associated with a superblock to form an active * hierarchy */struct cgroupfs_root {	struct super_block *sb; 	/*	 * The bitmask of subsystems intended to be attached to this	 * hierarchy	 */	unsigned long subsys_mask; 	/* Unique id for this hierarchy. */	int hierarchy_id; 	/* The bitmask of subsystems currently attached to this hierarchy */	unsigned long actual_subsys_mask; 	/* A list running through the attached subsystems */	struct list_head subsys_list; 	/* The root cgroup for this hierarchy */	struct cgroup top_cgroup; 	/* Tracks how many cgroups are currently defined in hierarchy.*/	int number_of_cgroups; 	/* A list running through the active hierarchies */	struct list_head root_list; 	/* All cgroups on this root, cgroup_mutex protected */	struct list_head allcg_list; 	/* Hierarchy-specific flags */	unsigned long flags; 	/* IDs for cgroups in this hierarchy */	struct ida cgroup_ida; 	/* The path to use for release notifications. */	char release_agent_path[PATH_MAX]; 	/* The name for this hierarchy - may be empty */	char name[MAX_CGROUP_ROOT_NAMELEN];}; /* * The "rootnode" hierarchy is 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 rootnode; /* * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. */struct cfent {	struct list_head		node;	struct dentry			*dentry;	struct cftype			*type;}; /* * 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(roots);static int root_count; static DEFINE_IDA(hierarchy_ida);static int next_hierarchy_id;static DEFINE_SPINLOCK(hierarchy_id_lock); /* dummytop is a shorthand for the dummy hierarchy's top cgroup */#define dummytop (&rootnode.top_cgroup) /* 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 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); #ifdef CONFIG_PROVE_LOCKINGint cgroup_lock_is_held(void){	return lockdep_is_held(&cgroup_mutex);}#else /* #ifdef CONFIG_PROVE_LOCKING */int cgroup_lock_is_held(void){	return mutex_is_locked(&cgroup_mutex);}#endif /* #else #ifdef CONFIG_PROVE_LOCKING */ EXPORT_SYMBOL_GPL(cgroup_lock_is_held); static int css_unbias_refcnt(int refcnt){	return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;} /* the current nr of refs, always >= 0 whether @css is deactivated or not */static int css_refcnt(struct cgroup_subsys_state *css){	int v = atomic_read(&css->refcnt); 	return css_unbias_refcnt(v);} /* convenient tests for these bits */inline int cgroup_is_removed(const struct cgroup *cgrp){	return test_bit(CGRP_REMOVED, &cgrp->flags);} /* bits in struct cgroupfs_root flags field */enum {	ROOT_NOPREFIX,	/* mounted subsystems have no named prefix */	ROOT_XATTR,	/* supports extended attributes */}; 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() allows you to iterate on each subsystem attached to * an active hierarchy */#define for_each_subsys(_root, _ss) \list_for_each_entry(_ss, &_root->subsys_list, sibling) /* for_each_active_root() allows you to iterate across the active hierarchies */#define for_each_active_root(_root) \list_for_each_entry(_root, &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;} /* 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); /* Link structure for associating css_set objects with cgroups */struct cg_cgroup_link {	/*	 * List running through cg_cgroup_links associated with a	 * cgroup, anchored on cgroup->css_sets	 */	struct list_head cgrp_link_list;	struct cgroup *cgrp;	/*	 * List running through cg_cgroup_links pointing at a	 * single css_set object, anchored on css_set->cg_links	 */	struct list_head cg_link_list;	struct css_set *cg;}; /* 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 cg_cgroup_link init_css_set_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[]){	int i;	unsigned long key = 0UL; 	for (i = 0; i < CGROUP_SUBSYS_COUNT; 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 *cg, int taskexit){	struct cg_cgroup_link *link;	struct cg_cgroup_link *saved_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(&cg->refcount, -1, 1))		return;	write_lock(&css_set_lock);	if (!atomic_dec_and_test(&cg->refcount)) {		write_unlock(&css_set_lock);		return;	} 	/* This css_set is dead. unlink it and release cgroup refcounts */	hash_del(&cg->hlist);	css_set_count--; 	list_for_each_entry_safe(link, saved_link, &cg->cg_links,				 cg_link_list) {		struct cgroup *cgrp = link->cgrp;		list_del(&link->cg_link_list);		list_del(&link->cgrp_link_list); 		/*		 * We may not be holding cgroup_mutex, and if cgrp->count is		 * dropped to 0 the cgroup can be destroyed at any time, hence		 * rcu_read_lock is used to keep it alive.		 */		rcu_read_lock();		if (atomic_dec_and_test(&cgrp->count) &&		    notify_on_release(cgrp)) {			if (taskexit)				set_bit(CGRP_RELEASABLE, &cgrp->flags);			check_for_release(cgrp);		}		rcu_read_unlock(); 		kfree(link);	} 	write_unlock(&css_set_lock);	kfree_rcu(cg, rcu_head);} /* * refcounted get/put for css_set objects */static inline void get_css_set(struct css_set *cg){	atomic_inc(&cg->refcount);} static inline void put_css_set(struct css_set *cg){	__put_css_set(cg, 0);} static inline void put_css_set_taskexit(struct css_set *cg){	__put_css_set(cg, 1);} /* * compare_css_sets - helper function for find_existing_css_set(). * @cg: candidate css_set being tested * @old_cg: 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 *cg,			     struct css_set *old_cg,			     struct cgroup *new_cgrp,			     struct cgroup_subsys_state *template[]){	struct list_head *l1, *l2; 	if (memcmp(template, cg->subsys, sizeof(cg->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 = &cg->cg_links;	l2 = &old_cg->cg_links;	while (1) {		struct cg_cgroup_link *cgl1, *cgl2;		struct cgroup *cg1, *cg2;

Lines 1–500 of 5636.