/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* TODO: replace with more sophisticated array */ #include #include #include /* used in cgroup_attach_proc */ #include static DEFINE_MUTEX(cgroup_mutex); /* * Generate an array of cgroup subsystem pointers. At boot time, this is * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are * registered after that. The mutable section of this array is protected by * cgroup_mutex. */ #define SUBSYS(_x) &_x ## _subsys, static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { #include }; #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_bits; /* Unique id for this hierarchy. */ int hierarchy_id; /* The bitmask of subsystems currently attached to this hierarchy */ unsigned long actual_subsys_bits; /* 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; /* Hierarchy-specific flags */ unsigned long flags; /* 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; /* * 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_is_removed() * 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; #ifdef CONFIG_PROVE_LOCKING int 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); /* 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 */ }; 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); } static int clone_children(const struct cgroup *cgrp) { return test_bit(CGRP_CLONE_CHILDREN, &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) /* the list of cgroups eligible for automatic release. Protected by * release_list_lock */ static LIST_HEAD(release_list); static DEFINE_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 7 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS) static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE]; static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[]) { int i; int index; unsigned long tmp = 0UL; for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) tmp += (unsigned long)css[i]; tmp = (tmp >> 16) ^ tmp; index = hash_long(tmp, CSS_SET_HASH_BITS); return &css_set_table[index]; } /* 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 */ hlist_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); if (atomic_dec_and_test(&cgrp->count) && 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(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; l1 = l1->next; l2 = l2->next; /* See if we reached the end - both lists are equal length. */ if (l1 == &cg->cg_links) { BUG_ON(l2 != &old_cg->cg_links); break; } else { BUG_ON(l2 == &old_cg->cg_links); } /* Locate the cgroups associated with these links. */ cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); cg1 = cgl1->cgrp; cg2 = cgl2->cgrp; /* Hierarchies should be linked in the same order. */ BUG_ON(cg1->root != cg2->root); /* * If this hierarchy is the hierarchy of the cgroup * that's changing, then we need to check that this * css_set points to the new cgroup; if it's any other * hierarchy, then this css_set should point to the * same cgroup as the old css_set. */ if (cg1->root == new_cgrp->root) { if (cg1 != new_cgrp) return false; } else { if (cg1 != cg2) return false; } } return true; } /* * find_existing_css_set() is a helper for * find_css_set(), and checks to see whether an existing * css_set is suitable. * * oldcg: the cgroup group that we're using before the cgroup * transition * * cgrp: the cgroup that we're moving into * * template: location in which to build the desired set of subsystem * state objects for the new cgroup group */ static struct css_set *find_existing_css_set( struct css_set *oldcg, struct cgroup *cgrp, struct cgroup_subsys_state *template[]) { int i; struct cgroupfs_root *root = cgrp->root; struct hlist_head *hhead; struct hlist_node *node; struct css_set *cg; /* * Build the set of subsystem state objects that we want to see in the * new css_set. while subsystems can change globally, the entries here * won't change, so no need for locking. */ for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { if (root->subsys_bits & (1UL << i)) { /* Subsystem is in this hierarchy. So we want * the subsystem state from the new * cgroup */ template[i] = cgrp->subsys[i]; } else { /* Subsystem is not in this hierarchy, so we * don't want to change the subsystem state */ template[i] = oldcg->subsys[i]; } } hhead = css_set_hash(template); hlist_for_each_entry(cg, node, hhead, hlist) { if (!compare_css_sets(cg, oldcg, cgrp, template)) continue; /* This css_set matches what we need */ return cg; } /* No existing cgroup group matched */ return NULL; } static void free_cg_links(struct list_head *tmp) { struct cg_cgroup_link *link; struct cg_cgroup_link *saved_link; list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { list_del(&link->cgrp_link_list); kfree(link); } } /* * allocate_cg_links() allocates "count" cg_cgroup_link structures * and chains them on tmp through their cgrp_link_list fields. Returns 0 on * success or a negative error */ static int allocate_cg_links(int count, struct list_head *tmp) { struct cg_cgroup_link *link; int i; INIT_LIST_HEAD(tmp); for (i = 0; i < count; i++) { link = kmalloc(sizeof(*link), GFP_KERNEL); if (!link) { free_cg_links(tmp); return -ENOMEM; } list_add(&link->cgrp_link_list, tmp); } return 0; } /** * link_css_set - a helper function to link a css_set to a cgroup * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() * @cg: the css_set to be linked * @cgrp: the destination cgroup */ static void link_css_set(struct list_head *tmp_cg_links, struct css_set *cg, struct cgroup *cgrp) { struct cg_cgroup_link *link; BUG_ON(list_empty(tmp_cg_links)); link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, cgrp_link_list); link->cg = cg; link->cgrp = cgrp; atomic_inc(&cgrp->count); list_move(&link->cgrp_link_list, &cgrp->css_sets); /* * Always add links to the tail of the list so that the list * is sorted by order of hierarchy creation */ list_add_tail(&link->cg_link_list, &cg->cg_links); } /* * find_css_set() takes an existing cgroup group and a * cgroup object, and returns a css_set object that's * equivalent to the old group, but with the given cgroup * substituted into the appropriate hierarchy. Must be called with * cgroup_mutex held */ static struct css_set *find_css_set( struct css_set *oldcg, struct cgroup *cgrp) { struct css_set *res; struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; struct list_head tmp_cg_links; struct hlist_head *hhead; struct cg_cgroup_link *link; /* First see if we already have a cgroup group that matches * the desired set */ read_lock(&css_set_lock); res = find_existing_css_set(oldcg, cgrp, template); if (res) get_css_set(res); read_unlock(&css_set_lock); if (res) return res; res = kmalloc(sizeof(*res), GFP_KERNEL); if (!res) return NULL; /* Allocate all the cg_cgroup_link objects that we'll need */ if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { kfree(res); return NULL; } atomic_set(&res->refcount, 1); INIT_LIST_HEAD(&res->cg_links); INIT_LIST_HEAD(&res->tasks); INIT_HLIST_NODE(&res->hlist); /* Copy the set of subsystem state objects generated in * find_existing_css_set() */ memcpy(res->subsys, template, sizeof(res->subsys)); write_lock(&css_set_lock); /* Add reference counts and links from the new css_set. */ list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { struct cgroup *c = link->cgrp; if (c->root == cgrp->root) c = cgrp; link_css_set(&tmp_cg_links, res, c); } BUG_ON(!list_empty(&tmp_cg_links)); css_set_count++; /* Add this cgroup group to the hash table */ hhead = css_set_hash(res->subsys); hlist_add_head(&res->hlist, hhead); write_unlock(&css_set_lock); return res; } /* * Return the cgroup for "task" from the given hierarchy. Must be * called with cgroup_mutex held. */ static struct cgroup *task_cgroup_from_root(struct task_struct *task, struct cgroupfs_root *root) { struct css_set *css; struct cgroup *res = NULL; BUG_ON(!mutex_is_locked(&cgroup_mutex)); read_lock(&css_set_lock); /* * No need to lock the task - since we hold cgroup_mutex the * task can't change groups, so the only thing that can happen * is that it exits and its css is set back to init_css_set. */ css = task->cgroups; if (css == &init_css_set) { res = &root->top_cgroup; } else { struct cg_cgroup_link *link; list_for_each_entry(link, &css->cg_links, cg_link_list) { struct cgroup *c = link->cgrp; if (c->root == root) { res = c; break; } } } read_unlock(&css_set_lock); BUG_ON(!res); return res; } /* * There is one global cgroup mutex. We also require taking * task_lock() when dereferencing a task's cgroup subsys pointers. * See "The task_lock() exception", at the end of this comment. * * A task must hold cgroup_mutex to modify cgroups. * * Any task can increment and decrement the count field without lock. * So in general, code holding cgroup_mutex can't rely on the count * field not changing. However, if the count goes to zero, then only * cgroup_attach_task() can increment it again. Because a count of zero * means that no tasks are currently attached, therefore there is no * way a task attached to that cgroup can fork (the other way to * increment the count). So code holding cgroup_mutex can safely * assume that if the count is zero, it will stay zero. Similarly, if * a task holds cgroup_mutex on a cgroup with zero count, it * knows that the cgroup won't be removed, as cgroup_rmdir() * needs that mutex. * * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't * (usually) take cgroup_mutex. These are the two most performance * critical pieces of code here. The exception occurs on cgroup_exit(), * when a task in a notify_on_release cgroup exits. Then cgroup_mutex * is taken, and if the cgroup count is zero, a usermode call made * to the release agent with the name of the cgroup (path relative to * the root of cgroup file system) as the argument. * * A cgroup can only be deleted if both its 'count' of using tasks * is zero, and its list of 'children' cgroups is empty. Since all * tasks in the system use _some_ cgroup, and since there is always at * least one task in the system (init, pid == 1), therefore, top_cgroup * always has either children cgroups and/or using tasks. So we don't * need a special hack to ensure that top_cgroup cannot be deleted. * * The task_lock() exception * * The need for this exception arises from the action of * cgroup_attach_task(), which overwrites one tasks cgroup pointer with * another. It does so using cgroup_mutex, however there are * several performance critical places that need to reference * task->cgroup without the expense of grabbing a system global * mutex. Therefore except as noted below, when dereferencing or, as * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use * task_lock(), which acts on a spinlock (task->alloc_lock) already in * the task_struct routinely used for such matters. * * P.S. One more locking exception. RCU is used to guard the * update of a tasks cgroup pointer by cgroup_attach_task() */ /** * cgroup_lock - lock out any changes to cgroup structures * */ void cgroup_lock(void) { mutex_lock(&cgroup_mutex); } EXPORT_SYMBOL_GPL(cgroup_lock); /** * cgroup_unlock - release lock on cgroup changes * * Undo the lock taken in a previous cgroup_lock() call. */ void cgroup_unlock(void) { mutex_unlock(&cgroup_mutex); } EXPORT_SYMBOL_GPL(cgroup_unlock); /* * A couple of forward declarations required, due to cyclic reference loop: * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations * -> cgroup_mkdir. */ static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode); static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *); static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); static int cgroup_populate_dir(struct cgroup *cgrp); static const struct inode_operations cgroup_dir_inode_operations; static const struct file_operations proc_cgroupstats_operations; static struct backing_dev_info cgroup_backing_dev_info = { .name = "cgroup", .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, }; static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, struct cgroup *child); static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb) { struct inode *inode = new_inode(sb); if (inode) { inode->i_ino = get_next_ino(); inode->i_mode = mode; inode->i_uid = current_fsuid(); inode->i_gid = current_fsgid(); inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; } return inode; } /* * Call subsys's pre_destroy handler. * This is called before css refcnt check. */ static int cgroup_call_pre_destroy(struct cgroup *cgrp) { struct cgroup_subsys *ss; int ret = 0; for_each_subsys(cgrp->root, ss) if (ss->pre_destroy) { ret = ss->pre_destroy(ss, cgrp); if (ret) break; } return ret; } static void cgroup_diput(struct dentry *dentry, struct inode *inode) { /* is dentry a directory ? if so, kfree() associated cgroup */ if (S_ISDIR(inode->i_mode)) { struct cgroup *cgrp = dentry->d_fsdata; struct cgroup_subsys *ss; BUG_ON(!(cgroup_is_removed(cgrp))); /* It's possible for external users to be holding css * reference counts on a cgroup; css_put() needs to * be able to access the cgroup after decrementing * the reference count in order to know if it needs to * queue the cgroup to be handled by the release * agent */ synchronize_rcu(); mutex_lock(&cgroup_mutex); /* * Release the subsystem state objects. */ for_each_subsys(cgrp->root, ss) ss->destroy(ss, cgrp); cgrp->root->number_of_cgroups--; mutex_unlock(&cgroup_mutex); /* * Drop the active superblock reference that we took when we * created the cgroup */ deactivate_super(cgrp->root->sb); /* * if we're getting rid of the cgroup, refcount should ensure * that there are no pidlists left. */ BUG_ON(!list_empty(&cgrp->pidlists)); kfree_rcu(cgrp, rcu_head); } iput(inode); } static int cgroup_delete(const struct dentry *d) { return 1; } static void remove_dir(struct dentry *d) { struct dentry *parent = dget(d->d_parent); d_delete(d); simple_rmdir(parent->d_inode, d); dput(parent); } static void cgroup_clear_directory(struct dentry *dentry) { struct list_head *node; BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); spin_lock(&dentry->d_lock); node = dentry->d_subdirs.next; while (node != &dentry->d_subdirs) { struct dentry *d = list_entry(node, struct dentry, d_u.d_child); spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); list_del_init(node); if (d->d_inode) { /* This should never be called on a cgroup * directory with child cgroups */ BUG_ON(d->d_inode->i_mode & S_IFDIR); dget_dlock(d); spin_unlock(&d->d_lock); spin_unlock(&dentry->d_lock); d_delete(d); simple_unlink(dentry->d_inode, d); dput(d); spin_lock(&dentry->d_lock); } else spin_unlock(&d->d_lock); node = dentry->d_subdirs.next; } spin_unlock(&dentry->d_lock); } /* * NOTE : the dentry must have been dget()'ed */ static void cgroup_d_remove_dir(struct dentry *dentry) { struct dentry *parent; cgroup_clear_directory(dentry); parent = dentry->d_parent; spin_lock(&parent->d_lock); spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); list_del_init(&dentry->d_u.d_child); spin_unlock(&dentry->d_lock); spin_unlock(&parent->d_lock); remove_dir(dentry); } /* * A queue for waiters to do rmdir() cgroup. A tasks will sleep when * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some * reference to css->refcnt. In general, this refcnt is expected to goes down * to zero, soon. * * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex; */ DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq); static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp) { if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))) wake_up_all(&cgroup_rmdir_waitq); } void cgroup_exclude_rmdir(struct cgroup_subsys_state *css) { css_get(css); } void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css) { cgroup_wakeup_rmdir_waiter(css->cgroup); css_put(css); } /* * Call with cgroup_mutex held. Drops reference counts on modules, including * any duplicate ones that parse_cgroupfs_options took. If this function * returns an error, no reference counts are touched. */ static int rebind_subsystems(struct cgroupfs_root *root, unsigned long final_bits) { unsigned long added_bits, removed_bits; struct cgroup *cgrp = &root->top_cgroup; int i; BUG_ON(!mutex_is_locked(&cgroup_mutex)); removed_bits = root->actual_subsys_bits & ~final_bits; added_bits = final_bits & ~root->actual_subsys_bits; /* Check that any added subsystems are currently free */ for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { unsigned long bit = 1UL << i; struct cgroup_subsys *ss = subsys[i]; if (!(bit & added_bits)) continue; /* * Nobody should tell us to do a subsys that doesn't exist: * parse_cgroupfs_options should catch that case and refcounts * ensure that subsystems won't disappear once selected. */ BUG_ON(ss == NULL); if (ss->root != &rootnode) { /* Subsystem isn't free */ return -EBUSY; } } /* Currently we don't handle adding/removing subsystems when * any child cgroups exist. This is theoretically supportable * but involves complex error handling, so it's being left until * later */ if (root->number_of_cgroups > 1) return -EBUSY; /* Process each subsystem */ for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { struct cgroup_subsys *ss = subsys[i]; unsigned long bit = 1UL << i; if (bit & added_bits) { /* We're binding this subsystem to this hierarchy */ BUG_ON(ss == NULL); BUG_ON(cgrp->subsys[i]); BUG_ON(!dummytop->subsys[i]); BUG_ON(dummytop->subsys[i]->cgroup != dummytop); mutex_lock(&ss->hierarchy_mutex); cgrp->subsys[i] = dummytop->subsys[i]; cgrp->subsys[i]->cgroup = cgrp; list_move(&ss->sibling, &root->subsys_list); ss->root = root; if (ss->bind) ss->bind(ss, cgrp); mutex_unlock(&ss->hierarchy_mutex); /* refcount was already taken, and we're keeping it */ } else if (bit & removed_bits) { /* We're removing this subsystem */ BUG_ON(ss == NULL); BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); BUG_ON(cgrp->subsys[i]->cgroup != cgrp); mutex_lock(&ss->hierarchy_mutex); if (ss->bind) ss->bind(ss, dummytop); dummytop->subsys[i]->cgroup = dummytop; cgrp->subsys[i] = NULL; subsys[i]->root = &rootnode; list_move(&ss->sibling, &rootnode.subsys_list); mutex_unlock(&ss->hierarchy_mutex); /* subsystem is now free - drop reference on module */ module_put(ss->module); } else if (bit & final_bits) { /* Subsystem state should already exist */ BUG_ON(ss == NULL); BUG_ON(!cgrp->subsys[i]); /* * a refcount was taken, but we already had one, so * drop the extra reference. */ module_put(ss->module); #ifdef CONFIG_MODULE_UNLOAD BUG_ON(ss->module && !module_refcount(ss->module)); #endif } else { /* Subsystem state shouldn't exist */ BUG_ON(cgrp->subsys[i]); } } root->subsys_bits = root->actual_subsys_bits = final_bits; synchronize_rcu(); return 0; } static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs) { struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info; struct cgroup_subsys *ss; mutex_lock(&cgroup_mutex); for_each_subsys(root, ss) seq_printf(seq, ",%s", ss->name); if (test_bit(ROOT_NOPREFIX, &root->flags)) seq_puts(seq, ",noprefix"); if (strlen(root->release_agent_path)) seq_printf(seq, ",release_agent=%s", root->release_agent_path); if (clone_children(&root->top_cgroup)) seq_puts(seq, ",clone_children"); if (strlen(root->name)) seq_printf(seq, ",name=%s", root->name); mutex_unlock(&cgroup_mutex); return 0; } struct cgroup_sb_opts { unsigned long subsys_bits; unsigned long flags; char *release_agent; bool clone_children; char *name; /* User explicitly requested empty subsystem */ bool none; struct cgroupfs_root *new_root; }; /* * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call * with cgroup_mutex held to protect the subsys[] array. This function takes * refcounts on subsystems to be used, unless it returns error, in which case * no refcounts are taken. */ static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) { char *token, *o = data; bool all_ss = false, one_ss = false; unsigned long mask = (unsigned long)-1; int i; bool module_pin_failed = false; BUG_ON(!mutex_is_locked(&cgroup_mutex)); #ifdef CONFIG_CPUSETS mask = ~(1UL << cpuset_subsys_id); #endif memset(opts, 0, sizeof(*opts)); while ((token = strsep(&o, ",")) != NULL) { if (!*token) return -EINVAL; if (!strcmp(token, "none")) { /* Explicitly have no subsystems */ opts->none = true; continue; } if (!strcmp(token, "all")) { /* Mutually exclusive option 'all' + subsystem name */ if (one_ss) return -EINVAL; all_ss = true; continue; } if (!strcmp(token, "noprefix")) { set_bit(ROOT_NOPREFIX, &opts->flags); continue; } if (!strcmp(token, "clone_children")) { opts->clone_children = true; continue; } if (!strncmp(token, "release_agent=", 14)) { /* Specifying two release agents is forbidden */ if (opts->release_agent) return -EINVAL; opts->release_agent = kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); if (!opts->release_agent) return -ENOMEM; continue; } if (!strncmp(token, "name=", 5)) { const char *name = token + 5; /* Can't specify an empty name */ if (!strlen(name)) return -EINVAL; /* Must match [\w.-]+ */ for (i = 0; i < strlen(name); i++) { char c = name[i]; if (isalnum(c)) continue; if ((c == '.') || (c == '-') || (c == '_')) continue; return -EINVAL; } /* Specifying two names is forbidden */ if (opts->name) return -EINVAL; opts->name = kstrndup(name, MAX_CGROUP_ROOT_NAMELEN - 1, GFP_KERNEL); if (!opts->name) return -ENOMEM; continue; } for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { struct cgroup_subsys *ss = subsys[i]; if (ss == NULL) continue; if (strcmp(token, ss->name)) continue; if (ss->disabled) continue; /* Mutually exclusive option 'all' + subsystem name */ if (all_ss) return -EINVAL; set_bit(i, &opts->subsys_bits); one_ss = true; break; } if (i == CGROUP_SUBSYS_COUNT) return -ENOENT; } /* * If the 'all' option was specified select all the subsystems, * otherwise 'all, 'none' and a subsystem name options were not * specified, let's default to 'all' */ if (all_ss || (!all_ss && !one_ss && !opts->none)) { for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { struct cgroup_subsys *ss = subsys[i]; if (ss == NULL) continue; if (ss->disabled) continue; set_bit(i, &opts->subsys_bits); } } /* Consistency checks */ /* * Option noprefix was introduced just for backward compatibility * with the old cpuset, so we allow noprefix only if mounting just * the cpuset subsystem. */ if (test_bit(ROOT_NOPREFIX, &opts->flags) && (opts->subsys_bits & mask)) return -EINVAL; /* Can't specify "none" and some subsystems */ if (opts->subsys_bits && opts->none) return -EINVAL; /* * We either have to specify by name or by subsystems. (So all * empty hierarchies must have a name). */ if (!opts->subsys_bits && !opts->name) return -EINVAL; /* * Grab references on all the modules we'll need, so the subsystems * don't dance around before rebind_subsystems attaches them. This may * take duplicate reference counts on a subsystem that's already used, * but rebind_subsystems handles this case. */ for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) { unsigned long bit = 1UL << i; if (!(bit & opts->subsys_bits)) continue; if (!try_module_get(subsys[i]->module)) { module_pin_failed = true; break; } } if (module_pin_failed) { /* * oops, one of the modules was going away. this means that we * raced with a module_delete call, and to the user this is * essentially a "subsystem doesn't exist" case. */ for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) { /* drop refcounts only on the ones we took */ unsigned long bit = 1UL << i; if (!(bit & opts->subsys_bits)) continue; module_put(subsys[i]->module); } return -ENOENT; } return 0; } static void drop_parsed_module_refcounts(unsigned long subsys_bits) { int i; for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) { unsigned long bit = 1UL << i; if (!(bit & subsys_bits)) continue; module_put(subsys[i]->module); } } static int cgroup_remount(struct super_block *sb, int *flags, char *data) { int ret = 0; struct cgroupfs_root *root = sb->s_fs_info; struct cgroup *cgrp = &root->top_cgroup; struct cgroup_sb_opts opts; mutex_lock(&cgrp->dentry->d_inode->i_mutex); mutex_lock(&cgroup_mutex); /* See what subsystems are wanted */ ret = parse_cgroupfs_options(data, &opts); if (ret) goto out_unlock; /* Don't allow flags or name to change at remount */ if (opts.flags != root->flags || (opts.name && strcmp(opts.name, root->name))) { ret = -EINVAL; drop_parsed_module_refcounts(opts.subsys_bits); goto out_unlock; } ret = rebind_subsystems(root, opts.subsys_bits); if (ret) { drop_parsed_module_refcounts(opts.subsys_bits); goto out_unlock; } /* (re)populate subsystem files */ cgroup_populate_dir(cgrp); if (opts.release_agent) strcpy(root->release_agent_path, opts.release_agent); out_unlock: kfree(opts.release_agent); kfree(opts.name); mutex_unlock(&cgroup_mutex); mutex_unlock(&cgrp->dentry->d_inode->i_mutex); return ret; } static const struct super_operations cgroup_ops = { .statfs = simple_statfs, .drop_inode = generic_delete_inode, .show_options = cgroup_show_options, .remount_fs = cgroup_remount, }; static void init_cgroup_housekeeping(struct cgroup *cgrp) { INIT_LIST_HEAD(&cgrp->sibling); INIT_LIST_HEAD(&cgrp->children); INIT_LIST_HEAD(&cgrp->css_sets); INIT_LIST_HEAD(&cgrp->release_list); INIT_LIST_HEAD(&cgrp->pidlists); mutex_init(&cgrp->pidlist_mutex); INIT_LIST_HEAD(&cgrp->event_list); spin_lock_init(&cgrp->event_list_lock); } static void init_cgroup_root(struct cgroupfs_root *root) { struct cgroup *cgrp = &root->top_cgroup; INIT_LIST_HEAD(&root->subsys_list); INIT_LIST_HEAD(&root->root_list); root->number_of_cgroups = 1; cgrp->root = root; cgrp->top_cgroup = cgrp; init_cgroup_housekeeping(cgrp); } static bool init_root_id(struct cgroupfs_root *root) { int ret = 0; do { if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) return false; spin_lock(&hierarchy_id_lock); /* Try to allocate the next unused ID */ ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, &root->hierarchy_id); if (ret == -ENOSPC) /* Try again starting from 0 */ ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); if (!ret) { next_hierarchy_id = root->hierarchy_id + 1; } else if (ret != -EAGAIN) { /* Can only get here if the 31-bit IDR is full ... */ BUG_ON(ret); } spin_unlock(&hierarchy_id_lock); } while (ret); return true; } static int cgroup_test_super(struct super_block *sb, void *data) { struct cgroup_sb_opts *opts = data; struct cgroupfs_root *root = sb->s_fs_info; /* If we asked for a name then it must match */ if (opts->name && strcmp(opts->name, root->name)) return 0; /* * If we asked for subsystems (or explicitly for no * subsystems) then they must match */ if ((opts->subsys_bits || opts->none) && (opts->subsys_bits != root->subsys_bits)) return 0; return 1; } static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) { struct cgroupfs_root *root; if (!opts->subsys_bits && !opts->none) return NULL; root = kzalloc(sizeof(*root), GFP_KERNEL); if (!root) return ERR_PTR(-ENOMEM); if (!init_root_id(root)) { kfree(root); return ERR_PTR(-ENOMEM); } init_cgroup_root(root); root->subsys_bits = opts->subsys_bits; root->flags = opts->flags; if (opts->release_agent) strcpy(root->release_agent_path, opts->release_agent); if (opts->name) strcpy(root->name, opts->name); if (opts->clone_children) set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags); return root; } static void cgroup_drop_root(struct cgroupfs_root *root) { if (!root) return; BUG_ON(!root->hierarchy_id); spin_lock(&hierarchy_id_lock); ida_remove(&hierarchy_ida, root->hierarchy_id); spin_unlock(&hierarchy_id_lock); kfree(root); } static int cgroup_set_super(struct super_block *sb, void *data) { int ret; struct cgroup_sb_opts *opts = data; /* If we don't have a new root, we can't set up a new sb */ if (!opts->new_root) return -EINVAL; BUG_ON(!opts->subsys_bits && !opts->none); ret = set_anon_super(sb, NULL); if (ret) return ret; sb->s_fs_info = opts->new_root; opts->new_root->sb = sb; sb->s_blocksize = PAGE_CACHE_SIZE; sb->s_blocksize_bits = PAGE_CACHE_SHIFT; sb->s_magic = CGROUP_SUPER_MAGIC; sb->s_op = &cgroup_ops; return 0; } static int cgroup_get_rootdir(struct super_block *sb) { static const struct dentry_operations cgroup_dops = { .d_iput = cgroup_diput, .d_delete = cgroup_delete, }; struct inode *inode = cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); struct dentry *dentry; if (!inode) return -ENOMEM; inode->i_fop = &simple_dir_operations; inode->i_op = &cgroup_dir_inode_operations; /* directories start off with i_nlink == 2 (for "." entry) */ inc_nlink(inode); dentry = d_alloc_root(inode); if (!dentry) { iput(inode); return -ENOMEM; } sb->s_root = dentry; /* for everything else we want ->d_op set */ sb->s_d_op = &cgroup_dops; return 0; } static struct dentry *cgroup_mount(struct file_system_type *fs_type, int flags, const char *unused_dev_name, void *data) { struct cgroup_sb_opts opts; struct cgroupfs_root *root; int ret = 0; struct super_block *sb; struct cgroupfs_root *new_root; /* First find the desired set of subsystems */ mutex_lock(&cgroup_mutex); ret = parse_cgroupfs_options(data, &opts); mutex_unlock(&cgroup_mutex); if (ret) goto out_err; /* * Allocate a new cgroup root. We may not need it if we're * reusing an existing hierarchy. */ new_root = cgroup_root_from_opts(&opts); if (IS_ERR(new_root)) { ret = PTR_ERR(new_root); goto drop_modules; } opts.new_root = new_root; /* Locate an existing or new sb for this hierarchy */ sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts); if (IS_ERR(sb)) { ret = PTR_ERR(sb); cgroup_drop_root(opts.new_root); goto drop_modules; } root = sb->s_fs_info; BUG_ON(!root); if (root == opts.new_root) { /* We used the new root structure, so this is a new hierarchy */ struct list_head tmp_cg_links; struct cgroup *root_cgrp = &root->top_cgroup; struct inode *inode; struct cgroupfs_root *existing_root; int i; BUG_ON(sb->s_root != NULL); ret = cgroup_get_rootdir(sb); if (ret) goto drop_new_super; inode = sb->s_root->d_inode; mutex_lock(&inode->i_mutex); mutex_lock(&cgroup_mutex); if (strlen(root->name)) { /* Check for name clashes with existing mounts */ for_each_active_root(existing_root) { if (!strcmp(existing_root->name, root->name)) { ret = -EBUSY; mutex_unlock(&cgroup_mutex); mutex_unlock(&inode->i_mutex); goto drop_new_super; } } } /* * We're accessing css_set_count without locking * css_set_lock here, but that's OK - it can only be * increased by someone holding cgroup_lock, and * that's us. The worst that can happen is that we * have some link structures left over */ ret = allocate_cg_links(css_set_count, &tmp_cg_links); if (ret) { mutex_unlock(&cgroup_mutex); mutex_unlock(&inode->i_mutex); goto drop_new_super; } ret = rebind_subsystems(root, root->subsys_bits); if (ret == -EBUSY) { mutex_unlock(&cgroup_mutex); mutex_unlock(&inode->i_mutex); free_cg_links(&tmp_cg_links); goto drop_new_super; } /* * There must be no failure case after here, since rebinding * takes care of subsystems' refcounts, which are explicitly * dropped in the failure exit path. */ /* EBUSY should be the only error here */ BUG_ON(ret); list_add(&root->root_list, &roots); root_count++; sb->s_root->d_fsdata = root_cgrp; root->top_cgroup.dentry = sb->s_root; /* Link the top cgroup in this hierarchy into all * the css_set objects */ write_lock(&css_set_lock); for (i = 0; i < CSS_SET_TABLE_SIZE; i++) { struct hlist_head *hhead = &css_set_table[i]; struct hlist_node *node; struct css_set *cg; hlist_for_each_entry(cg, node, hhead, hlist) link_css_set(&tmp_cg_links, cg, root_cgrp); } write_unlock(&css_set_lock); free_cg_links(&tmp_cg_links); BUG_ON(!list_empty(&root_cgrp->sibling)); BUG_ON(!list_empty(&root_cgrp->children)); BUG_ON(root->number_of_cgroups != 1); cgroup_populate_dir(root_cgrp); mutex_unlock(&cgroup_mutex); mutex_unlock(&inode->i_mutex); } else { /* * We re-used an existing hierarchy - the new root (if * any) is not needed */ cgroup_drop_root(opts.new_root); /* no subsys rebinding, so refcounts don't change */ drop_parsed_module_refcounts(opts.subsys_bits); } kfree(opts.release_agent); kfree(opts.name); return dget(sb->s_root); drop_new_super: deactivate_locked_super(sb); drop_modules: drop_parsed_module_refcounts(opts.subsys_bits); out_err: kfree(opts.release_agent); kfree(opts.name); return ERR_PTR(ret); } static void cgroup_kill_sb(struct super_block *sb) { struct cgroupfs_root *root = sb->s_fs_info; struct cgroup *cgrp = &root->top_cgroup; int ret; struct cg_cgroup_link *link; struct cg_cgroup_link *saved_link; BUG_ON(!root); BUG_ON(root->number_of_cgroups != 1); BUG_ON(!list_empty(&cgrp->children)); BUG_ON(!list_empty(&cgrp->sibling)); mutex_lock(&cgroup_mutex); /* Rebind all subsystems back to the default hierarchy */ ret = rebind_subsystems(root, 0); /* Shouldn't be able to fail ... */ BUG_ON(ret); /* * Release all the links from css_sets to this hierarchy's * root cgroup */ write_lock(&css_set_lock); list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, cgrp_link_list) { list_del(&link->cg_link_list); list_del(&link->cgrp_link_list); kfree(link); } write_unlock(&css_set_lock); if (!list_empty(&root->root_list)) { list_del(&root->root_list); root_count--; } mutex_unlock(&cgroup_mutex); kill_litter_super(sb); cgroup_drop_root(root); } static struct file_system_type cgroup_fs_type = { .name = "cgroup", .mount = cgroup_mount, .kill_sb = cgroup_kill_sb, }; static struct kobject *cgroup_kobj; static inline struct cgroup *__d_cgrp(struct dentry *dentry) { return dentry->d_fsdata; } static inline struct cftype *__d_cft(struct dentry *dentry) { return dentry->d_fsdata; } /** * cgroup_path - generate the path of a cgroup * @cgrp: the cgroup in question * @buf: the buffer to write the path into * @buflen: the length of the buffer * * Called with cgroup_mutex held or else with an RCU-protected cgroup * reference. Writes path of cgroup into buf. Returns 0 on success, * -errno on error. */ int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) { char *start; struct dentry *dentry = rcu_dereference_check(cgrp->dentry, rcu_read_lock_held() || cgroup_lock_is_held()); if (!dentry || cgrp == dummytop) { /* * Inactive subsystems have no dentry for their root * cgroup */ strcpy(buf, "/"); return 0; } start = buf + buflen; *--start = '\0'; for (;;) { int len = dentry->d_name.len; if ((start -= len) < buf) return -ENAMETOOLONG; memcpy(start, dentry->d_name.name, len); cgrp = cgrp->parent; if (!cgrp) break; dentry = rcu_dereference_check(cgrp->dentry, rcu_read_lock_held() || cgroup_lock_is_held()); if (!cgrp->parent) continue; if (--start < buf) return -ENAMETOOLONG; *start = '/'; } memmove(buf, start, buf + buflen - start); return 0; } EXPORT_SYMBOL_GPL(cgroup_path); /* * cgroup_task_migrate - move a task from one cgroup to another. * * 'guarantee' is set if the caller promises that a new css_set for the task * will already exist. If not set, this function might sleep, and can fail with * -ENOMEM. Otherwise, it can only fail with -ESRCH. */ static int cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp, struct task_struct *tsk, bool guarantee) { struct css_set *oldcg; struct css_set *newcg; /* * get old css_set. we need to take task_lock and refcount it, because * an exiting task can change its css_set to init_css_set and drop its * old one without taking cgroup_mutex. */ task_lock(tsk); oldcg = tsk->cgroups; get_css_set(oldcg); task_unlock(tsk); /* locate or allocate a new css_set for this task. */ if (guarantee) { /* we know the css_set we want already exists. */ struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; read_lock(&css_set_lock); newcg = find_existing_css_set(oldcg, cgrp, template); BUG_ON(!newcg); get_css_set(newcg); read_unlock(&css_set_lock); } else { might_sleep(); /* find_css_set will give us newcg already referenced. */ newcg = find_css_set(oldcg, cgrp); if (!newcg) { put_css_set(oldcg); return -ENOMEM; } } put_css_set(oldcg); /* if PF_EXITING is set, the tsk->cgroups pointer is no longer safe. */ task_lock(tsk); if (tsk->flags & PF_EXITING) { task_unlock(tsk); put_css_set(newcg); return -ESRCH; } rcu_assign_pointer(tsk->cgroups, newcg); task_unlock(tsk); /* Update the css_set linked lists if we're using them */ write_lock(&css_set_lock); if (!list_empty(&tsk->cg_list)) list_move(&tsk->cg_list, &newcg->tasks); write_unlock(&css_set_lock); /* * We just gained a reference on oldcg by taking it from the task. As * trading it for newcg is protected by cgroup_mutex, we're safe to drop * it here; it will be freed under RCU. */ put_css_set(oldcg); set_bit(CGRP_RELEASABLE, &oldcgrp->flags); return 0; } /** * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' * @cgrp: the cgroup the task is attaching to * @tsk: the task to be attached * * Call holding cgroup_mutex. May take task_lock of * the task 'tsk' during call. */ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) { int retval; struct cgroup_subsys *ss, *failed_ss = NULL; struct cgroup *oldcgrp; struct cgroupfs_root *root = cgrp->root; /* Nothing to do if the task is already in that cgroup */ oldcgrp = task_cgroup_from_root(tsk, root); if (cgrp == oldcgrp) return 0; for_each_subsys(root, ss) { if (ss->can_attach) { retval = ss->can_attach(ss, cgrp, tsk); if (retval) { /* * Remember on which subsystem the can_attach() * failed, so that we only call cancel_attach() * against the subsystems whose can_attach() * succeeded. (See below) */ failed_ss = ss; goto out; } } if (ss->can_attach_task) { retval = ss->can_attach_task(cgrp, tsk); if (retval) { failed_ss = ss; goto out; } } } retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, false); if (retval) goto out; for_each_subsys(root, ss) { if (ss->pre_attach) ss->pre_attach(cgrp); if (ss->attach_task) ss->attach_task(cgrp, tsk); if (ss->attach) ss->attach(ss, cgrp, oldcgrp, tsk); } synchronize_rcu(); /* * wake up rmdir() waiter. the rmdir should fail since the cgroup * is no longer empty. */ cgroup_wakeup_rmdir_waiter(cgrp); out: if (retval) { for_each_subsys(root, ss) { if (ss == failed_ss) /* * This subsystem was the one that failed the * can_attach() check earlier, so we don't need * to call cancel_attach() against it or any * remaining subsystems. */ break; if (ss->cancel_attach) ss->cancel_attach(ss, cgrp, tsk); } } return retval; } /** * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' * @from: attach to all cgroups of a given task * @tsk: the task to be attached */ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) { struct cgroupfs_root *root; int retval = 0; cgroup_lock(); for_each_active_root(root) { struct cgroup *from_cg = task_cgroup_from_root(from, root); retval = cgroup_attach_task(from_cg, tsk); if (retval) break; } cgroup_unlock(); return retval; } EXPORT_SYMBOL_GPL(cgroup_attach_task_all); /* * cgroup_attach_proc works in two stages, the first of which prefetches all * new css_sets needed (to make sure we have enough memory before committing * to the move) and stores them in a list of entries of the following type. * TODO: possible optimization: use css_set->rcu_head for chaining instead */ struct cg_list_entry { struct css_set *cg; struct list_head links; }; static bool css_set_check_fetched(struct cgroup *cgrp, struct task_struct *tsk, struct css_set *cg, struct list_head *newcg_list) { struct css_set *newcg; struct cg_list_entry *cg_entry; struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; read_lock(&css_set_lock); newcg = find_existing_css_set(cg, cgrp, template); if (newcg) get_css_set(newcg); read_unlock(&css_set_lock); /* doesn't exist at all? */ if (!newcg) return false; /* see if it's already in the list */ list_for_each_entry(cg_entry, newcg_list, links) { if (cg_entry->cg == newcg) { put_css_set(newcg); return true; } } /* not found */ put_css_set(newcg); return false; } /* * Find the new css_set and store it in the list in preparation for moving the * given task to the given cgroup. Returns 0 or -ENOMEM. */ static int css_set_prefetch(struct cgroup *cgrp, struct css_set *cg, struct list_head *newcg_list) { struct css_set *newcg; struct cg_list_entry *cg_entry; /* ensure a new css_set will exist for this thread */ newcg = find_css_set(cg, cgrp); if (!newcg) return -ENOMEM; /* add it to the list */ cg_entry = kmalloc(sizeof(struct cg_list_entry), GFP_KERNEL); if (!cg_entry) { put_css_set(newcg); return -ENOMEM; } cg_entry->cg = newcg; list_add(&cg_entry->links, newcg_list); return 0; } /** * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup * @cgrp: the cgroup to attach to * @leader: the threadgroup leader task_struct of the group to be attached * * Call holding cgroup_mutex and the threadgroup_fork_lock of the leader. Will * take task_lock of each thread in leader's threadgroup individually in turn. */ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader) { int retval, i, group_size; struct cgroup_subsys *ss, *failed_ss = NULL; bool cancel_failed_ss = false; /* guaranteed to be initialized later, but the compiler needs this */ struct cgroup *oldcgrp = NULL; struct css_set *oldcg; struct cgroupfs_root *root = cgrp->root; /* threadgroup list cursor and array */ struct task_struct *tsk; struct flex_array *group; /* * we need to make sure we have css_sets for all the tasks we're * going to move -before- we actually start moving them, so that in * case we get an ENOMEM we can bail out before making any changes. */ struct list_head newcg_list; struct cg_list_entry *cg_entry, *temp_nobe; /* * step 0: in order to do expensive, possibly blocking operations for * every thread, we cannot iterate the thread group list, since it needs * rcu or tasklist locked. instead, build an array of all threads in the * group - threadgroup_fork_lock prevents new threads from appearing, * and if threads exit, this will just be an over-estimate. */ group_size = get_nr_threads(leader); /* flex_array supports very large thread-groups better than kmalloc. */ group = flex_array_alloc(sizeof(struct task_struct *), group_size, GFP_KERNEL); if (!group) return -ENOMEM; /* pre-allocate to guarantee space while iterating in rcu read-side. */ retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL); if (retval) goto out_free_group_list; /* prevent changes to the threadgroup list while we take a snapshot. */ rcu_read_lock(); if (!thread_group_leader(leader)) { /* * a race with de_thread from another thread's exec() may strip * us of our leadership, making while_each_thread unsafe to use * on this task. if this happens, there is no choice but to * throw this task away and try again (from cgroup_procs_write); * this is "double-double-toil-and-trouble-check locking". */ rcu_read_unlock(); retval = -EAGAIN; goto out_free_group_list; } /* take a reference on each task in the group to go in the array. */ tsk = leader; i = 0; do { /* as per above, nr_threads may decrease, but not increase. */ BUG_ON(i >= group_size); get_task_struct(tsk); /* * saying GFP_ATOMIC has no effect here because we did prealloc * earlier, but it's good form to communicate our expectations. */ retval = flex_array_put_ptr(group, i, tsk, GFP_ATOMIC); BUG_ON(retval != 0); i++; } while_each_thread(leader, tsk); /* remember the number of threads in the array for later. */ group_size = i; rcu_read_unlock(); /* * step 1: check that we can legitimately attach to the cgroup. */ for_each_subsys(root, ss) { if (ss->can_attach) { retval = ss->can_attach(ss, cgrp, leader); if (retval) { failed_ss = ss; goto out_cancel_attach; } } /* a callback to be run on every thread in the threadgroup. */ if (ss->can_attach_task) { /* run on each task in the threadgroup. */ for (i = 0; i < group_size; i++) { tsk = flex_array_get_ptr(group, i); retval = ss->can_attach_task(cgrp, tsk); if (retval) { failed_ss = ss; cancel_failed_ss = true; goto out_cancel_attach; } } } } /* * step 2: make sure css_sets exist for all threads to be migrated. * we use find_css_set, which allocates a new one if necessary. */ INIT_LIST_HEAD(&newcg_list); for (i = 0; i < group_size; i++) { tsk = flex_array_get_ptr(group, i); /* nothing to do if this task is already in the cgroup */ oldcgrp = task_cgroup_from_root(tsk, root); if (cgrp == oldcgrp) continue; /* get old css_set pointer */ task_lock(tsk); if (tsk->flags & PF_EXITING) { /* ignore this task if it's going away */ task_unlock(tsk); continue; } oldcg = tsk->cgroups; get_css_set(oldcg); task_unlock(tsk); /* see if the new one for us is already in the list? */ if (css_set_check_fetched(cgrp, tsk, oldcg, &newcg_list)) { /* was already there, nothing to do. */ put_css_set(oldcg); } else { /* we don't already have it. get new one. */ retval = css_set_prefetch(cgrp, oldcg, &newcg_list); put_css_set(oldcg); if (retval) goto out_list_teardown; } } /* * step 3: now that we're guaranteed success wrt the css_sets, proceed * to move all tasks to the new cgroup, calling ss->attach_task for each * one along the way. there are no failure cases after here, so this is * the commit point. */ for_each_subsys(root, ss) { if (ss->pre_attach) ss->pre_attach(cgrp); } for (i = 0; i < group_size; i++) { tsk = flex_array_get_ptr(group, i); /* leave current thread as it is if it's already there */ oldcgrp = task_cgroup_from_root(tsk, root); if (cgrp == oldcgrp) continue; /* attach each task to each subsystem */ for_each_subsys(root, ss) { if (ss->attach_task) ss->attach_task(cgrp, tsk); } /* if the thread is PF_EXITING, it can just get skipped. */ retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true); BUG_ON(retval != 0 && retval != -ESRCH); } /* nothing is sensitive to fork() after this point. */ /* * step 4: do expensive, non-thread-specific subsystem callbacks. * TODO: if ever a subsystem needs to know the oldcgrp for each task * being moved, this call will need to be reworked to communicate that. */ for_each_subsys(root, ss) { if (ss->attach) ss->attach(ss, cgrp, oldcgrp, leader); } /* * step 5: success! and cleanup */ synchronize_rcu(); cgroup_wakeup_rmdir_waiter(cgrp); retval = 0; out_list_teardown: /* clean up the list of prefetched css_sets. */ list_for_each_entry_safe(cg_entry, temp_nobe, &newcg_list, links) { list_del(&cg_entry->links); put_css_set(cg_entry->cg); kfree(cg_entry); } out_cancel_attach: /* same deal as in cgroup_attach_task */ if (retval) { for_each_subsys(root, ss) { if (ss == failed_ss) { if (cancel_failed_ss && ss->cancel_attach) ss->cancel_attach(ss, cgrp, leader); break; } if (ss->cancel_attach) ss->cancel_attach(ss, cgrp, leader); } } /* clean up the array of referenced threads in the group. */ for (i = 0; i < group_size; i++) { tsk = flex_array_get_ptr(group, i); put_task_struct(tsk); } out_free_group_list: flex_array_free(group); return retval; } /* * Find the task_struct of the task to attach by vpid and pass it along to the * function to attach either it or all tasks in its threadgroup. Will take * cgroup_mutex; may take task_lock of task. */ static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup) { struct task_struct *tsk; const struct cred *cred = current_cred(), *tcred; int ret; if (!cgroup_lock_live_group(cgrp)) return -ENODEV; if (pid) { rcu_read_lock(); tsk = find_task_by_vpid(pid); if (!tsk) { rcu_read_unlock(); cgroup_unlock(); return -ESRCH; } if (threadgroup) { /* * RCU protects this access, since tsk was found in the * tid map. a race with de_thread may cause group_leader * to stop being the leader, but cgroup_attach_proc will * detect it later. */ tsk = tsk->group_leader; } else if (tsk->flags & PF_EXITING) { /* optimization for the single-task-only case */ rcu_read_unlock(); cgroup_unlock(); return -ESRCH; } /* * even if we're attaching all tasks in the thread group, we * only need to check permissions on one of them. */ tcred = __task_cred(tsk); if (cred->euid && cred->euid != tcred->uid && cred->euid != tcred->suid) { rcu_read_unlock(); cgroup_unlock(); return -EACCES; } get_task_struct(tsk); rcu_read_unlock(); } else { if (threadgroup) tsk = current->group_leader; else tsk = current; get_task_struct(tsk); } if (threadgroup) { threadgroup_fork_write_lock(tsk); ret = cgroup_attach_proc(cgrp, tsk); threadgroup_fork_write_unlock(tsk); } else { ret = cgroup_attach_task(cgrp, tsk); } put_task_struct(tsk); cgroup_unlock(); return ret; } static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) { return attach_task_by_pid(cgrp, pid, false); } static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) { int ret; do { /* * attach_proc fails with -EAGAIN if threadgroup leadership * changes in the middle of the operation, in which case we need * to find the task_struct for the new leader and start over. */ ret = attach_task_by_pid(cgrp, tgid, true); } while (ret == -EAGAIN); return ret; } /** * 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 lock should be later released with * cgroup_unlock(). On failure returns false with no lock held. */ bool cgroup_lock_live_group(struct cgroup *cgrp) { mutex_lock(&cgroup_mutex); if (cgroup_is_removed(cgrp)) { mutex_unlock(&cgroup_mutex); return false; } return true; } EXPORT_SYMBOL_GPL(cgroup_lock_live_group); static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, const char *buffer) { BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); if (strlen(buffer) >= PATH_MAX) return -EINVAL; if (!cgroup_lock_live_group(cgrp)) return -ENODEV; strcpy(cgrp->root->release_agent_path, buffer); cgroup_unlock(); return 0; } static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, struct seq_file *seq) { if (!cgroup_lock_live_group(cgrp)) return -ENODEV; seq_puts(seq, cgrp->root->release_agent_path); seq_putc(seq, '\n'); cgroup_unlock(); return 0; } /* A buffer size big enough for numbers or short strings */ #define CGROUP_LOCAL_BUFFER_SIZE 64 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, struct file *file, const char __user *userbuf, size_t nbytes, loff_t *unused_ppos) { char buffer[CGROUP_LOCAL_BUFFER_SIZE]; int retval = 0; char *end; if (!nbytes) return -EINVAL; if (nbytes >= sizeof(buffer)) return -E2BIG; if (copy_from_user(buffer, userbuf, nbytes)) return -EFAULT; buffer[nbytes] = 0; /* nul-terminate */ if (cft->write_u64) { u64 val = simple_strtoull(strstrip(buffer), &end, 0); if (*end) return -EINVAL; retval = cft->write_u64(cgrp, cft, val); } else { s64 val = simple_strtoll(strstrip(buffer), &end, 0); if (*end) return -EINVAL; retval = cft->write_s64(cgrp, cft, val); } if (!retval) retval = nbytes; return retval; } static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, struct file *file, const char __user *userbuf, size_t nbytes, loff_t *unused_ppos) { char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; int retval = 0; size_t max_bytes = cft->max_write_len; char *buffer = local_buffer; if (!max_bytes) max_bytes = sizeof(local_buffer) - 1; if (nbytes >= max_bytes) return -E2BIG; /* Allocate a dynamic buffer if we need one */ if (nbytes >= sizeof(local_buffer)) { buffer = kmalloc(nbytes + 1, GFP_KERNEL); if (buffer == NULL) return -ENOMEM; } if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { retval = -EFAULT; goto out; } buffer[nbytes] = 0; /* nul-terminate */ retval = cft->write_string(cgrp, cft, strstrip(buffer)); if (!retval) retval = nbytes; out: if (buffer != local_buffer) kfree(buffer); return retval; } static ssize_t cgroup_file_write(struct file *file, const char __user *buf, size_t nbytes, loff_t *ppos) { struct cftype *cft = __d_cft(file->f_dentry); struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); if (cgroup_is_removed(cgrp)) return -ENODEV; if (cft->write) return cft->write(cgrp, cft, file, buf, nbytes, ppos); if (cft->write_u64 || cft->write_s64) return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); if (cft->write_string) return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); if (cft->trigger) { int ret = cft->trigger(cgrp, (unsigned int)cft->private); return ret ? ret : nbytes; } return -EINVAL; } static ssize_t cgroup_read_u64(struct cg