Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 43 | #include <linux/slab.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/spinlock.h> |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 45 | #include <linux/rwsem.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 46 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 47 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 48 | #include <linux/kmod.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 49 | #include <linux/delayacct.h> |
| 50 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 51 | #include <linux/hashtable.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 52 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 53 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 54 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 55 | #include <linux/kthread.h> |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 56 | #include <linux/delay.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 57 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 58 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 59 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 60 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 61 | * pidlists linger the following amount before being destroyed. The goal |
| 62 | * is avoiding frequent destruction in the middle of consecutive read calls |
| 63 | * Expiring in the middle is a performance problem not a correctness one. |
| 64 | * 1 sec should be enough. |
| 65 | */ |
| 66 | #define CGROUP_PIDLIST_DESTROY_DELAY HZ |
| 67 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 68 | #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ |
| 69 | MAX_CFTYPE_NAME + 2) |
| 70 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 71 | /* |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 72 | * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file |
| 73 | * creation/removal and hierarchy changing operations including cgroup |
| 74 | * creation, removal, css association and controller rebinding. This outer |
| 75 | * lock is needed mainly to resolve the circular dependency between kernfs |
| 76 | * active ref and cgroup_mutex. cgroup_tree_mutex nests above both. |
| 77 | */ |
| 78 | static DEFINE_MUTEX(cgroup_tree_mutex); |
| 79 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 80 | /* |
| 81 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 82 | * hierarchy must be performed while holding it. |
| 83 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 84 | * css_set_rwsem protects task->cgroups pointer, the list of css_set |
| 85 | * objects, and the chain of tasks off each css_set. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 86 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 87 | * These locks are exported if CONFIG_PROVE_RCU so that accessors in |
| 88 | * cgroup.h can use them for lockdep annotations. |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 90 | #ifdef CONFIG_PROVE_RCU |
| 91 | DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 92 | DECLARE_RWSEM(css_set_rwsem); |
| 93 | EXPORT_SYMBOL_GPL(cgroup_mutex); |
| 94 | EXPORT_SYMBOL_GPL(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 95 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 96 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 97 | static DECLARE_RWSEM(css_set_rwsem); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 98 | #endif |
| 99 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 100 | /* |
| 101 | * Protects cgroup_subsys->release_agent_path. Modifying it also requires |
| 102 | * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock. |
| 103 | */ |
| 104 | static DEFINE_SPINLOCK(release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 105 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 106 | #define cgroup_assert_mutexes_or_rcu_locked() \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 107 | rcu_lockdep_assert(rcu_read_lock_held() || \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 108 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 109 | lockdep_is_held(&cgroup_mutex), \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 110 | "cgroup_[tree_]mutex or RCU read lock required"); |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 111 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 112 | /* |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 113 | * cgroup destruction makes heavy use of work items and there can be a lot |
| 114 | * of concurrent destructions. Use a separate workqueue so that cgroup |
| 115 | * destruction work items don't end up filling up max_active of system_wq |
| 116 | * which may lead to deadlock. |
| 117 | */ |
| 118 | static struct workqueue_struct *cgroup_destroy_wq; |
| 119 | |
| 120 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 121 | * pidlist destructions need to be flushed on cgroup destruction. Use a |
| 122 | * separate workqueue as flush domain. |
| 123 | */ |
| 124 | static struct workqueue_struct *cgroup_pidlist_destroy_wq; |
| 125 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 126 | /* generate an array of cgroup subsystem pointers */ |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 127 | #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 128 | static struct cgroup_subsys *cgroup_subsys[] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 129 | #include <linux/cgroup_subsys.h> |
| 130 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 131 | #undef SUBSYS |
| 132 | |
| 133 | /* array of cgroup subsystem names */ |
| 134 | #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, |
| 135 | static const char *cgroup_subsys_name[] = { |
| 136 | #include <linux/cgroup_subsys.h> |
| 137 | }; |
| 138 | #undef SUBSYS |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 139 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 140 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 141 | * The default hierarchy, reserved for the subsystems that are otherwise |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 142 | * unattached - it never has more than a single cgroup, and all tasks are |
| 143 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 145 | struct cgroup_root cgrp_dfl_root; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 146 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 147 | /* |
| 148 | * The default hierarchy always exists but is hidden until mounted for the |
| 149 | * first time. This is for backward compatibility. |
| 150 | */ |
| 151 | static bool cgrp_dfl_root_visible; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 152 | |
| 153 | /* The list of hierarchy roots */ |
| 154 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 155 | static LIST_HEAD(cgroup_roots); |
| 156 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 157 | |
Tejun Heo | 3417ae1 | 2014-02-08 10:37:01 -0500 | [diff] [blame] | 158 | /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 159 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 160 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 161 | /* |
| 162 | * Assign a monotonically increasing serial number to cgroups. It |
| 163 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 164 | * numbers. Also, as cgroups are always appended to the parent's |
| 165 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 166 | * the ascending serial number order on the list. Protected by |
| 167 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 168 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 169 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 170 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 171 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 172 | * check for fork/exit handlers to call. This avoids us having to do |
| 173 | * extra work in the fork/exit path if none of the subsystems need to |
| 174 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 175 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 176 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 177 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 178 | static struct cftype cgroup_base_files[]; |
| 179 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 180 | static void cgroup_put(struct cgroup *cgrp); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 181 | static int rebind_subsystems(struct cgroup_root *dst_root, |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 182 | unsigned long ss_mask); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 183 | static void cgroup_destroy_css_killed(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 184 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 185 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 186 | bool is_add); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 187 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 188 | |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 189 | /** |
| 190 | * cgroup_css - obtain a cgroup's css for the specified subsystem |
| 191 | * @cgrp: the cgroup of interest |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 192 | * @ss: the subsystem of interest (%NULL returns the dummy_css) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 193 | * |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 194 | * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This |
| 195 | * function must be called either under cgroup_mutex or rcu_read_lock() and |
| 196 | * the caller is responsible for pinning the returned css if it wants to |
| 197 | * keep accessing it outside the said locks. This function may return |
| 198 | * %NULL if @cgrp doesn't have @subsys_id enabled. |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 199 | */ |
| 200 | static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 201 | struct cgroup_subsys *ss) |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 202 | { |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 203 | if (ss) |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 204 | return rcu_dereference_check(cgrp->subsys[ss->id], |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 205 | lockdep_is_held(&cgroup_tree_mutex) || |
| 206 | lockdep_is_held(&cgroup_mutex)); |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 207 | else |
| 208 | return &cgrp->dummy_css; |
Tejun Heo | 95109b6 | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 209 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 210 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 211 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 212 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 213 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 214 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 217 | struct cgroup_subsys_state *seq_css(struct seq_file *seq) |
| 218 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 219 | struct kernfs_open_file *of = seq->private; |
| 220 | struct cgroup *cgrp = of->kn->parent->priv; |
| 221 | struct cftype *cft = seq_cft(seq); |
| 222 | |
| 223 | /* |
| 224 | * This is open and unprotected implementation of cgroup_css(). |
| 225 | * seq_css() is only called from a kernfs file operation which has |
| 226 | * an active reference on the file. Because all the subsystem |
| 227 | * files are drained before a css is disassociated with a cgroup, |
| 228 | * the matching css from the cgroup's subsys table is guaranteed to |
| 229 | * be and stay valid until the enclosing operation is complete. |
| 230 | */ |
| 231 | if (cft->ss) |
| 232 | return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); |
| 233 | else |
| 234 | return &cgrp->dummy_css; |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL_GPL(seq_css); |
| 237 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 238 | /** |
| 239 | * cgroup_is_descendant - test ancestry |
| 240 | * @cgrp: the cgroup to be tested |
| 241 | * @ancestor: possible ancestor of @cgrp |
| 242 | * |
| 243 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 244 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 245 | * and @ancestor are accessible. |
| 246 | */ |
| 247 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 248 | { |
| 249 | while (cgrp) { |
| 250 | if (cgrp == ancestor) |
| 251 | return true; |
| 252 | cgrp = cgrp->parent; |
| 253 | } |
| 254 | return false; |
| 255 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 256 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 257 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 258 | { |
| 259 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 260 | (1 << CGRP_RELEASABLE) | |
| 261 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 262 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 265 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 266 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 267 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 270 | /** |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 271 | * for_each_css - iterate all css's of a cgroup |
| 272 | * @css: the iteration cursor |
| 273 | * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end |
| 274 | * @cgrp: the target cgroup to iterate css's of |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 275 | * |
| 276 | * Should be called under cgroup_mutex. |
| 277 | */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 278 | #define for_each_css(css, ssid, cgrp) \ |
| 279 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ |
| 280 | if (!((css) = rcu_dereference_check( \ |
| 281 | (cgrp)->subsys[(ssid)], \ |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 282 | lockdep_is_held(&cgroup_tree_mutex) || \ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 283 | lockdep_is_held(&cgroup_mutex)))) { } \ |
| 284 | else |
| 285 | |
| 286 | /** |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 287 | * for_each_subsys - iterate all enabled cgroup subsystems |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 288 | * @ss: the iteration cursor |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 289 | * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 290 | */ |
Tejun Heo | 780cd8b | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 291 | #define for_each_subsys(ss, ssid) \ |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 292 | for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \ |
| 293 | (((ss) = cgroup_subsys[ssid]) || true); (ssid)++) |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 294 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 295 | /* iterate across the hierarchies */ |
| 296 | #define for_each_root(root) \ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 297 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 298 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 299 | /** |
| 300 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 301 | * @cgrp: the cgroup to be checked for liveness |
| 302 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 303 | * On success, returns true; the mutex should be later unlocked. On |
| 304 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 305 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 306 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 307 | { |
| 308 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 309 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 310 | mutex_unlock(&cgroup_mutex); |
| 311 | return false; |
| 312 | } |
| 313 | return true; |
| 314 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 315 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 316 | /* the list of cgroups eligible for automatic release. Protected by |
| 317 | * release_list_lock */ |
| 318 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 319 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 320 | static void cgroup_release_agent(struct work_struct *work); |
| 321 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 322 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 323 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 324 | /* |
| 325 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 326 | * belong to different cgroups on different hierarchies. In the other |
| 327 | * direction, a css_set is naturally associated with multiple cgroups. |
| 328 | * This M:N relationship is represented by the following link structure |
| 329 | * which exists for each association and allows traversing the associations |
| 330 | * from both sides. |
| 331 | */ |
| 332 | struct cgrp_cset_link { |
| 333 | /* the cgroup and css_set this link associates */ |
| 334 | struct cgroup *cgrp; |
| 335 | struct css_set *cset; |
| 336 | |
| 337 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 338 | struct list_head cset_link; |
| 339 | |
| 340 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 341 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 342 | }; |
| 343 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 344 | /* |
| 345 | * The default css_set - used by init and its children prior to any |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 346 | * hierarchies being mounted. It contains a pointer to the root state |
| 347 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 348 | * reference-counted, to improve performance when child cgroups |
| 349 | * haven't been created. |
| 350 | */ |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 351 | static struct css_set init_css_set = { |
| 352 | .refcount = ATOMIC_INIT(1), |
| 353 | .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), |
| 354 | .tasks = LIST_HEAD_INIT(init_css_set.tasks), |
| 355 | .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), |
| 356 | .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), |
| 357 | .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), |
| 358 | }; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 359 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 360 | static int css_set_count = 1; /* 1 for init_css_set */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 361 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 362 | /* |
| 363 | * hash table for cgroup groups. This improves the performance to find |
| 364 | * an existing css_set. This hash doesn't (currently) take into |
| 365 | * account cgroups in empty hierarchies. |
| 366 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 367 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 368 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 369 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 370 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 371 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 372 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 373 | struct cgroup_subsys *ss; |
| 374 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 375 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 376 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 377 | key += (unsigned long)css[i]; |
| 378 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 379 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 380 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 383 | static void put_css_set_locked(struct css_set *cset, bool taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 384 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 385 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 386 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 387 | lockdep_assert_held(&css_set_rwsem); |
| 388 | |
| 389 | if (!atomic_dec_and_test(&cset->refcount)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 390 | return; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 391 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 392 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 393 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 394 | css_set_count--; |
| 395 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 396 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 397 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 398 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 399 | list_del(&link->cset_link); |
| 400 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 401 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 402 | /* @cgrp can't go away while we're holding css_set_rwsem */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 403 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 404 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 405 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 406 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 407 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 408 | |
| 409 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 410 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 411 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 412 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 415 | static void put_css_set(struct css_set *cset, bool taskexit) |
| 416 | { |
| 417 | /* |
| 418 | * Ensure that the refcount doesn't hit zero while any readers |
| 419 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 420 | * rwlock |
| 421 | */ |
| 422 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
| 423 | return; |
| 424 | |
| 425 | down_write(&css_set_rwsem); |
| 426 | put_css_set_locked(cset, taskexit); |
| 427 | up_write(&css_set_rwsem); |
| 428 | } |
| 429 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 430 | /* |
| 431 | * refcounted get/put for css_set objects |
| 432 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 433 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 434 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 435 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 438 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 439 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 440 | * @cset: candidate css_set being tested |
| 441 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 442 | * @new_cgrp: cgroup that's being entered by the task |
| 443 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 444 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 445 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 446 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 447 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 448 | static bool compare_css_sets(struct css_set *cset, |
| 449 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 450 | struct cgroup *new_cgrp, |
| 451 | struct cgroup_subsys_state *template[]) |
| 452 | { |
| 453 | struct list_head *l1, *l2; |
| 454 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 455 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 456 | /* Not all subsystems matched */ |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Compare cgroup pointers in order to distinguish between |
| 462 | * different cgroups in heirarchies with no subsystems. We |
| 463 | * could get by with just this check alone (and skip the |
| 464 | * memcmp above) but on most setups the memcmp check will |
| 465 | * avoid the need for this more expensive check on almost all |
| 466 | * candidates. |
| 467 | */ |
| 468 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 469 | l1 = &cset->cgrp_links; |
| 470 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 471 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 472 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 473 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | |
| 475 | l1 = l1->next; |
| 476 | l2 = l2->next; |
| 477 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 478 | if (l1 == &cset->cgrp_links) { |
| 479 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | break; |
| 481 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 482 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 483 | } |
| 484 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 485 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 486 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 487 | cgrp1 = link1->cgrp; |
| 488 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 490 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * If this hierarchy is the hierarchy of the cgroup |
| 494 | * that's changing, then we need to check that this |
| 495 | * css_set points to the new cgroup; if it's any other |
| 496 | * hierarchy, then this css_set should point to the |
| 497 | * same cgroup as the old css_set. |
| 498 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 499 | if (cgrp1->root == new_cgrp->root) { |
| 500 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 501 | return false; |
| 502 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 503 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 510 | /** |
| 511 | * find_existing_css_set - init css array and find the matching css_set |
| 512 | * @old_cset: the css_set that we're using before the cgroup transition |
| 513 | * @cgrp: the cgroup that we're moving into |
| 514 | * @template: out param for the new set of csses, should be clear on entry |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 515 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 516 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 517 | struct cgroup *cgrp, |
| 518 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 519 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 520 | struct cgroup_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 521 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 522 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 523 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 524 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 525 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 526 | /* |
| 527 | * Build the set of subsystem state objects that we want to see in the |
| 528 | * new css_set. while subsystems can change globally, the entries here |
| 529 | * won't change, so no need for locking. |
| 530 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 531 | for_each_subsys(ss, i) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 532 | if (root->cgrp.subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 533 | /* Subsystem is in this hierarchy. So we want |
| 534 | * the subsystem state from the new |
| 535 | * cgroup */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 536 | template[i] = cgroup_css(cgrp, ss); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 537 | } else { |
| 538 | /* Subsystem is not in this hierarchy, so we |
| 539 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 544 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 545 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 546 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 547 | continue; |
| 548 | |
| 549 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 550 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 551 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 552 | |
| 553 | /* No existing cgroup group matched */ |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 557 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 558 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 559 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 560 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 561 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 562 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 563 | kfree(link); |
| 564 | } |
| 565 | } |
| 566 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 567 | /** |
| 568 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 569 | * @count: the number of links to allocate |
| 570 | * @tmp_links: list_head the allocated links are put on |
| 571 | * |
| 572 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 573 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 574 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 575 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 576 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 577 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 578 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | |
| 580 | INIT_LIST_HEAD(tmp_links); |
| 581 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 582 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 583 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 584 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 586 | return -ENOMEM; |
| 587 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 588 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 593 | /** |
| 594 | * link_css_set - a helper function to link a css_set to a cgroup |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 596 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 597 | * @cgrp: the destination cgroup |
| 598 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 600 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 601 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 602 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 603 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 604 | BUG_ON(list_empty(tmp_links)); |
| 605 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 606 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 607 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 608 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 609 | /* |
| 610 | * Always add links to the tail of the list so that the list |
| 611 | * is sorted by order of hierarchy creation |
| 612 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 613 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 616 | /** |
| 617 | * find_css_set - return a new css_set with one cgroup updated |
| 618 | * @old_cset: the baseline css_set |
| 619 | * @cgrp: the cgroup to be updated |
| 620 | * |
| 621 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 622 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 623 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 624 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 625 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 626 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 627 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 628 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 629 | struct list_head tmp_links; |
| 630 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 631 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 632 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 633 | lockdep_assert_held(&cgroup_mutex); |
| 634 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 635 | /* First see if we already have a cgroup group that matches |
| 636 | * the desired set */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 637 | down_read(&css_set_rwsem); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 638 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 639 | if (cset) |
| 640 | get_css_set(cset); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 641 | up_read(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 642 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 643 | if (cset) |
| 644 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 645 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 646 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 647 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 648 | return NULL; |
| 649 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 650 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 651 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 652 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
| 655 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 656 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 657 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 658 | INIT_LIST_HEAD(&cset->tasks); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 659 | INIT_LIST_HEAD(&cset->mg_tasks); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 660 | INIT_LIST_HEAD(&cset->mg_preload_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 661 | INIT_LIST_HEAD(&cset->mg_node); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 662 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* Copy the set of subsystem state objects generated in |
| 665 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 666 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 668 | down_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 669 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 670 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 671 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 672 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 673 | if (c->root == cgrp->root) |
| 674 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 675 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 676 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 678 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 679 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 680 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 681 | |
| 682 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 683 | key = css_set_hash(cset->subsys); |
| 684 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 685 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 686 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 687 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 688 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 691 | static struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 692 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 693 | struct cgroup *root_cgrp = kf_root->kn->priv; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 694 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 695 | return root_cgrp->root; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 696 | } |
| 697 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 698 | static int cgroup_init_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 699 | { |
| 700 | int id; |
| 701 | |
| 702 | lockdep_assert_held(&cgroup_mutex); |
| 703 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 704 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 705 | if (id < 0) |
| 706 | return id; |
| 707 | |
| 708 | root->hierarchy_id = id; |
| 709 | return 0; |
| 710 | } |
| 711 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 712 | static void cgroup_exit_root_id(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 713 | { |
| 714 | lockdep_assert_held(&cgroup_mutex); |
| 715 | |
| 716 | if (root->hierarchy_id) { |
| 717 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
| 718 | root->hierarchy_id = 0; |
| 719 | } |
| 720 | } |
| 721 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 722 | static void cgroup_free_root(struct cgroup_root *root) |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 723 | { |
| 724 | if (root) { |
| 725 | /* hierarhcy ID shoulid already have been released */ |
| 726 | WARN_ON_ONCE(root->hierarchy_id); |
| 727 | |
| 728 | idr_destroy(&root->cgroup_idr); |
| 729 | kfree(root); |
| 730 | } |
| 731 | } |
| 732 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 733 | static void cgroup_destroy_root(struct cgroup_root *root) |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 734 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 735 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 736 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 737 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 738 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 739 | mutex_lock(&cgroup_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 740 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 741 | BUG_ON(atomic_read(&root->nr_cgrps)); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 742 | BUG_ON(!list_empty(&cgrp->children)); |
| 743 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 744 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 745 | rebind_subsystems(&cgrp_dfl_root, cgrp->subsys_mask); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * Release all the links from cset_links to this hierarchy's |
| 749 | * root cgroup |
| 750 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 751 | down_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 752 | |
| 753 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 754 | list_del(&link->cset_link); |
| 755 | list_del(&link->cgrp_link); |
| 756 | kfree(link); |
| 757 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 758 | up_write(&css_set_rwsem); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 759 | |
| 760 | if (!list_empty(&root->root_list)) { |
| 761 | list_del(&root->root_list); |
| 762 | cgroup_root_count--; |
| 763 | } |
| 764 | |
| 765 | cgroup_exit_root_id(root); |
| 766 | |
| 767 | mutex_unlock(&cgroup_mutex); |
| 768 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 769 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 770 | kernfs_destroy_root(root->kf_root); |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 771 | cgroup_free_root(root); |
| 772 | } |
| 773 | |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 774 | /* look up cgroup associated with given css_set on the specified hierarchy */ |
| 775 | static struct cgroup *cset_cgroup_from_root(struct css_set *cset, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 776 | struct cgroup_root *root) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 777 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 778 | struct cgroup *res = NULL; |
| 779 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 780 | lockdep_assert_held(&cgroup_mutex); |
| 781 | lockdep_assert_held(&css_set_rwsem); |
| 782 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 783 | if (cset == &init_css_set) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 784 | res = &root->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 785 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 786 | struct cgrp_cset_link *link; |
| 787 | |
| 788 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 789 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 790 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 791 | if (c->root == root) { |
| 792 | res = c; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 797 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 798 | BUG_ON(!res); |
| 799 | return res; |
| 800 | } |
| 801 | |
| 802 | /* |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 803 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 804 | * called with cgroup_mutex and css_set_rwsem held. |
| 805 | */ |
| 806 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 807 | struct cgroup_root *root) |
Tejun Heo | ceb6a08 | 2014-02-25 10:04:02 -0500 | [diff] [blame] | 808 | { |
| 809 | /* |
| 810 | * No need to lock the task - since we hold cgroup_mutex the |
| 811 | * task can't change groups, so the only thing that can happen |
| 812 | * is that it exits and its css is set back to init_css_set. |
| 813 | */ |
| 814 | return cset_cgroup_from_root(task_css_set(task), root); |
| 815 | } |
| 816 | |
| 817 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 818 | * A task must hold cgroup_mutex to modify cgroups. |
| 819 | * |
| 820 | * Any task can increment and decrement the count field without lock. |
| 821 | * So in general, code holding cgroup_mutex can't rely on the count |
| 822 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 823 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 824 | * means that no tasks are currently attached, therefore there is no |
| 825 | * way a task attached to that cgroup can fork (the other way to |
| 826 | * increment the count). So code holding cgroup_mutex can safely |
| 827 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 828 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 829 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 830 | * needs that mutex. |
| 831 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 832 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 833 | * (usually) take cgroup_mutex. These are the two most performance |
| 834 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 835 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 836 | * is taken, and if the cgroup count is zero, a usermode call made |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 837 | * to the release agent with the name of the cgroup (path relative to |
| 838 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 839 | * |
| 840 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 841 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 842 | * tasks in the system use _some_ cgroup, and since there is always at |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 843 | * least one task in the system (init, pid == 1), therefore, root cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 844 | * always has either children cgroups and/or using tasks. So we don't |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 845 | * need a special hack to ensure that root cgroup cannot be deleted. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 846 | * |
| 847 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 848 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 849 | */ |
| 850 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 851 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 852 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 853 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 854 | |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 855 | static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, |
| 856 | char *buf) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 857 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 858 | if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && |
| 859 | !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) |
| 860 | snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s", |
| 861 | cft->ss->name, cft->name); |
| 862 | else |
| 863 | strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX); |
| 864 | return buf; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 867 | /** |
| 868 | * cgroup_file_mode - deduce file mode of a control file |
| 869 | * @cft: the control file in question |
| 870 | * |
| 871 | * returns cft->mode if ->mode is not 0 |
| 872 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 873 | * returns S_IRUGO if it has only a read handler |
| 874 | * returns S_IWUSR if it has only a write hander |
| 875 | */ |
| 876 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 877 | { |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 878 | umode_t mode = 0; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 879 | |
Tejun Heo | f2e85d5 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 880 | if (cft->mode) |
| 881 | return cft->mode; |
| 882 | |
| 883 | if (cft->read_u64 || cft->read_s64 || cft->seq_show) |
| 884 | mode |= S_IRUGO; |
| 885 | |
| 886 | if (cft->write_u64 || cft->write_s64 || cft->write_string || |
| 887 | cft->trigger) |
| 888 | mode |= S_IWUSR; |
| 889 | |
| 890 | return mode; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 891 | } |
| 892 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 893 | static void cgroup_free_fn(struct work_struct *work) |
| 894 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 895 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 896 | |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 897 | atomic_dec(&cgrp->root->nr_cgrps); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 898 | cgroup_pidlist_destroy_all(cgrp); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 899 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 900 | if (cgrp->parent) { |
| 901 | /* |
| 902 | * We get a ref to the parent, and put the ref when this |
| 903 | * cgroup is being freed, so it's guaranteed that the |
| 904 | * parent won't be destroyed before its children. |
| 905 | */ |
| 906 | cgroup_put(cgrp->parent); |
| 907 | kernfs_put(cgrp->kn); |
| 908 | kfree(cgrp); |
| 909 | } else { |
| 910 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 911 | * This is root cgroup's refcnt reaching zero, which |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 912 | * indicates that the root should be released. |
| 913 | */ |
| 914 | cgroup_destroy_root(cgrp->root); |
| 915 | } |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static void cgroup_free_rcu(struct rcu_head *head) |
| 919 | { |
| 920 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 921 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 922 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 923 | queue_work(cgroup_destroy_wq, &cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 924 | } |
| 925 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 926 | static void cgroup_get(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 927 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 928 | WARN_ON_ONCE(cgroup_is_dead(cgrp)); |
| 929 | WARN_ON_ONCE(atomic_read(&cgrp->refcnt) <= 0); |
| 930 | atomic_inc(&cgrp->refcnt); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 933 | static void cgroup_put(struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 935 | if (!atomic_dec_and_test(&cgrp->refcnt)) |
| 936 | return; |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 937 | if (WARN_ON_ONCE(cgrp->parent && !cgroup_is_dead(cgrp))) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 938 | return; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 939 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 940 | /* |
| 941 | * XXX: cgrp->id is only used to look up css's. As cgroup and |
| 942 | * css's lifetimes will be decoupled, it should be made |
| 943 | * per-subsystem and moved to css->id so that lookups are |
| 944 | * successful until the target css is released. |
| 945 | */ |
| 946 | mutex_lock(&cgroup_mutex); |
| 947 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 948 | mutex_unlock(&cgroup_mutex); |
| 949 | cgrp->id = -1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 950 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 951 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 954 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 955 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 956 | char name[CGROUP_FILE_NAME_MAX]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 957 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 958 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 959 | kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 962 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 963 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 964 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 965 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 966 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 967 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 968 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 969 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 970 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 971 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 972 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 973 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 974 | |
| 975 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 976 | continue; |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 977 | list_for_each_entry(cfts, &ss->cfts, node) |
| 978 | cgroup_addrm_files(cgrp, cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 979 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 982 | static int rebind_subsystems(struct cgroup_root *dst_root, |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 983 | unsigned long ss_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 985 | struct cgroup_subsys *ss; |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 986 | int ssid, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 987 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 988 | lockdep_assert_held(&cgroup_tree_mutex); |
| 989 | lockdep_assert_held(&cgroup_mutex); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 990 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 991 | for_each_subsys(ss, ssid) { |
| 992 | if (!(ss_mask & (1 << ssid))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 994 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 995 | /* if @ss is on the dummy_root, we can always move it */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 996 | if (ss->root == &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 997 | continue; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 998 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 999 | /* if @ss has non-root cgroups attached to it, can't move */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1000 | if (!list_empty(&ss->root->cgrp.children)) |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1001 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1003 | /* can't move between two non-dummy roots either */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1004 | if (dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1005 | return -EBUSY; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1008 | ret = cgroup_populate_dir(&dst_root->cgrp, ss_mask); |
| 1009 | if (ret) { |
| 1010 | if (dst_root != &cgrp_dfl_root) |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1011 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1012 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1013 | /* |
| 1014 | * Rebinding back to the default root is not allowed to |
| 1015 | * fail. Using both default and non-default roots should |
| 1016 | * be rare. Moving subsystems back and forth even more so. |
| 1017 | * Just warn about it and continue. |
| 1018 | */ |
| 1019 | if (cgrp_dfl_root_visible) { |
| 1020 | pr_warning("cgroup: failed to create files (%d) while rebinding 0x%lx to default root\n", |
| 1021 | ret, ss_mask); |
| 1022 | pr_warning("cgroup: you may retry by moving them to a different hierarchy and unbinding\n"); |
| 1023 | } |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1024 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | * Nothing can fail from this point on. Remove files for the |
| 1028 | * removed subsystems and rebind each subsystem. |
| 1029 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1030 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1031 | for_each_subsys(ss, ssid) |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1032 | if (ss_mask & (1 << ssid)) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1033 | cgroup_clear_dir(&ss->root->cgrp, 1 << ssid); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1034 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1035 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1036 | for_each_subsys(ss, ssid) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1037 | struct cgroup_root *src_root; |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1038 | struct cgroup_subsys_state *css; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1039 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1040 | if (!(ss_mask & (1 << ssid))) |
| 1041 | continue; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1042 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1043 | src_root = ss->root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1044 | css = cgroup_css(&src_root->cgrp, ss); |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1045 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1046 | WARN_ON(!css || cgroup_css(&dst_root->cgrp, ss)); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1047 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1048 | RCU_INIT_POINTER(src_root->cgrp.subsys[ssid], NULL); |
| 1049 | rcu_assign_pointer(dst_root->cgrp.subsys[ssid], css); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1050 | ss->root = dst_root; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1051 | css->cgroup = &dst_root->cgrp; |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1052 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1053 | src_root->cgrp.subsys_mask &= ~(1 << ssid); |
| 1054 | dst_root->cgrp.subsys_mask |= 1 << ssid; |
Tejun Heo | 73e80ed | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 1055 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1056 | if (ss->bind) |
| 1057 | ss->bind(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1059 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1060 | kernfs_activate(dst_root->cgrp.kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1064 | static int cgroup_show_options(struct seq_file *seq, |
| 1065 | struct kernfs_root *kf_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1066 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1067 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1068 | struct cgroup_subsys *ss; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1069 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1071 | for_each_subsys(ss, ssid) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1072 | if (root->cgrp.subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 1073 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1074 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1075 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1076 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1077 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1078 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1079 | seq_puts(seq, ",xattr"); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1080 | |
| 1081 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1082 | if (strlen(root->release_agent_path)) |
| 1083 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1084 | spin_unlock(&release_agent_path_lock); |
| 1085 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1086 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1087 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1088 | if (strlen(root->name)) |
| 1089 | seq_printf(seq, ",name=%s", root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1094 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1095 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1096 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1097 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1098 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1099 | /* User explicitly requested empty subsystem */ |
| 1100 | bool none; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | }; |
| 1102 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1103 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1104 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1105 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1106 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1107 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1108 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1109 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1111 | char *token, *o = data; |
| 1112 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1113 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1114 | struct cgroup_subsys *ss; |
| 1115 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1116 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1117 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1118 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1119 | #ifdef CONFIG_CPUSETS |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1120 | mask = ~(1UL << cpuset_cgrp_id); |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1121 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1122 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1123 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1124 | |
| 1125 | while ((token = strsep(&o, ",")) != NULL) { |
| 1126 | if (!*token) |
| 1127 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1128 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1129 | /* Explicitly have no subsystems */ |
| 1130 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1131 | continue; |
| 1132 | } |
| 1133 | if (!strcmp(token, "all")) { |
| 1134 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1135 | if (one_ss) |
| 1136 | return -EINVAL; |
| 1137 | all_ss = true; |
| 1138 | continue; |
| 1139 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1140 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1141 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1142 | continue; |
| 1143 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1144 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1145 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1146 | continue; |
| 1147 | } |
| 1148 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1149 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1150 | continue; |
| 1151 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1152 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1153 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1154 | continue; |
| 1155 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1156 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1157 | /* Specifying two release agents is forbidden */ |
| 1158 | if (opts->release_agent) |
| 1159 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1160 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1161 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1162 | if (!opts->release_agent) |
| 1163 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1164 | continue; |
| 1165 | } |
| 1166 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1167 | const char *name = token + 5; |
| 1168 | /* Can't specify an empty name */ |
| 1169 | if (!strlen(name)) |
| 1170 | return -EINVAL; |
| 1171 | /* Must match [\w.-]+ */ |
| 1172 | for (i = 0; i < strlen(name); i++) { |
| 1173 | char c = name[i]; |
| 1174 | if (isalnum(c)) |
| 1175 | continue; |
| 1176 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1177 | continue; |
| 1178 | return -EINVAL; |
| 1179 | } |
| 1180 | /* Specifying two names is forbidden */ |
| 1181 | if (opts->name) |
| 1182 | return -EINVAL; |
| 1183 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1184 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1185 | GFP_KERNEL); |
| 1186 | if (!opts->name) |
| 1187 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1188 | |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1192 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1193 | if (strcmp(token, ss->name)) |
| 1194 | continue; |
| 1195 | if (ss->disabled) |
| 1196 | continue; |
| 1197 | |
| 1198 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1199 | if (all_ss) |
| 1200 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1201 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1202 | one_ss = true; |
| 1203 | |
| 1204 | break; |
| 1205 | } |
| 1206 | if (i == CGROUP_SUBSYS_COUNT) |
| 1207 | return -ENOENT; |
| 1208 | } |
| 1209 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1210 | /* Consistency checks */ |
| 1211 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1212 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1213 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1214 | |
Tejun Heo | d3ba07c | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1215 | if ((opts->flags & (CGRP_ROOT_NOPREFIX | CGRP_ROOT_XATTR)) || |
| 1216 | opts->cpuset_clone_children || opts->release_agent || |
| 1217 | opts->name) { |
| 1218 | pr_err("cgroup: sane_behavior: noprefix, xattr, clone_children, release_agent and name are not allowed\n"); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1219 | return -EINVAL; |
| 1220 | } |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1221 | } else { |
| 1222 | /* |
| 1223 | * If the 'all' option was specified select all the |
| 1224 | * subsystems, otherwise if 'none', 'name=' and a subsystem |
| 1225 | * name options were not specified, let's default to 'all' |
| 1226 | */ |
| 1227 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1228 | for_each_subsys(ss, i) |
| 1229 | if (!ss->disabled) |
| 1230 | set_bit(i, &opts->subsys_mask); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1231 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1232 | /* |
| 1233 | * We either have to specify by name or by subsystems. (So |
| 1234 | * all empty hierarchies must have a name). |
| 1235 | */ |
| 1236 | if (!opts->subsys_mask && !opts->name) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1237 | return -EINVAL; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1240 | /* |
| 1241 | * Option noprefix was introduced just for backward compatibility |
| 1242 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1243 | * the cpuset subsystem. |
| 1244 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1245 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1246 | return -EINVAL; |
| 1247 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1248 | |
| 1249 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1250 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1251 | return -EINVAL; |
| 1252 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1253 | return 0; |
| 1254 | } |
| 1255 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1256 | static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1257 | { |
| 1258 | int ret = 0; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1259 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1260 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1261 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1262 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1263 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1264 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1265 | return -EINVAL; |
| 1266 | } |
| 1267 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1268 | mutex_lock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1269 | mutex_lock(&cgroup_mutex); |
| 1270 | |
| 1271 | /* See what subsystems are wanted */ |
| 1272 | ret = parse_cgroupfs_options(data, &opts); |
| 1273 | if (ret) |
| 1274 | goto out_unlock; |
| 1275 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1276 | if (opts.subsys_mask != root->cgrp.subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1277 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1278 | task_tgid_nr(current), current->comm); |
| 1279 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1280 | added_mask = opts.subsys_mask & ~root->cgrp.subsys_mask; |
| 1281 | removed_mask = root->cgrp.subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1282 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1283 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1284 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1286 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1287 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1288 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1290 | goto out_unlock; |
| 1291 | } |
| 1292 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1293 | /* remounting is not allowed for populated hierarchies */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1294 | if (!list_empty(&root->cgrp.children)) { |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1295 | ret = -EBUSY; |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1296 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1297 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1298 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1299 | ret = rebind_subsystems(root, added_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1300 | if (ret) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1301 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1302 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1303 | rebind_subsystems(&cgrp_dfl_root, removed_mask); |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1304 | |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1305 | if (opts.release_agent) { |
| 1306 | spin_lock(&release_agent_path_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1307 | strcpy(root->release_agent_path, opts.release_agent); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1308 | spin_unlock(&release_agent_path_lock); |
| 1309 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1310 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1311 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1312 | kfree(opts.name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1313 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1314 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1315 | return ret; |
| 1316 | } |
| 1317 | |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1318 | /* |
| 1319 | * To reduce the fork() overhead for systems that are not actually using |
| 1320 | * their cgroups capability, we don't maintain the lists running through |
| 1321 | * each css_set to its tasks until we see the list actually used - in other |
| 1322 | * words after the first mount. |
| 1323 | */ |
| 1324 | static bool use_task_css_set_links __read_mostly; |
| 1325 | |
| 1326 | static void cgroup_enable_task_cg_lists(void) |
| 1327 | { |
| 1328 | struct task_struct *p, *g; |
| 1329 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1330 | down_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1331 | |
| 1332 | if (use_task_css_set_links) |
| 1333 | goto out_unlock; |
| 1334 | |
| 1335 | use_task_css_set_links = true; |
| 1336 | |
| 1337 | /* |
| 1338 | * We need tasklist_lock because RCU is not safe against |
| 1339 | * while_each_thread(). Besides, a forking task that has passed |
| 1340 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 1341 | * is not guaranteed to have its child immediately visible in the |
| 1342 | * tasklist if we walk through it with RCU. |
| 1343 | */ |
| 1344 | read_lock(&tasklist_lock); |
| 1345 | do_each_thread(g, p) { |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1346 | WARN_ON_ONCE(!list_empty(&p->cg_list) || |
| 1347 | task_css_set(p) != &init_css_set); |
| 1348 | |
| 1349 | /* |
| 1350 | * We should check if the process is exiting, otherwise |
| 1351 | * it will race with cgroup_exit() in that the list |
| 1352 | * entry won't be deleted though the process has exited. |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1353 | * Do it while holding siglock so that we don't end up |
| 1354 | * racing against cgroup_exit(). |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1355 | */ |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1356 | spin_lock_irq(&p->sighand->siglock); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1357 | if (!(p->flags & PF_EXITING)) { |
| 1358 | struct css_set *cset = task_css_set(p); |
| 1359 | |
| 1360 | list_add(&p->cg_list, &cset->tasks); |
| 1361 | get_css_set(cset); |
| 1362 | } |
Tejun Heo | f153ad1 | 2014-02-25 09:56:49 -0500 | [diff] [blame] | 1363 | spin_unlock_irq(&p->sighand->siglock); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1364 | } while_each_thread(g, p); |
| 1365 | read_unlock(&tasklist_lock); |
| 1366 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1367 | up_write(&css_set_rwsem); |
Tejun Heo | afeb0f9 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 1368 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1369 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1370 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1371 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1372 | atomic_set(&cgrp->refcnt, 1); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1373 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1374 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1375 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1376 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1377 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1378 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1379 | cgrp->dummy_css.cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1380 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1381 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1382 | static void init_cgroup_root(struct cgroup_root *root, |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1383 | struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1384 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1385 | struct cgroup *cgrp = &root->cgrp; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1386 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1387 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1388 | atomic_set(&root->nr_cgrps, 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1389 | cgrp->root = root; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1390 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1391 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1392 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1393 | root->flags = opts->flags; |
| 1394 | if (opts->release_agent) |
| 1395 | strcpy(root->release_agent_path, opts->release_agent); |
| 1396 | if (opts->name) |
| 1397 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1398 | if (opts->cpuset_clone_children) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1399 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1402 | static int cgroup_setup_root(struct cgroup_root *root, unsigned long ss_mask) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1403 | { |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1404 | LIST_HEAD(tmp_links); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1405 | struct cgroup *root_cgrp = &root->cgrp; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1406 | struct css_set *cset; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1407 | int i, ret; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1408 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1409 | lockdep_assert_held(&cgroup_tree_mutex); |
| 1410 | lockdep_assert_held(&cgroup_mutex); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1411 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1412 | ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL); |
| 1413 | if (ret < 0) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1414 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1415 | root_cgrp->id = ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1416 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1417 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1418 | * We're accessing css_set_count without locking css_set_rwsem here, |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1419 | * but that's OK - it can only be increased by someone holding |
| 1420 | * cgroup_lock, and that's us. The worst that can happen is that we |
| 1421 | * have some link structures left over |
| 1422 | */ |
| 1423 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1424 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1425 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1426 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1427 | ret = cgroup_init_root_id(root); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1428 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1429 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1430 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1431 | root->kf_root = kernfs_create_root(&cgroup_kf_syscall_ops, |
| 1432 | KERNFS_ROOT_CREATE_DEACTIVATED, |
| 1433 | root_cgrp); |
| 1434 | if (IS_ERR(root->kf_root)) { |
| 1435 | ret = PTR_ERR(root->kf_root); |
| 1436 | goto exit_root_id; |
| 1437 | } |
| 1438 | root_cgrp->kn = root->kf_root->kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1439 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1440 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
| 1441 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1442 | goto destroy_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1443 | |
Tejun Heo | 5df3603 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1444 | ret = rebind_subsystems(root, ss_mask); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1445 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1446 | goto destroy_root; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1447 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1448 | /* |
| 1449 | * There must be no failure case after here, since rebinding takes |
| 1450 | * care of subsystems' refcounts, which are explicitly dropped in |
| 1451 | * the failure exit path. |
| 1452 | */ |
| 1453 | list_add(&root->root_list, &cgroup_roots); |
| 1454 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1455 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1456 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1457 | * Link the root cgroup in this hierarchy into all the css_set |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1458 | * objects. |
| 1459 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1460 | down_write(&css_set_rwsem); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1461 | hash_for_each(css_set_table, i, cset, hlist) |
| 1462 | link_css_set(&tmp_links, cset, root_cgrp); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1463 | up_write(&css_set_rwsem); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1464 | |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1465 | BUG_ON(!list_empty(&root_cgrp->children)); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1466 | BUG_ON(atomic_read(&root->nr_cgrps) != 1); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1467 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1468 | kernfs_activate(root_cgrp->kn); |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1469 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1470 | goto out; |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1471 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1472 | destroy_root: |
| 1473 | kernfs_destroy_root(root->kf_root); |
| 1474 | root->kf_root = NULL; |
| 1475 | exit_root_id: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1476 | cgroup_exit_root_id(root); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1477 | out: |
Tejun Heo | d427dfe | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1478 | free_cgrp_cset_links(&tmp_links); |
| 1479 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1482 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1483 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1484 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1485 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1486 | struct cgroup_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1487 | struct cgroup_sb_opts opts; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1488 | struct dentry *dentry; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1489 | int ret; |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1490 | bool new_sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1491 | |
| 1492 | /* |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1493 | * The first time anyone tries to mount a cgroup, enable the list |
| 1494 | * linking each css_set to its tasks and fix up all existing tasks. |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1495 | */ |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1496 | if (!use_task_css_set_links) |
| 1497 | cgroup_enable_task_cg_lists(); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1498 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1499 | mutex_lock(&cgroup_tree_mutex); |
| 1500 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1501 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1502 | /* First find the desired set of subsystems */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1503 | ret = parse_cgroupfs_options(data, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1504 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1505 | goto out_unlock; |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1506 | retry: |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1507 | /* look for a matching existing root */ |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 1508 | if (!opts.subsys_mask && !opts.none && !opts.name) { |
| 1509 | cgrp_dfl_root_visible = true; |
| 1510 | root = &cgrp_dfl_root; |
| 1511 | cgroup_get(&root->cgrp); |
| 1512 | ret = 0; |
| 1513 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1516 | for_each_root(root) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1517 | bool name_match = false; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1518 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1519 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1520 | continue; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1521 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1522 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1523 | * If we asked for a name then it must match. Also, if |
| 1524 | * name matches but sybsys_mask doesn't, we should fail. |
| 1525 | * Remember whether name matched. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1526 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1527 | if (opts.name) { |
| 1528 | if (strcmp(opts.name, root->name)) |
| 1529 | continue; |
| 1530 | name_match = true; |
| 1531 | } |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1532 | |
| 1533 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1534 | * If we asked for subsystems (or explicitly for no |
| 1535 | * subsystems) then they must match. |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1536 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1537 | if ((opts.subsys_mask || opts.none) && |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1538 | (opts.subsys_mask != root->cgrp.subsys_mask)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1539 | if (!name_match) |
| 1540 | continue; |
| 1541 | ret = -EBUSY; |
| 1542 | goto out_unlock; |
| 1543 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1544 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1545 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1546 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1547 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1548 | ret = -EINVAL; |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1549 | goto out_unlock; |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1550 | } else { |
| 1551 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1552 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1553 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1554 | |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1555 | /* |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1556 | * A root's lifetime is governed by its root cgroup. Zero |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1557 | * ref indicate that the root is being destroyed. Wait for |
| 1558 | * destruction to complete so that the subsystems are free. |
| 1559 | * We can use wait_queue for the wait but this path is |
| 1560 | * super cold. Let's just sleep for a bit and retry. |
| 1561 | */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1562 | if (!atomic_inc_not_zero(&root->cgrp.refcnt)) { |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1563 | mutex_unlock(&cgroup_mutex); |
| 1564 | mutex_unlock(&cgroup_tree_mutex); |
| 1565 | msleep(10); |
Li Zefan | e37a06f | 2014-04-17 13:53:08 +0800 | [diff] [blame] | 1566 | mutex_lock(&cgroup_tree_mutex); |
| 1567 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 776f02f | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1568 | goto retry; |
| 1569 | } |
| 1570 | |
| 1571 | ret = 0; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1572 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1575 | /* |
| 1576 | * No such thing, create a new one. name= matching without subsys |
| 1577 | * specification is allowed for already existing hierarchies but we |
| 1578 | * can't create new one without subsys specification. |
| 1579 | */ |
| 1580 | if (!opts.subsys_mask && !opts.none) { |
| 1581 | ret = -EINVAL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1582 | goto out_unlock; |
| 1583 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1584 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 1585 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1586 | if (!root) { |
| 1587 | ret = -ENOMEM; |
| 1588 | goto out_unlock; |
| 1589 | } |
| 1590 | |
| 1591 | init_cgroup_root(root, &opts); |
| 1592 | |
Tejun Heo | 3558557 | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 1593 | ret = cgroup_setup_root(root, opts.subsys_mask); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1594 | if (ret) |
| 1595 | cgroup_free_root(root); |
| 1596 | |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1597 | out_unlock: |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1598 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 1599 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1600 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1601 | kfree(opts.release_agent); |
| 1602 | kfree(opts.name); |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1603 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1604 | if (ret) |
Tejun Heo | 8e30e2b | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 1605 | return ERR_PTR(ret); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1606 | |
Li Zefan | c6b3d5b | 2014-04-04 17:14:41 +0800 | [diff] [blame] | 1607 | dentry = kernfs_mount(fs_type, flags, root->kf_root, &new_sb); |
| 1608 | if (IS_ERR(dentry) || !new_sb) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1609 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1610 | return dentry; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 1613 | static void cgroup_kill_sb(struct super_block *sb) |
| 1614 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1615 | struct kernfs_root *kf_root = kernfs_root_from_sb(sb); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1616 | struct cgroup_root *root = cgroup_root_from_kf(kf_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1617 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1618 | cgroup_put(&root->cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 1619 | kernfs_kill_sb(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | static struct file_system_type cgroup_fs_type = { |
| 1623 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1624 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1625 | .kill_sb = cgroup_kill_sb, |
| 1626 | }; |
| 1627 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1628 | static struct kobject *cgroup_kobj; |
| 1629 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1630 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1631 | * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1632 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1633 | * @buf: the buffer to write the path into |
| 1634 | * @buflen: the length of the buffer |
| 1635 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1636 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1637 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1638 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1639 | * cgroup controller callbacks. |
| 1640 | * |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1641 | * Return value is the same as kernfs_path(). |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1642 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1643 | char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1644 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 1645 | struct cgroup_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1646 | struct cgroup *cgrp; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1647 | int hierarchy_id = 1; |
| 1648 | char *path = NULL; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1649 | |
| 1650 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1651 | down_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1652 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1653 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1654 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1655 | if (root) { |
| 1656 | cgrp = task_cgroup_from_root(task, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1657 | path = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1658 | } else { |
| 1659 | /* if no hierarchy exists, everyone is in "/" */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1660 | if (strlcpy(buf, "/", buflen) < buflen) |
| 1661 | path = buf; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 1664 | up_read(&css_set_rwsem); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1665 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 1666 | return path; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1667 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1668 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1669 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1670 | /* used to track tasks and other necessary states during migration */ |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1671 | struct cgroup_taskset { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1672 | /* the src and dst cset list running through cset->mg_node */ |
| 1673 | struct list_head src_csets; |
| 1674 | struct list_head dst_csets; |
| 1675 | |
| 1676 | /* |
| 1677 | * Fields for cgroup_taskset_*() iteration. |
| 1678 | * |
| 1679 | * Before migration is committed, the target migration tasks are on |
| 1680 | * ->mg_tasks of the csets on ->src_csets. After, on ->mg_tasks of |
| 1681 | * the csets on ->dst_csets. ->csets point to either ->src_csets |
| 1682 | * or ->dst_csets depending on whether migration is committed. |
| 1683 | * |
| 1684 | * ->cur_csets and ->cur_task point to the current task position |
| 1685 | * during iteration. |
| 1686 | */ |
| 1687 | struct list_head *csets; |
| 1688 | struct css_set *cur_cset; |
| 1689 | struct task_struct *cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1690 | }; |
| 1691 | |
| 1692 | /** |
| 1693 | * cgroup_taskset_first - reset taskset and return the first task |
| 1694 | * @tset: taskset of interest |
| 1695 | * |
| 1696 | * @tset iteration is initialized and the first task is returned. |
| 1697 | */ |
| 1698 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1699 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1700 | tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); |
| 1701 | tset->cur_task = NULL; |
| 1702 | |
| 1703 | return cgroup_taskset_next(tset); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1704 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1705 | |
| 1706 | /** |
| 1707 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1708 | * @tset: taskset of interest |
| 1709 | * |
| 1710 | * Return the next task in @tset. Iteration must have been initialized |
| 1711 | * with cgroup_taskset_first(). |
| 1712 | */ |
| 1713 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1714 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1715 | struct css_set *cset = tset->cur_cset; |
| 1716 | struct task_struct *task = tset->cur_task; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1717 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1718 | while (&cset->mg_node != tset->csets) { |
| 1719 | if (!task) |
| 1720 | task = list_first_entry(&cset->mg_tasks, |
| 1721 | struct task_struct, cg_list); |
| 1722 | else |
| 1723 | task = list_next_entry(task, cg_list); |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1724 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1725 | if (&task->cg_list != &cset->mg_tasks) { |
| 1726 | tset->cur_cset = cset; |
| 1727 | tset->cur_task = task; |
| 1728 | return task; |
| 1729 | } |
| 1730 | |
| 1731 | cset = list_next_entry(cset, mg_node); |
| 1732 | task = NULL; |
| 1733 | } |
| 1734 | |
| 1735 | return NULL; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1736 | } |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1737 | |
| 1738 | /** |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1739 | * cgroup_task_migrate - move a task from one cgroup to another. |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1740 | * @old_cgrp; the cgroup @tsk is being migrated from |
| 1741 | * @tsk: the task being migrated |
| 1742 | * @new_cset: the new css_set @tsk is being attached to |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1743 | * |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1744 | * Must be called with cgroup_mutex, threadgroup and css_set_rwsem locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1745 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1746 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1747 | struct task_struct *tsk, |
| 1748 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1749 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1750 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1751 | |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1752 | lockdep_assert_held(&cgroup_mutex); |
| 1753 | lockdep_assert_held(&css_set_rwsem); |
| 1754 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1755 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1756 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1757 | * setting such that we can't race against cgroup_exit() changing the |
| 1758 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1759 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1760 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1761 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1762 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1763 | get_css_set(new_cset); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1764 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1765 | |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1766 | /* |
| 1767 | * Use move_tail so that cgroup_taskset_first() still returns the |
| 1768 | * leader after migration. This works because cgroup_migrate() |
| 1769 | * ensures that the dst_cset of the leader is the first on the |
| 1770 | * tset's dst_csets list. |
| 1771 | */ |
| 1772 | list_move_tail(&tsk->cg_list, &new_cset->mg_tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1773 | |
| 1774 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1775 | * We just gained a reference on old_cset by taking it from the |
| 1776 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1777 | * we're safe to drop it here; it will be freed under RCU. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1778 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1779 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1780 | put_css_set_locked(old_cset, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1783 | /** |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1784 | * cgroup_migrate_finish - cleanup after attach |
| 1785 | * @preloaded_csets: list of preloaded css_sets |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1786 | * |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1787 | * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See |
| 1788 | * those functions for details. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1789 | */ |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1790 | static void cgroup_migrate_finish(struct list_head *preloaded_csets) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1791 | { |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1792 | struct css_set *cset, *tmp_cset; |
| 1793 | |
| 1794 | lockdep_assert_held(&cgroup_mutex); |
| 1795 | |
| 1796 | down_write(&css_set_rwsem); |
| 1797 | list_for_each_entry_safe(cset, tmp_cset, preloaded_csets, mg_preload_node) { |
| 1798 | cset->mg_src_cgrp = NULL; |
| 1799 | cset->mg_dst_cset = NULL; |
| 1800 | list_del_init(&cset->mg_preload_node); |
| 1801 | put_css_set_locked(cset, false); |
| 1802 | } |
| 1803 | up_write(&css_set_rwsem); |
| 1804 | } |
| 1805 | |
| 1806 | /** |
| 1807 | * cgroup_migrate_add_src - add a migration source css_set |
| 1808 | * @src_cset: the source css_set to add |
| 1809 | * @dst_cgrp: the destination cgroup |
| 1810 | * @preloaded_csets: list of preloaded css_sets |
| 1811 | * |
| 1812 | * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin |
| 1813 | * @src_cset and add it to @preloaded_csets, which should later be cleaned |
| 1814 | * up by cgroup_migrate_finish(). |
| 1815 | * |
| 1816 | * This function may be called without holding threadgroup_lock even if the |
| 1817 | * target is a process. Threads may be created and destroyed but as long |
| 1818 | * as cgroup_mutex is not dropped, no new css_set can be put into play and |
| 1819 | * the preloaded css_sets are guaranteed to cover all migrations. |
| 1820 | */ |
| 1821 | static void cgroup_migrate_add_src(struct css_set *src_cset, |
| 1822 | struct cgroup *dst_cgrp, |
| 1823 | struct list_head *preloaded_csets) |
| 1824 | { |
| 1825 | struct cgroup *src_cgrp; |
| 1826 | |
| 1827 | lockdep_assert_held(&cgroup_mutex); |
| 1828 | lockdep_assert_held(&css_set_rwsem); |
| 1829 | |
| 1830 | src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); |
| 1831 | |
| 1832 | /* nothing to do if this cset already belongs to the cgroup */ |
| 1833 | if (src_cgrp == dst_cgrp) |
| 1834 | return; |
| 1835 | |
| 1836 | if (!list_empty(&src_cset->mg_preload_node)) |
| 1837 | return; |
| 1838 | |
| 1839 | WARN_ON(src_cset->mg_src_cgrp); |
| 1840 | WARN_ON(!list_empty(&src_cset->mg_tasks)); |
| 1841 | WARN_ON(!list_empty(&src_cset->mg_node)); |
| 1842 | |
| 1843 | src_cset->mg_src_cgrp = src_cgrp; |
| 1844 | get_css_set(src_cset); |
| 1845 | list_add(&src_cset->mg_preload_node, preloaded_csets); |
| 1846 | } |
| 1847 | |
| 1848 | /** |
| 1849 | * cgroup_migrate_prepare_dst - prepare destination css_sets for migration |
| 1850 | * @dst_cgrp: the destination cgroup |
| 1851 | * @preloaded_csets: list of preloaded source css_sets |
| 1852 | * |
| 1853 | * Tasks are about to be moved to @dst_cgrp and all the source css_sets |
| 1854 | * have been preloaded to @preloaded_csets. This function looks up and |
| 1855 | * pins all destination css_sets, links each to its source, and put them on |
| 1856 | * @preloaded_csets. |
| 1857 | * |
| 1858 | * This function must be called after cgroup_migrate_add_src() has been |
| 1859 | * called on each migration source css_set. After migration is performed |
| 1860 | * using cgroup_migrate(), cgroup_migrate_finish() must be called on |
| 1861 | * @preloaded_csets. |
| 1862 | */ |
| 1863 | static int cgroup_migrate_prepare_dst(struct cgroup *dst_cgrp, |
| 1864 | struct list_head *preloaded_csets) |
| 1865 | { |
| 1866 | LIST_HEAD(csets); |
| 1867 | struct css_set *src_cset; |
| 1868 | |
| 1869 | lockdep_assert_held(&cgroup_mutex); |
| 1870 | |
| 1871 | /* look up the dst cset for each src cset and link it to src */ |
| 1872 | list_for_each_entry(src_cset, preloaded_csets, mg_preload_node) { |
| 1873 | struct css_set *dst_cset; |
| 1874 | |
| 1875 | dst_cset = find_css_set(src_cset, dst_cgrp); |
| 1876 | if (!dst_cset) |
| 1877 | goto err; |
| 1878 | |
| 1879 | WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); |
| 1880 | src_cset->mg_dst_cset = dst_cset; |
| 1881 | |
| 1882 | if (list_empty(&dst_cset->mg_preload_node)) |
| 1883 | list_add(&dst_cset->mg_preload_node, &csets); |
| 1884 | else |
| 1885 | put_css_set(dst_cset, false); |
| 1886 | } |
| 1887 | |
| 1888 | list_splice(&csets, preloaded_csets); |
| 1889 | return 0; |
| 1890 | err: |
| 1891 | cgroup_migrate_finish(&csets); |
| 1892 | return -ENOMEM; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * cgroup_migrate - migrate a process or task to a cgroup |
| 1897 | * @cgrp: the destination cgroup |
| 1898 | * @leader: the leader of the process or the task to migrate |
| 1899 | * @threadgroup: whether @leader points to the whole process or a single task |
| 1900 | * |
| 1901 | * Migrate a process or task denoted by @leader to @cgrp. If migrating a |
| 1902 | * process, the caller must be holding threadgroup_lock of @leader. The |
| 1903 | * caller is also responsible for invoking cgroup_migrate_add_src() and |
| 1904 | * cgroup_migrate_prepare_dst() on the targets before invoking this |
| 1905 | * function and following up with cgroup_migrate_finish(). |
| 1906 | * |
| 1907 | * As long as a controller's ->can_attach() doesn't fail, this function is |
| 1908 | * guaranteed to succeed. This means that, excluding ->can_attach() |
| 1909 | * failure, when migrating multiple targets, the success or failure can be |
| 1910 | * decided for all targets by invoking group_migrate_prepare_dst() before |
| 1911 | * actually starting migrating. |
| 1912 | */ |
| 1913 | static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader, |
| 1914 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1915 | { |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1916 | struct cgroup_taskset tset = { |
| 1917 | .src_csets = LIST_HEAD_INIT(tset.src_csets), |
| 1918 | .dst_csets = LIST_HEAD_INIT(tset.dst_csets), |
| 1919 | .csets = &tset.src_csets, |
| 1920 | }; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1921 | struct cgroup_subsys_state *css, *failed_css = NULL; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1922 | struct css_set *cset, *tmp_cset; |
| 1923 | struct task_struct *task, *tmp_task; |
| 1924 | int i, ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1925 | |
| 1926 | /* |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1927 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1928 | * already PF_EXITING could be freed from underneath us unless we |
| 1929 | * take an rcu_read_lock. |
| 1930 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1931 | down_write(&css_set_rwsem); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1932 | rcu_read_lock(); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1933 | task = leader; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | do { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1935 | /* @task either already exited or can't exit until the end */ |
| 1936 | if (task->flags & PF_EXITING) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1937 | goto next; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1938 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1939 | /* leave @task alone if post_fork() hasn't linked it yet */ |
| 1940 | if (list_empty(&task->cg_list)) |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1941 | goto next; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1942 | |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1943 | cset = task_css_set(task); |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1944 | if (!cset->mg_src_cgrp) |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1945 | goto next; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1946 | |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1947 | /* |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1948 | * cgroup_taskset_first() must always return the leader. |
| 1949 | * Take care to avoid disturbing the ordering. |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1950 | */ |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 1951 | list_move_tail(&task->cg_list, &cset->mg_tasks); |
| 1952 | if (list_empty(&cset->mg_node)) |
| 1953 | list_add_tail(&cset->mg_node, &tset.src_csets); |
| 1954 | if (list_empty(&cset->mg_dst_cset->mg_node)) |
| 1955 | list_move_tail(&cset->mg_dst_cset->mg_node, |
| 1956 | &tset.dst_csets); |
Anjana V Kumar | ea84753 | 2013-10-12 10:59:17 +0800 | [diff] [blame] | 1957 | next: |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1958 | if (!threadgroup) |
| 1959 | break; |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1960 | } while_each_thread(leader, task); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1961 | rcu_read_unlock(); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1962 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1964 | /* methods shouldn't be called if no task is actually migrating */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1965 | if (list_empty(&tset.src_csets)) |
| 1966 | return 0; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1967 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1968 | /* check that we can legitimately attach to the cgroup */ |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1969 | for_each_css(css, i, cgrp) { |
| 1970 | if (css->ss->can_attach) { |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 1971 | ret = css->ss->can_attach(css, &tset); |
| 1972 | if (ret) { |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1973 | failed_css = css; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | goto out_cancel_attach; |
| 1975 | } |
| 1976 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1980 | * Now that we're guaranteed success, proceed to move all tasks to |
| 1981 | * the new cgroup. There are no failure cases after here, so this |
| 1982 | * is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1983 | */ |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1984 | down_write(&css_set_rwsem); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1985 | list_for_each_entry(cset, &tset.src_csets, mg_node) { |
| 1986 | list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) |
| 1987 | cgroup_task_migrate(cset->mg_src_cgrp, task, |
| 1988 | cset->mg_dst_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1989 | } |
Tejun Heo | cb0f1fe | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 1990 | up_write(&css_set_rwsem); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1991 | |
| 1992 | /* |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 1993 | * Migration is committed, all target tasks are now on dst_csets. |
| 1994 | * Nothing is sensitive to fork() after this point. Notify |
| 1995 | * controllers that migration is complete. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1996 | */ |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 1997 | tset.csets = &tset.dst_csets; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1998 | |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 1999 | for_each_css(css, i, cgrp) |
| 2000 | if (css->ss->attach) |
| 2001 | css->ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2002 | |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2003 | ret = 0; |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2004 | goto out_release_tset; |
| 2005 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | out_cancel_attach: |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2007 | for_each_css(css, i, cgrp) { |
| 2008 | if (css == failed_css) |
| 2009 | break; |
| 2010 | if (css->ss->cancel_attach) |
| 2011 | css->ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | } |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2013 | out_release_tset: |
| 2014 | down_write(&css_set_rwsem); |
| 2015 | list_splice_init(&tset.dst_csets, &tset.src_csets); |
| 2016 | list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) { |
Tejun Heo | 1b9aba4 | 2014-03-19 17:43:21 -0400 | [diff] [blame] | 2017 | list_splice_tail_init(&cset->mg_tasks, &cset->tasks); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2018 | list_del_init(&cset->mg_node); |
Tejun Heo | b3dc094 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2019 | } |
| 2020 | up_write(&css_set_rwsem); |
Tejun Heo | 9db8de3 | 2014-02-13 06:58:43 -0500 | [diff] [blame] | 2021 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2024 | /** |
| 2025 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
| 2026 | * @dst_cgrp: the cgroup to attach to |
| 2027 | * @leader: the task or the leader of the threadgroup to be attached |
| 2028 | * @threadgroup: attach the whole threadgroup? |
| 2029 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2030 | * Call holding cgroup_mutex and threadgroup_lock of @leader. |
Tejun Heo | 1958d2d | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2031 | */ |
| 2032 | static int cgroup_attach_task(struct cgroup *dst_cgrp, |
| 2033 | struct task_struct *leader, bool threadgroup) |
| 2034 | { |
| 2035 | LIST_HEAD(preloaded_csets); |
| 2036 | struct task_struct *task; |
| 2037 | int ret; |
| 2038 | |
| 2039 | /* look up all src csets */ |
| 2040 | down_read(&css_set_rwsem); |
| 2041 | rcu_read_lock(); |
| 2042 | task = leader; |
| 2043 | do { |
| 2044 | cgroup_migrate_add_src(task_css_set(task), dst_cgrp, |
| 2045 | &preloaded_csets); |
| 2046 | if (!threadgroup) |
| 2047 | break; |
| 2048 | } while_each_thread(leader, task); |
| 2049 | rcu_read_unlock(); |
| 2050 | up_read(&css_set_rwsem); |
| 2051 | |
| 2052 | /* prepare dst csets and commit */ |
| 2053 | ret = cgroup_migrate_prepare_dst(dst_cgrp, &preloaded_csets); |
| 2054 | if (!ret) |
| 2055 | ret = cgroup_migrate(dst_cgrp, leader, threadgroup); |
| 2056 | |
| 2057 | cgroup_migrate_finish(&preloaded_csets); |
| 2058 | return ret; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * Find the task_struct of the task to attach by vpid and pass it along to the |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2063 | * function to attach either it or all tasks in its threadgroup. Will lock |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2064 | * cgroup_mutex and threadgroup. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | */ |
| 2066 | static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2067 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2068 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2069 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2070 | int ret; |
| 2071 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2072 | if (!cgroup_lock_live_group(cgrp)) |
| 2073 | return -ENODEV; |
| 2074 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2075 | retry_find_task: |
| 2076 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2077 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2078 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2080 | rcu_read_unlock(); |
SeongJae Park | dd4b0a4 | 2014-01-18 16:56:47 +0900 | [diff] [blame] | 2081 | ret = -ESRCH; |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2082 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2083 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | /* |
| 2085 | * even if we're attaching all tasks in the thread group, we |
| 2086 | * only need to check permissions on one of them. |
| 2087 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2088 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2089 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2090 | !uid_eq(cred->euid, tcred->uid) && |
| 2091 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2092 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2093 | ret = -EACCES; |
| 2094 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2095 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2096 | } else |
| 2097 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2098 | |
| 2099 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2100 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2101 | |
| 2102 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2103 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2104 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2105 | * with no rt_runtime allocated. Just say no. |
| 2106 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2107 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2108 | ret = -EINVAL; |
| 2109 | rcu_read_unlock(); |
| 2110 | goto out_unlock_cgroup; |
| 2111 | } |
| 2112 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2113 | get_task_struct(tsk); |
| 2114 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2115 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2116 | threadgroup_lock(tsk); |
| 2117 | if (threadgroup) { |
| 2118 | if (!thread_group_leader(tsk)) { |
| 2119 | /* |
| 2120 | * a race with de_thread from another thread's exec() |
| 2121 | * may strip us of our leadership, if this happens, |
| 2122 | * there is no choice but to throw this task away and |
| 2123 | * try again; this is |
| 2124 | * "double-double-toil-and-trouble-check locking". |
| 2125 | */ |
| 2126 | threadgroup_unlock(tsk); |
| 2127 | put_task_struct(tsk); |
| 2128 | goto retry_find_task; |
| 2129 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2133 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2134 | threadgroup_unlock(tsk); |
| 2135 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2136 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2137 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2138 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2139 | return ret; |
| 2140 | } |
| 2141 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2142 | /** |
| 2143 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2144 | * @from: attach to all cgroups of a given task |
| 2145 | * @tsk: the task to be attached |
| 2146 | */ |
| 2147 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2148 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2149 | struct cgroup_root *root; |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2150 | int retval = 0; |
| 2151 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2152 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2153 | for_each_root(root) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2154 | struct cgroup *from_cgrp; |
| 2155 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2156 | if (root == &cgrp_dfl_root) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 2157 | continue; |
| 2158 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2159 | down_read(&css_set_rwsem); |
| 2160 | from_cgrp = task_cgroup_from_root(from, root); |
| 2161 | up_read(&css_set_rwsem); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2162 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2163 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2164 | if (retval) |
| 2165 | break; |
| 2166 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2167 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2168 | |
| 2169 | return retval; |
| 2170 | } |
| 2171 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2172 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2173 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2174 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2175 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2176 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2179 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2180 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2181 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2182 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2185 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
Tejun Heo | 4d3bb51 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2186 | struct cftype *cft, char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2187 | { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2188 | struct cgroup_root *root = css->cgroup->root; |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2189 | |
| 2190 | BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2191 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2192 | return -ENODEV; |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2193 | spin_lock(&release_agent_path_lock); |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2194 | strlcpy(root->release_agent_path, buffer, |
| 2195 | sizeof(root->release_agent_path)); |
Tejun Heo | 69e943b | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 2196 | spin_unlock(&release_agent_path_lock); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2197 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2198 | return 0; |
| 2199 | } |
| 2200 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2201 | static int cgroup_release_agent_show(struct seq_file *seq, void *v) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2202 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2203 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2204 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2205 | if (!cgroup_lock_live_group(cgrp)) |
| 2206 | return -ENODEV; |
| 2207 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2208 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2209 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2210 | return 0; |
| 2211 | } |
| 2212 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2213 | static int cgroup_sane_behavior_show(struct seq_file *seq, void *v) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2214 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2215 | struct cgroup *cgrp = seq_css(seq)->cgroup; |
| 2216 | |
| 2217 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2218 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2221 | static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, |
| 2222 | size_t nbytes, loff_t off) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2223 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2224 | struct cgroup *cgrp = of->kn->parent->priv; |
| 2225 | struct cftype *cft = of->kn->priv; |
| 2226 | struct cgroup_subsys_state *css; |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2227 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2228 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2229 | /* |
| 2230 | * kernfs guarantees that a file isn't deleted with operations in |
| 2231 | * flight, which means that the matching css is and stays alive and |
| 2232 | * doesn't need to be pinned. The RCU locking is not necessary |
| 2233 | * either. It's just for the convenience of using cgroup_css(). |
| 2234 | */ |
| 2235 | rcu_read_lock(); |
| 2236 | css = cgroup_css(cgrp, cft->ss); |
| 2237 | rcu_read_unlock(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2238 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2239 | if (cft->write_string) { |
| 2240 | ret = cft->write_string(css, cft, strstrip(buf)); |
| 2241 | } else if (cft->write_u64) { |
| 2242 | unsigned long long v; |
| 2243 | ret = kstrtoull(buf, 0, &v); |
| 2244 | if (!ret) |
| 2245 | ret = cft->write_u64(css, cft, v); |
| 2246 | } else if (cft->write_s64) { |
| 2247 | long long v; |
| 2248 | ret = kstrtoll(buf, 0, &v); |
| 2249 | if (!ret) |
| 2250 | ret = cft->write_s64(css, cft, v); |
| 2251 | } else if (cft->trigger) { |
| 2252 | ret = cft->trigger(css, (unsigned int)cft->private); |
| 2253 | } else { |
| 2254 | ret = -EINVAL; |
| 2255 | } |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2256 | |
Tejun Heo | a742c59 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 2257 | return ret ?: nbytes; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2260 | static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2261 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2262 | return seq_cft(seq)->seq_start(seq, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) |
| 2266 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2267 | return seq_cft(seq)->seq_next(seq, v, ppos); |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | static void cgroup_seqfile_stop(struct seq_file *seq, void *v) |
| 2271 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2272 | seq_cft(seq)->seq_stop(seq, v); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2276 | { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2277 | struct cftype *cft = seq_cft(m); |
| 2278 | struct cgroup_subsys_state *css = seq_css(m); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2279 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2280 | if (cft->seq_show) |
| 2281 | return cft->seq_show(m, arg); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2282 | |
Tejun Heo | 896f519 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 2283 | if (cft->read_u64) |
| 2284 | seq_printf(m, "%llu\n", cft->read_u64(css, cft)); |
| 2285 | else if (cft->read_s64) |
| 2286 | seq_printf(m, "%lld\n", cft->read_s64(css, cft)); |
| 2287 | else |
| 2288 | return -EINVAL; |
| 2289 | return 0; |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2292 | static struct kernfs_ops cgroup_kf_single_ops = { |
| 2293 | .atomic_write_len = PAGE_SIZE, |
| 2294 | .write = cgroup_file_write, |
| 2295 | .seq_show = cgroup_seqfile_show, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2296 | }; |
| 2297 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2298 | static struct kernfs_ops cgroup_kf_ops = { |
| 2299 | .atomic_write_len = PAGE_SIZE, |
| 2300 | .write = cgroup_file_write, |
| 2301 | .seq_start = cgroup_seqfile_start, |
| 2302 | .seq_next = cgroup_seqfile_next, |
| 2303 | .seq_stop = cgroup_seqfile_stop, |
| 2304 | .seq_show = cgroup_seqfile_show, |
| 2305 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2306 | |
| 2307 | /* |
| 2308 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2309 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2310 | static int cgroup_rename(struct kernfs_node *kn, struct kernfs_node *new_parent, |
| 2311 | const char *new_name_str) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2312 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2313 | struct cgroup *cgrp = kn->priv; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2314 | int ret; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2315 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2316 | if (kernfs_type(kn) != KERNFS_DIR) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2317 | return -ENOTDIR; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2318 | if (kn->parent != new_parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2319 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2320 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2321 | /* |
| 2322 | * This isn't a proper migration and its usefulness is very |
| 2323 | * limited. Disallow if sane_behavior. |
| 2324 | */ |
| 2325 | if (cgroup_sane_behavior(cgrp)) |
| 2326 | return -EPERM; |
| 2327 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2328 | /* |
| 2329 | * We're gonna grab cgroup_tree_mutex which nests outside kernfs |
| 2330 | * active_ref. kernfs_rename() doesn't require active_ref |
| 2331 | * protection. Break them before grabbing cgroup_tree_mutex. |
| 2332 | */ |
| 2333 | kernfs_break_active_protection(new_parent); |
| 2334 | kernfs_break_active_protection(kn); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2335 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2336 | mutex_lock(&cgroup_tree_mutex); |
| 2337 | mutex_lock(&cgroup_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2338 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2339 | ret = kernfs_rename(kn, new_parent, new_name_str); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2340 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2341 | mutex_unlock(&cgroup_mutex); |
| 2342 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2343 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 2344 | kernfs_unbreak_active_protection(kn); |
| 2345 | kernfs_unbreak_active_protection(new_parent); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2346 | return ret; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2349 | /* set uid and gid of cgroup dirs and files to that of the creator */ |
| 2350 | static int cgroup_kn_set_ugid(struct kernfs_node *kn) |
| 2351 | { |
| 2352 | struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, |
| 2353 | .ia_uid = current_fsuid(), |
| 2354 | .ia_gid = current_fsgid(), }; |
| 2355 | |
| 2356 | if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && |
| 2357 | gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) |
| 2358 | return 0; |
| 2359 | |
| 2360 | return kernfs_setattr(kn, &iattr); |
| 2361 | } |
| 2362 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2363 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2364 | { |
Tejun Heo | 8d7e6fb | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2365 | char name[CGROUP_FILE_NAME_MAX]; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2366 | struct kernfs_node *kn; |
| 2367 | struct lock_class_key *key = NULL; |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2368 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2369 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2370 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2371 | key = &cft->lockdep_key; |
| 2372 | #endif |
| 2373 | kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), |
| 2374 | cgroup_file_mode(cft), 0, cft->kf_ops, cft, |
| 2375 | NULL, false, key); |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 2376 | if (IS_ERR(kn)) |
| 2377 | return PTR_ERR(kn); |
| 2378 | |
| 2379 | ret = cgroup_kn_set_ugid(kn); |
| 2380 | if (ret) |
| 2381 | kernfs_remove(kn); |
| 2382 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2383 | } |
| 2384 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2385 | /** |
| 2386 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2387 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2388 | * @cfts: array of cftypes to be added |
| 2389 | * @is_add: whether to add or remove |
| 2390 | * |
| 2391 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2392 | * For removals, this function never fails. If addition fails, this |
| 2393 | * function doesn't remove files already added. The caller is responsible |
| 2394 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2395 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2396 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2397 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2398 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2399 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2400 | int ret; |
| 2401 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2402 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2403 | |
| 2404 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2405 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 8cbbf2c | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 2406 | if ((cft->flags & CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) |
| 2407 | continue; |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2408 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2409 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2410 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2411 | continue; |
| 2412 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2413 | continue; |
| 2414 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2415 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2416 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2417 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2418 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2419 | cft->name, ret); |
| 2420 | return ret; |
| 2421 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2422 | } else { |
| 2423 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2424 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2425 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2426 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2429 | static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2430 | { |
| 2431 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2432 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2433 | struct cgroup *root = &ss->root->cgrp; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2434 | struct cgroup_subsys_state *css; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2435 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2436 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2437 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2438 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2439 | /* don't bother if @ss isn't attached */ |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 2440 | if (ss->root == &cgrp_dfl_root) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2441 | return 0; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2442 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2443 | /* add/rm files for all cgroups created before */ |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2444 | css_for_each_descendant_pre(css, cgroup_css(root, ss)) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2445 | struct cgroup *cgrp = css->cgroup; |
| 2446 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2447 | if (cgroup_is_dead(cgrp)) |
| 2448 | continue; |
| 2449 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2450 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2451 | if (ret) |
| 2452 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2453 | } |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2454 | |
| 2455 | if (is_add && !ret) |
| 2456 | kernfs_activate(root->kn); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2457 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2460 | static void cgroup_exit_cftypes(struct cftype *cfts) |
| 2461 | { |
| 2462 | struct cftype *cft; |
| 2463 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2464 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2465 | /* free copy for custom atomic_write_len, see init_cftypes() */ |
| 2466 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) |
| 2467 | kfree(cft->kf_ops); |
| 2468 | cft->kf_ops = NULL; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2469 | cft->ss = NULL; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2470 | } |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2471 | } |
| 2472 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2473 | static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2474 | { |
| 2475 | struct cftype *cft; |
| 2476 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2477 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
| 2478 | struct kernfs_ops *kf_ops; |
| 2479 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2480 | WARN_ON(cft->ss || cft->kf_ops); |
| 2481 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2482 | if (cft->seq_start) |
| 2483 | kf_ops = &cgroup_kf_ops; |
| 2484 | else |
| 2485 | kf_ops = &cgroup_kf_single_ops; |
| 2486 | |
| 2487 | /* |
| 2488 | * Ugh... if @cft wants a custom max_write_len, we need to |
| 2489 | * make a copy of kf_ops to set its atomic_write_len. |
| 2490 | */ |
| 2491 | if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { |
| 2492 | kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); |
| 2493 | if (!kf_ops) { |
| 2494 | cgroup_exit_cftypes(cfts); |
| 2495 | return -ENOMEM; |
| 2496 | } |
| 2497 | kf_ops->atomic_write_len = cft->max_write_len; |
| 2498 | } |
| 2499 | |
| 2500 | cft->kf_ops = kf_ops; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2501 | cft->ss = ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | return 0; |
Tejun Heo | 2da440a | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 2505 | } |
| 2506 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2507 | static int cgroup_rm_cftypes_locked(struct cftype *cfts) |
| 2508 | { |
| 2509 | lockdep_assert_held(&cgroup_tree_mutex); |
| 2510 | |
| 2511 | if (!cfts || !cfts[0].ss) |
| 2512 | return -ENOENT; |
| 2513 | |
| 2514 | list_del(&cfts->node); |
| 2515 | cgroup_apply_cftypes(cfts, false); |
| 2516 | cgroup_exit_cftypes(cfts); |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2520 | /** |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2521 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2522 | * @cfts: zero-length name terminated array of cftypes |
| 2523 | * |
| 2524 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2525 | * existing cgroups and all future cgroups won't have them either. This |
| 2526 | * function can be called anytime whether @cfts' subsys is attached or not. |
| 2527 | * |
| 2528 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2529 | * registered. |
| 2530 | */ |
| 2531 | int cgroup_rm_cftypes(struct cftype *cfts) |
| 2532 | { |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2533 | int ret; |
Tejun Heo | 80b1358 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2534 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2535 | mutex_lock(&cgroup_tree_mutex); |
| 2536 | ret = cgroup_rm_cftypes_locked(cfts); |
| 2537 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2538 | return ret; |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2543 | * @ss: target cgroup subsystem |
| 2544 | * @cfts: zero-length name terminated array of cftypes |
| 2545 | * |
| 2546 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2547 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2548 | * have them too. This function can be called anytime whether @ss is |
| 2549 | * attached or not. |
| 2550 | * |
| 2551 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2552 | * function currently returns 0 as long as @cfts registration is successful |
| 2553 | * even if some file creation attempts on existing cgroups fail. |
| 2554 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2555 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2556 | { |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2557 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2558 | |
Li Zefan | dc5736e | 2014-02-17 10:41:50 +0800 | [diff] [blame] | 2559 | if (!cfts || cfts[0].name[0] == '\0') |
| 2560 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2561 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2562 | ret = cgroup_init_cftypes(ss, cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2563 | if (ret) |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 2564 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2565 | |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2566 | mutex_lock(&cgroup_tree_mutex); |
| 2567 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 2568 | list_add_tail(&cfts->node, &ss->cfts); |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2569 | ret = cgroup_apply_cftypes(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2570 | if (ret) |
Tejun Heo | 21a2d34 | 2014-02-12 09:29:49 -0500 | [diff] [blame] | 2571 | cgroup_rm_cftypes_locked(cfts); |
| 2572 | |
| 2573 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2574 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2575 | } |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2576 | |
| 2577 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2578 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2579 | * @cgrp: the cgroup in question |
| 2580 | * |
| 2581 | * Return the number of tasks in the cgroup. |
| 2582 | */ |
Tejun Heo | 07bc356 | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2583 | static int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2584 | { |
| 2585 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2586 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2587 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2588 | down_read(&css_set_rwsem); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2589 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2590 | count += atomic_read(&link->cset->refcount); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2591 | up_read(&css_set_rwsem); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2592 | return count; |
| 2593 | } |
| 2594 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2595 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2596 | * css_next_child - find the next child of a given css |
| 2597 | * @pos_css: the current position (%NULL to initiate traversal) |
| 2598 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2599 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2600 | * This function returns the next child of @parent_css and should be called |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2601 | * under either cgroup_mutex or RCU read lock. The only requirement is |
| 2602 | * that @parent_css and @pos_css are accessible. The next sibling is |
| 2603 | * guaranteed to be returned regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2604 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2605 | struct cgroup_subsys_state * |
| 2606 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 2607 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2608 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2609 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 2610 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2611 | struct cgroup *next; |
| 2612 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2613 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2614 | |
| 2615 | /* |
| 2616 | * @pos could already have been removed. Once a cgroup is removed, |
| 2617 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2618 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2619 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2620 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2621 | * finished its grace period even if it's already removed, and thus |
| 2622 | * safe to dereference from this RCU critical section. If |
| 2623 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 2624 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2625 | * |
| 2626 | * If @pos is dead, its next pointer can't be dereferenced; |
| 2627 | * however, as each cgroup is given a monotonically increasing |
| 2628 | * unique serial number and always appended to the sibling list, |
| 2629 | * the next one can be found by walking the parent's children until |
| 2630 | * we see a cgroup with higher serial number than @pos's. While |
| 2631 | * this path can be slower, it's taken only when either the current |
| 2632 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2633 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2634 | if (!pos) { |
| 2635 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 2636 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2637 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2638 | } else { |
| 2639 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 2640 | if (next->serial_nr > pos->serial_nr) |
| 2641 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2642 | } |
| 2643 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2644 | if (&next->sibling == &cgrp->children) |
| 2645 | return NULL; |
| 2646 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 2647 | return cgroup_css(next, parent_css->ss); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2648 | } |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2649 | |
| 2650 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2651 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2652 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2653 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2654 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2655 | * To be used by css_for_each_descendant_pre(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2656 | * to visit for pre-order traversal of @root's descendants. @root is |
| 2657 | * included in the iteration and the first node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2658 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2659 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2660 | * doesn't require the whole traversal to be contained in a single critical |
| 2661 | * section. This function will return the correct next descendant as long |
| 2662 | * as both @pos and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2663 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2664 | struct cgroup_subsys_state * |
| 2665 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 2666 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2667 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2668 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2669 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2670 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2671 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2672 | /* if first iteration, visit @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2673 | if (!pos) |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2674 | return root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2675 | |
| 2676 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2677 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2678 | if (next) |
| 2679 | return next; |
| 2680 | |
| 2681 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2682 | while (pos != root) { |
| 2683 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2684 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2685 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2686 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 2687 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2688 | |
| 2689 | return NULL; |
| 2690 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2691 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2692 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2693 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 2694 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2695 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2696 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 2697 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2698 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2699 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2700 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2701 | * doesn't require the whole traversal to be contained in a single critical |
| 2702 | * section. This function will return the correct rightmost descendant as |
| 2703 | * long as @pos is accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2704 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2705 | struct cgroup_subsys_state * |
| 2706 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2707 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2708 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2709 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2710 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2711 | |
| 2712 | do { |
| 2713 | last = pos; |
| 2714 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 2715 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2716 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2717 | pos = tmp; |
| 2718 | } while (pos); |
| 2719 | |
| 2720 | return last; |
| 2721 | } |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2722 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2723 | static struct cgroup_subsys_state * |
| 2724 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2725 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2726 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2727 | |
| 2728 | do { |
| 2729 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2730 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2731 | } while (pos); |
| 2732 | |
| 2733 | return last; |
| 2734 | } |
| 2735 | |
| 2736 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2737 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2738 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2739 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2740 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2741 | * To be used by css_for_each_descendant_post(). Find the next descendant |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2742 | * to visit for post-order traversal of @root's descendants. @root is |
| 2743 | * included in the iteration and the last node to be visited. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2744 | * |
Tejun Heo | 87fb54f | 2013-12-06 15:11:55 -0500 | [diff] [blame] | 2745 | * While this function requires cgroup_mutex or RCU read locking, it |
| 2746 | * doesn't require the whole traversal to be contained in a single critical |
| 2747 | * section. This function will return the correct next descendant as long |
| 2748 | * as both @pos and @cgroup are accessible and @pos is a descendant of |
| 2749 | * @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2750 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2751 | struct cgroup_subsys_state * |
| 2752 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 2753 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2754 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2755 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2756 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 2757 | cgroup_assert_mutexes_or_rcu_locked(); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2758 | |
Tejun Heo | 58b79a9 | 2013-09-06 15:31:08 -0400 | [diff] [blame] | 2759 | /* if first iteration, visit leftmost descendant which may be @root */ |
| 2760 | if (!pos) |
| 2761 | return css_leftmost_descendant(root); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2762 | |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2763 | /* if we visited @root, we're done */ |
| 2764 | if (pos == root) |
| 2765 | return NULL; |
| 2766 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2767 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2768 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2769 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2770 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2771 | |
| 2772 | /* no sibling left, visit parent */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 2773 | return css_parent(pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2774 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2775 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2776 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2777 | * css_advance_task_iter - advance a task itererator to the next css_set |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2778 | * @it: the iterator to advance |
| 2779 | * |
| 2780 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2781 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2782 | static void css_advance_task_iter(struct css_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2783 | { |
| 2784 | struct list_head *l = it->cset_link; |
| 2785 | struct cgrp_cset_link *link; |
| 2786 | struct css_set *cset; |
| 2787 | |
| 2788 | /* Advance to the next non-empty css_set */ |
| 2789 | do { |
| 2790 | l = l->next; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2791 | if (l == &it->origin_css->cgroup->cset_links) { |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2792 | it->cset_link = NULL; |
| 2793 | return; |
| 2794 | } |
| 2795 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2796 | cset = link->cset; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2797 | } while (list_empty(&cset->tasks) && list_empty(&cset->mg_tasks)); |
| 2798 | |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2799 | it->cset_link = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2800 | |
| 2801 | if (!list_empty(&cset->tasks)) |
| 2802 | it->task = cset->tasks.next; |
| 2803 | else |
| 2804 | it->task = cset->mg_tasks.next; |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2805 | } |
| 2806 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2807 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2808 | * css_task_iter_start - initiate task iteration |
| 2809 | * @css: the css to walk tasks of |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2810 | * @it: the task iterator to use |
| 2811 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2812 | * Initiate iteration through the tasks of @css. The caller can call |
| 2813 | * css_task_iter_next() to walk through the tasks until the function |
| 2814 | * returns NULL. On completion of iteration, css_task_iter_end() must be |
| 2815 | * called. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2816 | * |
| 2817 | * Note that this function acquires a lock which is released when the |
| 2818 | * iteration finishes. The caller can't sleep while iteration is in |
| 2819 | * progress. |
| 2820 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2821 | void css_task_iter_start(struct cgroup_subsys_state *css, |
| 2822 | struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2823 | __acquires(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2824 | { |
Tejun Heo | 56fde9e | 2014-02-13 06:58:38 -0500 | [diff] [blame] | 2825 | /* no one should try to iterate before mounting cgroups */ |
| 2826 | WARN_ON_ONCE(!use_task_css_set_links); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2827 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2828 | down_read(&css_set_rwsem); |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2829 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2830 | it->origin_css = css; |
| 2831 | it->cset_link = &css->cgroup->cset_links; |
Tejun Heo | c59cd3d | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2832 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2833 | css_advance_task_iter(it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2836 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2837 | * css_task_iter_next - return the next task for the iterator |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2838 | * @it: the task iterator being iterated |
| 2839 | * |
| 2840 | * The "next" function for task iteration. @it should have been |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2841 | * initialized via css_task_iter_start(). Returns NULL when the iteration |
| 2842 | * reaches the end. |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2843 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2844 | struct task_struct *css_task_iter_next(struct css_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2845 | { |
| 2846 | struct task_struct *res; |
| 2847 | struct list_head *l = it->task; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2848 | struct cgrp_cset_link *link = list_entry(it->cset_link, |
| 2849 | struct cgrp_cset_link, cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2850 | |
| 2851 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2852 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2853 | return NULL; |
| 2854 | res = list_entry(l, struct task_struct, cg_list); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2855 | |
| 2856 | /* |
| 2857 | * Advance iterator to find next entry. cset->tasks is consumed |
| 2858 | * first and then ->mg_tasks. After ->mg_tasks, we move onto the |
| 2859 | * next cset. |
| 2860 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2861 | l = l->next; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2862 | |
| 2863 | if (l == &link->cset->tasks) |
| 2864 | l = link->cset->mg_tasks.next; |
| 2865 | |
| 2866 | if (l == &link->cset->mg_tasks) |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2867 | css_advance_task_iter(it); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2868 | else |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2869 | it->task = l; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 2870 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2871 | return res; |
| 2872 | } |
| 2873 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2874 | /** |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2875 | * css_task_iter_end - finish task iteration |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2876 | * @it: the task iterator to finish |
| 2877 | * |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2878 | * Finish task iteration started by css_task_iter_start(). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2879 | */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 2880 | void css_task_iter_end(struct css_task_iter *it) |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2881 | __releases(css_set_rwsem) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2882 | { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 2883 | up_read(&css_set_rwsem); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | /** |
| 2887 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 2888 | * @to: cgroup to which the tasks will be moved |
| 2889 | * @from: cgroup in which the tasks currently reside |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2890 | * |
| 2891 | * Locking rules between cgroup_post_fork() and the migration path |
| 2892 | * guarantee that, if a task is forking while being migrated, the new child |
| 2893 | * is guaranteed to be either visible in the source cgroup after the |
| 2894 | * parent's migration is complete or put into the target cgroup. No task |
| 2895 | * can slip out of migration through forking. |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2896 | */ |
| 2897 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 2898 | { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2899 | LIST_HEAD(preloaded_csets); |
| 2900 | struct cgrp_cset_link *link; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2901 | struct css_task_iter it; |
| 2902 | struct task_struct *task; |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2903 | int ret; |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2904 | |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2905 | mutex_lock(&cgroup_mutex); |
| 2906 | |
| 2907 | /* all tasks in @from are being moved, all csets are source */ |
| 2908 | down_read(&css_set_rwsem); |
| 2909 | list_for_each_entry(link, &from->cset_links, cset_link) |
| 2910 | cgroup_migrate_add_src(link->cset, to, &preloaded_csets); |
| 2911 | up_read(&css_set_rwsem); |
| 2912 | |
| 2913 | ret = cgroup_migrate_prepare_dst(to, &preloaded_csets); |
| 2914 | if (ret) |
| 2915 | goto out_err; |
| 2916 | |
| 2917 | /* |
| 2918 | * Migrate tasks one-by-one until @form is empty. This fails iff |
| 2919 | * ->can_attach() fails. |
| 2920 | */ |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2921 | do { |
| 2922 | css_task_iter_start(&from->dummy_css, &it); |
| 2923 | task = css_task_iter_next(&it); |
| 2924 | if (task) |
| 2925 | get_task_struct(task); |
| 2926 | css_task_iter_end(&it); |
| 2927 | |
| 2928 | if (task) { |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2929 | ret = cgroup_migrate(to, task, false); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2930 | put_task_struct(task); |
| 2931 | } |
| 2932 | } while (task && !ret); |
Tejun Heo | 952aaa1 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 2933 | out_err: |
| 2934 | cgroup_migrate_finish(&preloaded_csets); |
| 2935 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | e406d1c | 2014-02-13 06:58:39 -0500 | [diff] [blame] | 2936 | return ret; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2939 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 2940 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2941 | * |
| 2942 | * Reading this file can return large amounts of data if a cgroup has |
| 2943 | * *lots* of attached tasks. So it may need several calls to read(), |
| 2944 | * but we cannot guarantee that the information we produce is correct |
| 2945 | * unless we produce it entirely atomically. |
| 2946 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2947 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2948 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2949 | /* which pidlist file are we talking about? */ |
| 2950 | enum cgroup_filetype { |
| 2951 | CGROUP_FILE_PROCS, |
| 2952 | CGROUP_FILE_TASKS, |
| 2953 | }; |
| 2954 | |
| 2955 | /* |
| 2956 | * A pidlist is a list of pids that virtually represents the contents of one |
| 2957 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 2958 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 2959 | * to the cgroup. |
| 2960 | */ |
| 2961 | struct cgroup_pidlist { |
| 2962 | /* |
| 2963 | * used to find which pidlist is wanted. doesn't change as long as |
| 2964 | * this particular list stays in the list. |
| 2965 | */ |
| 2966 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 2967 | /* array of xids */ |
| 2968 | pid_t *list; |
| 2969 | /* how many elements the above list has */ |
| 2970 | int length; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2971 | /* each of these stored in a list by its cgroup */ |
| 2972 | struct list_head links; |
| 2973 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 2974 | struct cgroup *owner; |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2975 | /* for delayed destruction */ |
| 2976 | struct delayed_work destroy_dwork; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 2977 | }; |
| 2978 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2979 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2980 | * The following two functions "fix" the issue where there are more pids |
| 2981 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 2982 | * TODO: replace with a kernel-wide solution to this problem |
| 2983 | */ |
| 2984 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 2985 | static void *pidlist_allocate(int count) |
| 2986 | { |
| 2987 | if (PIDLIST_TOO_LARGE(count)) |
| 2988 | return vmalloc(count * sizeof(pid_t)); |
| 2989 | else |
| 2990 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 2991 | } |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 2992 | |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 2993 | static void pidlist_free(void *p) |
| 2994 | { |
| 2995 | if (is_vmalloc_addr(p)) |
| 2996 | vfree(p); |
| 2997 | else |
| 2998 | kfree(p); |
| 2999 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3000 | |
| 3001 | /* |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3002 | * Used to destroy all pidlists lingering waiting for destroy timer. None |
| 3003 | * should be left afterwards. |
| 3004 | */ |
| 3005 | static void cgroup_pidlist_destroy_all(struct cgroup *cgrp) |
| 3006 | { |
| 3007 | struct cgroup_pidlist *l, *tmp_l; |
| 3008 | |
| 3009 | mutex_lock(&cgrp->pidlist_mutex); |
| 3010 | list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links) |
| 3011 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0); |
| 3012 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3013 | |
| 3014 | flush_workqueue(cgroup_pidlist_destroy_wq); |
| 3015 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 3016 | } |
| 3017 | |
| 3018 | static void cgroup_pidlist_destroy_work_fn(struct work_struct *work) |
| 3019 | { |
| 3020 | struct delayed_work *dwork = to_delayed_work(work); |
| 3021 | struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist, |
| 3022 | destroy_dwork); |
| 3023 | struct cgroup_pidlist *tofree = NULL; |
| 3024 | |
| 3025 | mutex_lock(&l->owner->pidlist_mutex); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3026 | |
| 3027 | /* |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3028 | * Destroy iff we didn't get queued again. The state won't change |
| 3029 | * as destroy_dwork can only be queued while locked. |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3030 | */ |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3031 | if (!delayed_work_pending(dwork)) { |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3032 | list_del(&l->links); |
| 3033 | pidlist_free(l->list); |
| 3034 | put_pid_ns(l->key.ns); |
| 3035 | tofree = l; |
| 3036 | } |
| 3037 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3038 | mutex_unlock(&l->owner->pidlist_mutex); |
| 3039 | kfree(tofree); |
| 3040 | } |
| 3041 | |
| 3042 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3043 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3044 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3045 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3046 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3047 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3048 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3049 | |
| 3050 | /* |
| 3051 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3052 | * edge cases first; no work needs to be done for either |
| 3053 | */ |
| 3054 | if (length == 0 || length == 1) |
| 3055 | return length; |
| 3056 | /* src and dest walk down the list; dest counts unique elements */ |
| 3057 | for (src = 1; src < length; src++) { |
| 3058 | /* find next unique element */ |
| 3059 | while (list[src] == list[src-1]) { |
| 3060 | src++; |
| 3061 | if (src == length) |
| 3062 | goto after; |
| 3063 | } |
| 3064 | /* dest always points to where the next unique element goes */ |
| 3065 | list[dest] = list[src]; |
| 3066 | dest++; |
| 3067 | } |
| 3068 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3069 | return dest; |
| 3070 | } |
| 3071 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3072 | /* |
| 3073 | * The two pid files - task and cgroup.procs - guaranteed that the result |
| 3074 | * is sorted, which forced this whole pidlist fiasco. As pid order is |
| 3075 | * different per namespace, each namespace needs differently sorted list, |
| 3076 | * making it impossible to use, for example, single rbtree of member tasks |
| 3077 | * sorted by task pointer. As pidlists can be fairly large, allocating one |
| 3078 | * per open file is dangerous, so cgroup had to implement shared pool of |
| 3079 | * pidlists keyed by cgroup and namespace. |
| 3080 | * |
| 3081 | * All this extra complexity was caused by the original implementation |
| 3082 | * committing to an entirely unnecessary property. In the long term, we |
| 3083 | * want to do away with it. Explicitly scramble sort order if |
| 3084 | * sane_behavior so that no such expectation exists in the new interface. |
| 3085 | * |
| 3086 | * Scrambling is done by swapping every two consecutive bits, which is |
| 3087 | * non-identity one-to-one mapping which disturbs sort order sufficiently. |
| 3088 | */ |
| 3089 | static pid_t pid_fry(pid_t pid) |
| 3090 | { |
| 3091 | unsigned a = pid & 0x55555555; |
| 3092 | unsigned b = pid & 0xAAAAAAAA; |
| 3093 | |
| 3094 | return (a << 1) | (b >> 1); |
| 3095 | } |
| 3096 | |
| 3097 | static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid) |
| 3098 | { |
| 3099 | if (cgroup_sane_behavior(cgrp)) |
| 3100 | return pid_fry(pid); |
| 3101 | else |
| 3102 | return pid; |
| 3103 | } |
| 3104 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3105 | static int cmppid(const void *a, const void *b) |
| 3106 | { |
| 3107 | return *(pid_t *)a - *(pid_t *)b; |
| 3108 | } |
| 3109 | |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3110 | static int fried_cmppid(const void *a, const void *b) |
| 3111 | { |
| 3112 | return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b); |
| 3113 | } |
| 3114 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3115 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3116 | enum cgroup_filetype type) |
| 3117 | { |
| 3118 | struct cgroup_pidlist *l; |
| 3119 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3120 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3121 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3122 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3123 | |
| 3124 | list_for_each_entry(l, &cgrp->pidlists, links) |
| 3125 | if (l->key.type == type && l->key.ns == ns) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3126 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3127 | return NULL; |
| 3128 | } |
| 3129 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3130 | /* |
| 3131 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3132 | * returns with the lock on that pidlist already held, and takes care |
| 3133 | * of the use count, or returns NULL with no locks held if we're out of |
| 3134 | * memory. |
| 3135 | */ |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3136 | static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp, |
| 3137 | enum cgroup_filetype type) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3138 | { |
| 3139 | struct cgroup_pidlist *l; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3140 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3141 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3142 | |
| 3143 | l = cgroup_pidlist_find(cgrp, type); |
| 3144 | if (l) |
| 3145 | return l; |
| 3146 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3147 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3148 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3149 | if (!l) |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3150 | return l; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3151 | |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3152 | INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3153 | l->key.type = type; |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3154 | /* don't need task_nsproxy() if we're looking at ourself */ |
| 3155 | l->key.ns = get_pid_ns(task_active_pid_ns(current)); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3156 | l->owner = cgrp; |
| 3157 | list_add(&l->links, &cgrp->pidlists); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3158 | return l; |
| 3159 | } |
| 3160 | |
| 3161 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3162 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3163 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3164 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3165 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3166 | { |
| 3167 | pid_t *array; |
| 3168 | int length; |
| 3169 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3170 | struct css_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3171 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3172 | struct cgroup_pidlist *l; |
| 3173 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3174 | lockdep_assert_held(&cgrp->pidlist_mutex); |
| 3175 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3176 | /* |
| 3177 | * If cgroup gets more users after we read count, we won't have |
| 3178 | * enough space - tough. This race is indistinguishable to the |
| 3179 | * caller from the case that the additional cgroup users didn't |
| 3180 | * show up until sometime later on. |
| 3181 | */ |
| 3182 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3183 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3184 | if (!array) |
| 3185 | return -ENOMEM; |
| 3186 | /* now, populate the array */ |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3187 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3188 | while ((tsk = css_task_iter_next(&it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3189 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3190 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3191 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3192 | if (type == CGROUP_FILE_PROCS) |
| 3193 | pid = task_tgid_vnr(tsk); |
| 3194 | else |
| 3195 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3196 | if (pid > 0) /* make sure to only use valid results */ |
| 3197 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3198 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3199 | css_task_iter_end(&it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3200 | length = n; |
| 3201 | /* now sort & (if procs) strip out duplicates */ |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3202 | if (cgroup_sane_behavior(cgrp)) |
| 3203 | sort(array, length, sizeof(pid_t), fried_cmppid, NULL); |
| 3204 | else |
| 3205 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3206 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3207 | length = pidlist_uniq(array, length); |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3208 | |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3209 | l = cgroup_pidlist_find_create(cgrp, type); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3210 | if (!l) { |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3211 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3212 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3213 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3214 | } |
Tejun Heo | e6b8171 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3215 | |
| 3216 | /* store array, freeing old if necessary */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3217 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3218 | l->list = array; |
| 3219 | l->length = length; |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3220 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3221 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3222 | } |
| 3223 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3224 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3225 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3226 | * @stats: cgroupstats to fill information into |
| 3227 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3228 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3229 | * |
| 3230 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3231 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3232 | */ |
| 3233 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3234 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3235 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3236 | struct cgroup *cgrp; |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3237 | struct css_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3238 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3239 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3240 | /* it should be kernfs_node belonging to cgroupfs and is a directory */ |
| 3241 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 3242 | kernfs_type(kn) != KERNFS_DIR) |
| 3243 | return -EINVAL; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3244 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3245 | mutex_lock(&cgroup_mutex); |
| 3246 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3247 | /* |
| 3248 | * We aren't being called from kernfs and there's no guarantee on |
| 3249 | * @kn->priv's validity. For this and css_tryget_from_dir(), |
| 3250 | * @kn->priv is RCU safe. Let's do the RCU dancing. |
| 3251 | */ |
| 3252 | rcu_read_lock(); |
| 3253 | cgrp = rcu_dereference(kn->priv); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3254 | if (!cgrp || cgroup_is_dead(cgrp)) { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3255 | rcu_read_unlock(); |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3256 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3257 | return -ENOENT; |
| 3258 | } |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3259 | rcu_read_unlock(); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3260 | |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3261 | css_task_iter_start(&cgrp->dummy_css, &it); |
| 3262 | while ((tsk = css_task_iter_next(&it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3263 | switch (tsk->state) { |
| 3264 | case TASK_RUNNING: |
| 3265 | stats->nr_running++; |
| 3266 | break; |
| 3267 | case TASK_INTERRUPTIBLE: |
| 3268 | stats->nr_sleeping++; |
| 3269 | break; |
| 3270 | case TASK_UNINTERRUPTIBLE: |
| 3271 | stats->nr_uninterruptible++; |
| 3272 | break; |
| 3273 | case TASK_STOPPED: |
| 3274 | stats->nr_stopped++; |
| 3275 | break; |
| 3276 | default: |
| 3277 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3278 | stats->nr_io_wait++; |
| 3279 | break; |
| 3280 | } |
| 3281 | } |
Tejun Heo | 72ec702 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3282 | css_task_iter_end(&it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3283 | |
Li Zefan | bad3466 | 2014-02-14 16:54:28 +0800 | [diff] [blame] | 3284 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3285 | return 0; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3288 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3289 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3290 | * seq_file methods for the tasks/procs files. The seq_file position is the |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3291 | * next pid to display; the seq_file iterator is a pointer to the pid |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3292 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3293 | */ |
| 3294 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3295 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3296 | { |
| 3297 | /* |
| 3298 | * Initially we receive a position value that corresponds to |
| 3299 | * one more than the last pid shown (or 0 on the first call or |
| 3300 | * after a seek to the start). Use a binary-search to find the |
| 3301 | * next pid to display, if any |
| 3302 | */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3303 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3304 | struct cgroup *cgrp = seq_css(s)->cgroup; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3305 | struct cgroup_pidlist *l; |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3306 | enum cgroup_filetype type = seq_cft(s)->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3307 | int index = 0, pid = *pos; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3308 | int *iter, ret; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3309 | |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3310 | mutex_lock(&cgrp->pidlist_mutex); |
| 3311 | |
| 3312 | /* |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3313 | * !NULL @of->priv indicates that this isn't the first start() |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3314 | * after open. If the matching pidlist is around, we can use that. |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3315 | * Look for it. Note that @of->priv can't be used directly. It |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3316 | * could already have been destroyed. |
| 3317 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3318 | if (of->priv) |
| 3319 | of->priv = cgroup_pidlist_find(cgrp, type); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3320 | |
| 3321 | /* |
| 3322 | * Either this is the first start() after open or the matching |
| 3323 | * pidlist has been destroyed inbetween. Create a new one. |
| 3324 | */ |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3325 | if (!of->priv) { |
| 3326 | ret = pidlist_array_load(cgrp, type, |
| 3327 | (struct cgroup_pidlist **)&of->priv); |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3328 | if (ret) |
| 3329 | return ERR_PTR(ret); |
| 3330 | } |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3331 | l = of->priv; |
Tejun Heo | 4bac00d | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3332 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3333 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3334 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3335 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3336 | while (index < end) { |
| 3337 | int mid = (index + end) / 2; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3338 | if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3339 | index = mid; |
| 3340 | break; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3341 | } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3342 | index = mid + 1; |
| 3343 | else |
| 3344 | end = mid; |
| 3345 | } |
| 3346 | } |
| 3347 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3348 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3349 | return NULL; |
| 3350 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3351 | iter = l->list + index; |
Tejun Heo | afb2bc1 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3352 | *pos = cgroup_pid_fry(cgrp, *iter); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3353 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3356 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3357 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3358 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3359 | struct cgroup_pidlist *l = of->priv; |
Tejun Heo | 6223685 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 3360 | |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3361 | if (l) |
| 3362 | mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, |
Tejun Heo | 0450236 | 2013-11-29 10:42:59 -0500 | [diff] [blame] | 3363 | CGROUP_PIDLIST_DESTROY_DELAY); |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3364 | mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3367 | static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3368 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3369 | struct kernfs_open_file *of = s->private; |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3370 | struct cgroup_pidlist *l = of->priv; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3371 | pid_t *p = v; |
| 3372 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3373 | /* |
| 3374 | * Advance to the next pid in the array. If this goes off the |
| 3375 | * end, we're done |
| 3376 | */ |
| 3377 | p++; |
| 3378 | if (p >= end) { |
| 3379 | return NULL; |
| 3380 | } else { |
Tejun Heo | 7da1127 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3381 | *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3382 | return p; |
| 3383 | } |
| 3384 | } |
| 3385 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3386 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3387 | { |
| 3388 | return seq_printf(s, "%d\n", *(int *)v); |
| 3389 | } |
| 3390 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3391 | /* |
| 3392 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3393 | * independent of whether it's tasks or procs |
| 3394 | */ |
| 3395 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3396 | .start = cgroup_pidlist_start, |
| 3397 | .stop = cgroup_pidlist_stop, |
| 3398 | .next = cgroup_pidlist_next, |
| 3399 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3400 | }; |
| 3401 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3402 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3403 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3404 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3405 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3406 | } |
| 3407 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3408 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3409 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3410 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3411 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3412 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3413 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3414 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3415 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3416 | return 0; |
| 3417 | } |
| 3418 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3419 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 3420 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3421 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3422 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3425 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 3426 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3427 | { |
| 3428 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3429 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3430 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3431 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3432 | return 0; |
| 3433 | } |
| 3434 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3435 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3436 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3437 | .name = "cgroup.procs", |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3438 | .seq_start = cgroup_pidlist_start, |
| 3439 | .seq_next = cgroup_pidlist_next, |
| 3440 | .seq_stop = cgroup_pidlist_stop, |
| 3441 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3442 | .private = CGROUP_FILE_PROCS, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3443 | .write_u64 = cgroup_procs_write, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3444 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3445 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3446 | { |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3447 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3448 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3449 | .read_u64 = cgroup_clone_children_read, |
| 3450 | .write_u64 = cgroup_clone_children_write, |
| 3451 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3452 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3453 | .name = "cgroup.sane_behavior", |
| 3454 | .flags = CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3455 | .seq_show = cgroup_sane_behavior_show, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3456 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3457 | |
| 3458 | /* |
| 3459 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 3460 | * don't exist if sane_behavior. If you're depending on these, be |
| 3461 | * prepared to be burned. |
| 3462 | */ |
| 3463 | { |
| 3464 | .name = "tasks", |
| 3465 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
Tejun Heo | 6612f05 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3466 | .seq_start = cgroup_pidlist_start, |
| 3467 | .seq_next = cgroup_pidlist_next, |
| 3468 | .seq_stop = cgroup_pidlist_stop, |
| 3469 | .seq_show = cgroup_pidlist_show, |
Tejun Heo | 5d22444 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3470 | .private = CGROUP_FILE_TASKS, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3471 | .write_u64 = cgroup_tasks_write, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 3472 | .mode = S_IRUGO | S_IWUSR, |
| 3473 | }, |
| 3474 | { |
| 3475 | .name = "notify_on_release", |
| 3476 | .flags = CFTYPE_INSANE, |
| 3477 | .read_u64 = cgroup_read_notify_on_release, |
| 3478 | .write_u64 = cgroup_write_notify_on_release, |
| 3479 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 3480 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3481 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 3482 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 3483 | .seq_show = cgroup_release_agent_show, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3484 | .write_string = cgroup_release_agent_write, |
Tejun Heo | 5f46990 | 2014-02-11 11:52:48 -0500 | [diff] [blame] | 3485 | .max_write_len = PATH_MAX - 1, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3486 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3487 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3488 | }; |
| 3489 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3490 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3491 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3492 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3493 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3494 | * |
| 3495 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3496 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3497 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3498 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3499 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3500 | int i, ret = 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3501 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3502 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3503 | for_each_subsys(ss, i) { |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3504 | struct cftype *cfts; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 3505 | |
| 3506 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3507 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3508 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 3509 | list_for_each_entry(cfts, &ss->cfts, node) { |
| 3510 | ret = cgroup_addrm_files(cgrp, cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3511 | if (ret < 0) |
| 3512 | goto err; |
| 3513 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3514 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3515 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3516 | err: |
| 3517 | cgroup_clear_dir(cgrp, subsys_mask); |
| 3518 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3521 | /* |
| 3522 | * css destruction is four-stage process. |
| 3523 | * |
| 3524 | * 1. Destruction starts. Killing of the percpu_ref is initiated. |
| 3525 | * Implemented in kill_css(). |
| 3526 | * |
| 3527 | * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs |
| 3528 | * and thus css_tryget() is guaranteed to fail, the css can be offlined |
| 3529 | * by invoking offline_css(). After offlining, the base ref is put. |
| 3530 | * Implemented in css_killed_work_fn(). |
| 3531 | * |
| 3532 | * 3. When the percpu_ref reaches zero, the only possible remaining |
| 3533 | * accessors are inside RCU read sections. css_release() schedules the |
| 3534 | * RCU callback. |
| 3535 | * |
| 3536 | * 4. After the grace period, the css can be freed. Implemented in |
| 3537 | * css_free_work_fn(). |
| 3538 | * |
| 3539 | * It is actually hairier because both step 2 and 4 require process context |
| 3540 | * and thus involve punting to css->destroy_work adding two additional |
| 3541 | * steps to the already complex sequence. |
| 3542 | */ |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3543 | static void css_free_work_fn(struct work_struct *work) |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3544 | { |
| 3545 | struct cgroup_subsys_state *css = |
Tejun Heo | 35ef10d | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3546 | container_of(work, struct cgroup_subsys_state, destroy_work); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3547 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3548 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3549 | if (css->parent) |
| 3550 | css_put(css->parent); |
| 3551 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3552 | css->ss->css_free(css); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3553 | cgroup_put(cgrp); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | static void css_free_rcu_fn(struct rcu_head *rcu_head) |
| 3557 | { |
| 3558 | struct cgroup_subsys_state *css = |
| 3559 | container_of(rcu_head, struct cgroup_subsys_state, rcu_head); |
| 3560 | |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3561 | INIT_WORK(&css->destroy_work, css_free_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3562 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3565 | static void css_release(struct percpu_ref *ref) |
| 3566 | { |
| 3567 | struct cgroup_subsys_state *css = |
| 3568 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3569 | |
Monam Agarwal | 01a97140 | 2014-03-24 00:17:18 +0530 | [diff] [blame] | 3570 | RCU_INIT_POINTER(css->cgroup->subsys[css->ss->id], NULL); |
Tejun Heo | 0c21ead | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3571 | call_rcu(&css->rcu_head, css_free_rcu_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3572 | } |
| 3573 | |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3574 | static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss, |
| 3575 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3576 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3577 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 3578 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3579 | css->flags = 0; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3580 | |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3581 | if (cgrp->parent) |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3582 | css->parent = cgroup_css(cgrp->parent, ss); |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3583 | else |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3584 | css->flags |= CSS_ROOT; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3585 | |
Tejun Heo | ca8bdca | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 3586 | BUG_ON(cgroup_css(cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3589 | /* invoke ->css_online() on a new CSS and mark it online if successful */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3590 | static int online_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3591 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3592 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3593 | int ret = 0; |
| 3594 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3595 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3596 | lockdep_assert_held(&cgroup_mutex); |
| 3597 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3598 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3599 | ret = ss->css_online(css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3600 | if (!ret) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3601 | css->flags |= CSS_ONLINE; |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3602 | css->cgroup->nr_css++; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3603 | rcu_assign_pointer(css->cgroup->subsys[ss->id], css); |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3604 | } |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3605 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 3608 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3609 | static void offline_css(struct cgroup_subsys_state *css) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3610 | { |
Tejun Heo | 623f926 | 2013-08-13 11:01:55 -0400 | [diff] [blame] | 3611 | struct cgroup_subsys *ss = css->ss; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3612 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3613 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3614 | lockdep_assert_held(&cgroup_mutex); |
| 3615 | |
| 3616 | if (!(css->flags & CSS_ONLINE)) |
| 3617 | return; |
| 3618 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 3619 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3620 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3621 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3622 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3623 | css->cgroup->nr_css--; |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3624 | RCU_INIT_POINTER(css->cgroup->subsys[ss->id], css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3625 | } |
| 3626 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3627 | /** |
| 3628 | * create_css - create a cgroup_subsys_state |
| 3629 | * @cgrp: the cgroup new css will be associated with |
| 3630 | * @ss: the subsys of new css |
| 3631 | * |
| 3632 | * Create a new css associated with @cgrp - @ss pair. On success, the new |
| 3633 | * css is online and installed in @cgrp with all interface files created. |
| 3634 | * Returns 0 on success, -errno on failure. |
| 3635 | */ |
| 3636 | static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss) |
| 3637 | { |
| 3638 | struct cgroup *parent = cgrp->parent; |
| 3639 | struct cgroup_subsys_state *css; |
| 3640 | int err; |
| 3641 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3642 | lockdep_assert_held(&cgroup_mutex); |
| 3643 | |
| 3644 | css = ss->css_alloc(cgroup_css(parent, ss)); |
| 3645 | if (IS_ERR(css)) |
| 3646 | return PTR_ERR(css); |
| 3647 | |
| 3648 | err = percpu_ref_init(&css->refcnt, css_release); |
| 3649 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3650 | goto err_free_css; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3651 | |
| 3652 | init_css(css, ss, cgrp); |
| 3653 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3654 | err = cgroup_populate_dir(cgrp, 1 << ss->id); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3655 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3656 | goto err_free_percpu_ref; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3657 | |
| 3658 | err = online_css(css); |
| 3659 | if (err) |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3660 | goto err_clear_dir; |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3661 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3662 | cgroup_get(cgrp); |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3663 | css_get(css->parent); |
| 3664 | |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3665 | cgrp->subsys_mask |= 1 << ss->id; |
| 3666 | |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3667 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 3668 | parent->parent) { |
| 3669 | pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n", |
| 3670 | current->comm, current->pid, ss->name); |
| 3671 | if (!strcmp(ss->name, "memory")) |
| 3672 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 3673 | ss->warned_broken_hierarchy = true; |
| 3674 | } |
| 3675 | |
| 3676 | return 0; |
| 3677 | |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3678 | err_clear_dir: |
Linus Torvalds | 32d01dc | 2014-04-03 13:05:42 -0700 | [diff] [blame] | 3679 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3680 | err_free_percpu_ref: |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3681 | percpu_ref_cancel_init(&css->refcnt); |
Li Zefan | 3eb59ec | 2014-03-18 17:02:36 +0800 | [diff] [blame] | 3682 | err_free_css: |
Tejun Heo | c81c925a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3683 | ss->css_free(css); |
| 3684 | return err; |
| 3685 | } |
| 3686 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3687 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3688 | * cgroup_create - create a cgroup |
| 3689 | * @parent: cgroup that will be parent of the new cgroup |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3690 | * @name: name of the new cgroup |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3691 | * @mode: mode to set on new cgroup |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3692 | */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3693 | static long cgroup_create(struct cgroup *parent, const char *name, |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3694 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3695 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3696 | struct cgroup *cgrp; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3697 | struct cgroup_root *root = parent->root; |
Tejun Heo | b58c899 | 2014-02-08 10:26:33 -0500 | [diff] [blame] | 3698 | int ssid, err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3699 | struct cgroup_subsys *ss; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3700 | struct kernfs_node *kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3701 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 3702 | /* |
| 3703 | * XXX: The default hierarchy isn't fully implemented yet. Block |
| 3704 | * !root cgroup creation on it for now. |
| 3705 | */ |
| 3706 | if (root == &cgrp_dfl_root) |
| 3707 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3708 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3709 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3710 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 3711 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3712 | return -ENOMEM; |
| 3713 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3714 | mutex_lock(&cgroup_tree_mutex); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 3715 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3716 | /* |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3717 | * Only live parents can have children. Note that the liveliness |
| 3718 | * check isn't strictly necessary because cgroup_mkdir() and |
| 3719 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 3720 | * anyway so that locking is contained inside cgroup proper and we |
| 3721 | * don't get nasty surprises if we ever grow another caller. |
| 3722 | */ |
| 3723 | if (!cgroup_lock_live_group(parent)) { |
| 3724 | err = -ENODEV; |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3725 | goto err_unlock_tree; |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 3730 | * a half-baked cgroup. |
| 3731 | */ |
| 3732 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
| 3733 | if (cgrp->id < 0) { |
| 3734 | err = -ENOMEM; |
| 3735 | goto err_unlock; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3736 | } |
| 3737 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3738 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3739 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3740 | cgrp->parent = parent; |
Tejun Heo | 0ae78e0 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 3741 | cgrp->dummy_css.parent = &parent->dummy_css; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3742 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3743 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 3744 | if (notify_on_release(parent)) |
| 3745 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3746 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3747 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 3748 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3749 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3750 | /* create the directory */ |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3751 | kn = kernfs_create_dir(parent->kn, name, mode, cgrp); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3752 | if (IS_ERR(kn)) { |
| 3753 | err = PTR_ERR(kn); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3754 | goto err_free_id; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3755 | } |
| 3756 | cgrp->kn = kn; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3757 | |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3758 | /* |
| 3759 | * This extra ref will be put in cgroup_free_fn() and guarantees |
| 3760 | * that @cgrp->kn is always accessible. |
| 3761 | */ |
| 3762 | kernfs_get(kn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3763 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 3764 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3765 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3766 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 3767 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 3768 | atomic_inc(&root->nr_cgrps); |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3769 | cgroup_get(parent); |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 3770 | |
Tejun Heo | 0d80255 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3771 | /* |
| 3772 | * @cgrp is now fully operational. If something fails after this |
| 3773 | * point, it'll be released via the normal destruction path. |
| 3774 | */ |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3775 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 3776 | |
Tejun Heo | 49957f8 | 2014-04-07 16:44:47 -0400 | [diff] [blame] | 3777 | err = cgroup_kn_set_ugid(kn); |
| 3778 | if (err) |
| 3779 | goto err_destroy; |
| 3780 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 3781 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 3782 | if (err) |
| 3783 | goto err_destroy; |
| 3784 | |
Tejun Heo | 9d403e9 | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3785 | /* let's create and online css's */ |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 3786 | for_each_subsys(ss, ssid) { |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3787 | if (root->cgrp.subsys_mask & (1 << ssid)) { |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 3788 | err = create_css(cgrp, ss); |
| 3789 | if (err) |
| 3790 | goto err_destroy; |
| 3791 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3792 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3793 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3794 | kernfs_activate(kn); |
| 3795 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3796 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3797 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3798 | |
| 3799 | return 0; |
| 3800 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 3801 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 3802 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 0ab02ca | 2014-02-11 16:05:46 +0800 | [diff] [blame] | 3803 | err_unlock: |
| 3804 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3805 | err_unlock_tree: |
| 3806 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3807 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3808 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3809 | |
| 3810 | err_destroy: |
| 3811 | cgroup_destroy_locked(cgrp); |
| 3812 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3813 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3814 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3817 | static int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, |
| 3818 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3819 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3820 | struct cgroup *parent = parent_kn->priv; |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 3821 | int ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3822 | |
Tejun Heo | e1b2dc1 | 2014-03-20 11:10:15 -0400 | [diff] [blame] | 3823 | /* |
| 3824 | * cgroup_create() grabs cgroup_tree_mutex which nests outside |
| 3825 | * kernfs active_ref and cgroup_create() already synchronizes |
| 3826 | * properly against removal through cgroup_lock_live_group(). |
| 3827 | * Break it before calling cgroup_create(). |
| 3828 | */ |
| 3829 | cgroup_get(parent); |
| 3830 | kernfs_break_active_protection(parent_kn); |
| 3831 | |
| 3832 | ret = cgroup_create(parent, name, mode); |
| 3833 | |
| 3834 | kernfs_unbreak_active_protection(parent_kn); |
| 3835 | cgroup_put(parent); |
| 3836 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3837 | } |
| 3838 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3839 | /* |
| 3840 | * This is called when the refcnt of a css is confirmed to be killed. |
| 3841 | * css_tryget() is now guaranteed to fail. |
| 3842 | */ |
| 3843 | static void css_killed_work_fn(struct work_struct *work) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3844 | { |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3845 | struct cgroup_subsys_state *css = |
| 3846 | container_of(work, struct cgroup_subsys_state, destroy_work); |
| 3847 | struct cgroup *cgrp = css->cgroup; |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3848 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3849 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3850 | mutex_lock(&cgroup_mutex); |
| 3851 | |
| 3852 | /* |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3853 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 3854 | * initate destruction. |
| 3855 | */ |
| 3856 | offline_css(css); |
| 3857 | |
| 3858 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3859 | * If @cgrp is marked dead, it's waiting for refs of all css's to |
| 3860 | * be disabled before proceeding to the second phase of cgroup |
| 3861 | * destruction. If we are the last one, kick it off. |
| 3862 | */ |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3863 | if (!cgrp->nr_css && cgroup_is_dead(cgrp)) |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3864 | cgroup_destroy_css_killed(cgrp); |
| 3865 | |
| 3866 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3867 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3868 | |
| 3869 | /* |
| 3870 | * Put the css refs from kill_css(). Each css holds an extra |
| 3871 | * reference to the cgroup's dentry and cgroup removal proceeds |
| 3872 | * regardless of css refs. On the last put of each css, whenever |
| 3873 | * that may be, the extra dentry ref is put so that dentry |
| 3874 | * destruction happens only after all css's are released. |
| 3875 | */ |
| 3876 | css_put(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3879 | /* css kill confirmation processing requires process context, bounce */ |
| 3880 | static void css_killed_ref_fn(struct percpu_ref *ref) |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3881 | { |
| 3882 | struct cgroup_subsys_state *css = |
| 3883 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 3884 | |
Tejun Heo | 223dbc3 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 3885 | INIT_WORK(&css->destroy_work, css_killed_work_fn); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 3886 | queue_work(cgroup_destroy_wq, &css->destroy_work); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3887 | } |
| 3888 | |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3889 | static void __kill_css(struct cgroup_subsys_state *css) |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3890 | { |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3891 | lockdep_assert_held(&cgroup_tree_mutex); |
| 3892 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3893 | /* |
| 3894 | * This must happen before css is disassociated with its cgroup. |
| 3895 | * See seq_css() for details. |
| 3896 | */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 3897 | cgroup_clear_dir(css->cgroup, 1 << css->ss->id); |
Tejun Heo | 3c14f8b | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3898 | |
Tejun Heo | edae0c3 | 2013-08-13 20:22:51 -0400 | [diff] [blame] | 3899 | /* |
| 3900 | * Killing would put the base ref, but we need to keep it alive |
| 3901 | * until after ->css_offline(). |
| 3902 | */ |
| 3903 | css_get(css); |
| 3904 | |
| 3905 | /* |
| 3906 | * cgroup core guarantees that, by the time ->css_offline() is |
| 3907 | * invoked, no new css reference will be given out via |
| 3908 | * css_tryget(). We can't simply call percpu_ref_kill() and |
| 3909 | * proceed to offlining css's because percpu_ref_kill() doesn't |
| 3910 | * guarantee that the ref is seen as killed on all CPUs on return. |
| 3911 | * |
| 3912 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 3913 | * css is confirmed to be seen as killed on all CPUs. |
| 3914 | */ |
| 3915 | percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | /** |
Tejun Heo | 9441962 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 3919 | * kill_css - destroy a css |
| 3920 | * @css: css to destroy |
| 3921 | * |
| 3922 | * This function initiates destruction of @css by removing cgroup interface |
| 3923 | * files and putting its base reference. ->css_offline() will be invoked |
| 3924 | * asynchronously once css_tryget() is guaranteed to fail and when the |
| 3925 | * reference count reaches zero, @css will be released. |
| 3926 | */ |
| 3927 | static void kill_css(struct cgroup_subsys_state *css) |
| 3928 | { |
| 3929 | struct cgroup *cgrp = css->cgroup; |
| 3930 | |
| 3931 | lockdep_assert_held(&cgroup_tree_mutex); |
| 3932 | |
| 3933 | /* if already killed, noop */ |
| 3934 | if (cgrp->subsys_mask & (1 << css->ss->id)) { |
| 3935 | cgrp->subsys_mask &= ~(1 << css->ss->id); |
| 3936 | __kill_css(css); |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | /** |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 3941 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 3942 | * @cgrp: cgroup to be destroyed |
| 3943 | * |
| 3944 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 3945 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 3946 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 3947 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 3948 | * the following two steps. |
| 3949 | * |
| 3950 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 3951 | * userland visible parts and start killing the percpu refcnts of |
| 3952 | * css's. Set up so that the next stage will be kicked off once all |
| 3953 | * the percpu refcnts are confirmed to be killed. |
| 3954 | * |
| 3955 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 3956 | * rest of destruction. Once all cgroup references are gone, the |
| 3957 | * cgroup is RCU-freed. |
| 3958 | * |
| 3959 | * This function implements s1. After this step, @cgrp is gone as far as |
| 3960 | * the userland is concerned and a new cgroup with the same name may be |
| 3961 | * created. As cgroup doesn't care about the names internally, this |
| 3962 | * doesn't cause any problem. |
| 3963 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3964 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 3965 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3966 | { |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3967 | struct cgroup *child; |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 3968 | struct cgroup_subsys_state *css; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3969 | bool empty; |
Tejun Heo | 1c6727a | 2013-12-06 15:11:56 -0500 | [diff] [blame] | 3970 | int ssid; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3971 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 3972 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 3973 | lockdep_assert_held(&cgroup_mutex); |
| 3974 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3975 | /* |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3976 | * css_set_rwsem synchronizes access to ->cset_links and prevents |
Tejun Heo | 89c5509 | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3977 | * @cgrp from being removed while put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3978 | */ |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3979 | down_read(&css_set_rwsem); |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3980 | empty = list_empty(&cgrp->cset_links); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 3981 | up_read(&css_set_rwsem); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 3982 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3983 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 3984 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 3985 | /* |
Hugh Dickins | bb78a92 | 2013-08-28 16:31:23 -0700 | [diff] [blame] | 3986 | * Make sure there's no live children. We can't test ->children |
| 3987 | * emptiness as dead children linger on it while being destroyed; |
| 3988 | * otherwise, "rmdir parent/child parent" may fail with -EBUSY. |
| 3989 | */ |
| 3990 | empty = true; |
| 3991 | rcu_read_lock(); |
| 3992 | list_for_each_entry_rcu(child, &cgrp->children, sibling) { |
| 3993 | empty = cgroup_is_dead(child); |
| 3994 | if (!empty) |
| 3995 | break; |
| 3996 | } |
| 3997 | rcu_read_unlock(); |
| 3998 | if (!empty) |
| 3999 | return -EBUSY; |
| 4000 | |
| 4001 | /* |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4002 | * Mark @cgrp dead. This prevents further task migration and child |
| 4003 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4004 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4005 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4006 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4007 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4008 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4009 | |
Tejun Heo | 5d77381 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4010 | /* |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4011 | * Initiate massacre of all css's. cgroup_destroy_css_killed() |
| 4012 | * will be invoked to perform the rest of destruction once the |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4013 | * percpu refs of all css's are confirmed to be killed. This |
| 4014 | * involves removing the subsystem's files, drop cgroup_mutex. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4015 | */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4016 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4017 | for_each_css(css, ssid, cgrp) |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4018 | kill_css(css); |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4019 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4020 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4021 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4022 | raw_spin_lock(&release_list_lock); |
| 4023 | if (!list_empty(&cgrp->release_list)) |
| 4024 | list_del_init(&cgrp->release_list); |
| 4025 | raw_spin_unlock(&release_list_lock); |
| 4026 | |
| 4027 | /* |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4028 | * If @cgrp has css's attached, the second stage of cgroup |
| 4029 | * destruction is kicked off from css_killed_work_fn() after the |
| 4030 | * refs of all attached css's are killed. If @cgrp doesn't have |
| 4031 | * any css, we kick it off here. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4032 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4033 | if (!cgrp->nr_css) |
| 4034 | cgroup_destroy_css_killed(cgrp); |
| 4035 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4036 | /* remove @cgrp directory along with the base files */ |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4037 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4038 | |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4039 | /* |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4040 | * There are two control paths which try to determine cgroup from |
| 4041 | * dentry without going through kernfs - cgroupstats_build() and |
| 4042 | * css_tryget_from_dir(). Those are supported by RCU protecting |
| 4043 | * clearing of cgrp->kn->priv backpointer, which should happen |
| 4044 | * after all files under it have been removed. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4045 | */ |
Tejun Heo | 6f30558 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4046 | kernfs_remove(cgrp->kn); /* @cgrp has an extra ref on its kn */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4047 | RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, NULL); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4048 | |
Tejun Heo | 4ac0601 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4049 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4050 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4051 | return 0; |
| 4052 | }; |
| 4053 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4054 | /** |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4055 | * cgroup_destroy_css_killed - the second step of cgroup destruction |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4056 | * @work: cgroup->destroy_free_work |
| 4057 | * |
| 4058 | * This function is invoked from a work item for a cgroup which is being |
Tejun Heo | 09a503ea | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4059 | * destroyed after all css's are offlined and performs the rest of |
| 4060 | * destruction. This is the second step of destruction described in the |
| 4061 | * comment above cgroup_destroy_locked(). |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4062 | */ |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4063 | static void cgroup_destroy_css_killed(struct cgroup *cgrp) |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4064 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4065 | struct cgroup *parent = cgrp->parent; |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4066 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4067 | lockdep_assert_held(&cgroup_tree_mutex); |
Tejun Heo | f20104d | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4068 | lockdep_assert_held(&cgroup_mutex); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4069 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4070 | /* delete this cgroup from parent->children */ |
| 4071 | list_del_rcu(&cgrp->sibling); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4072 | |
Tejun Heo | 59f5296 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4073 | cgroup_put(cgrp); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4074 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4075 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4076 | check_for_release(parent); |
| 4077 | } |
| 4078 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4079 | static int cgroup_rmdir(struct kernfs_node *kn) |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4080 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4081 | struct cgroup *cgrp = kn->priv; |
| 4082 | int ret = 0; |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4083 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4084 | /* |
| 4085 | * This is self-destruction but @kn can't be removed while this |
| 4086 | * callback is in progress. Let's break active protection. Once |
| 4087 | * the protection is broken, @cgrp can be destroyed at any point. |
| 4088 | * Pin it so that it stays accessible. |
| 4089 | */ |
| 4090 | cgroup_get(cgrp); |
| 4091 | kernfs_break_active_protection(kn); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4092 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4093 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4094 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4095 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4096 | /* |
| 4097 | * @cgrp might already have been destroyed while we're trying to |
| 4098 | * grab the mutexes. |
| 4099 | */ |
| 4100 | if (!cgroup_is_dead(cgrp)) |
| 4101 | ret = cgroup_destroy_locked(cgrp); |
| 4102 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4103 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4104 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4105 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4106 | kernfs_unbreak_active_protection(kn); |
| 4107 | cgroup_put(cgrp); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4108 | return ret; |
| 4109 | } |
| 4110 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4111 | static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { |
| 4112 | .remount_fs = cgroup_remount, |
| 4113 | .show_options = cgroup_show_options, |
| 4114 | .mkdir = cgroup_mkdir, |
| 4115 | .rmdir = cgroup_rmdir, |
| 4116 | .rename = cgroup_rename, |
| 4117 | }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4118 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4119 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4120 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4121 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4122 | |
| 4123 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4124 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4125 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4126 | mutex_lock(&cgroup_mutex); |
| 4127 | |
Tejun Heo | 0adb070 | 2014-02-12 09:29:48 -0500 | [diff] [blame] | 4128 | INIT_LIST_HEAD(&ss->cfts); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4129 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4130 | /* Create the root cgroup state for this subsystem */ |
| 4131 | ss->root = &cgrp_dfl_root; |
| 4132 | css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4133 | /* We don't handle early failures gracefully */ |
| 4134 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4135 | init_css(css, ss, &cgrp_dfl_root.cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4136 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4137 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4138 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4139 | * newly registered, all tasks and hence the |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4140 | * init_css_set is in the subsystem's root cgroup. */ |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4141 | init_css_set.subsys[ss->id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4142 | |
| 4143 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4144 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4145 | /* At system boot, before all subsystems have been |
| 4146 | * registered, no tasks have been forked, so we don't |
| 4147 | * need to invoke fork callbacks here. */ |
| 4148 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4149 | |
Tejun Heo | ae7f164 | 2013-08-13 20:22:50 -0400 | [diff] [blame] | 4150 | BUG_ON(online_css(css)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4151 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4152 | cgrp_dfl_root.cgrp.subsys_mask |= 1 << ss->id; |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4153 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4154 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4155 | mutex_unlock(&cgroup_tree_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4159 | * cgroup_init_early - cgroup initialization at system boot |
| 4160 | * |
| 4161 | * Initialize cgroups at system boot, and initialize any |
| 4162 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4163 | */ |
| 4164 | int __init cgroup_init_early(void) |
| 4165 | { |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4166 | static struct cgroup_sb_opts __initdata opts = |
| 4167 | { .flags = CGRP_ROOT_SANE_BEHAVIOR }; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4168 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4169 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4170 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4171 | init_cgroup_root(&cgrp_dfl_root, &opts); |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4172 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4173 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4174 | for_each_subsys(ss, i) { |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4175 | WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4176 | "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n", |
| 4177 | i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4178 | ss->id, ss->name); |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4179 | WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, |
| 4180 | "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4181 | |
Tejun Heo | aec2502 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4182 | ss->id = i; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4183 | ss->name = cgroup_subsys_name[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4184 | |
| 4185 | if (ss->early_init) |
| 4186 | cgroup_init_subsys(ss); |
| 4187 | } |
| 4188 | return 0; |
| 4189 | } |
| 4190 | |
| 4191 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4192 | * cgroup_init - cgroup initialization |
| 4193 | * |
| 4194 | * Register cgroup filesystem and /proc file, and initialize |
| 4195 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4196 | */ |
| 4197 | int __init cgroup_init(void) |
| 4198 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4199 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4200 | unsigned long key; |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4201 | int ssid, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4202 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4203 | BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4204 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4205 | mutex_lock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4206 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4207 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4208 | /* Add init_css_set to the hash table */ |
| 4209 | key = css_set_hash(init_css_set.subsys); |
| 4210 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4211 | |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4212 | BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4213 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4214 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4215 | mutex_unlock(&cgroup_tree_mutex); |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4216 | |
Tejun Heo | 172a2c06 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4217 | for_each_subsys(ss, ssid) { |
| 4218 | if (!ss->early_init) |
| 4219 | cgroup_init_subsys(ss); |
| 4220 | |
| 4221 | /* |
| 4222 | * cftype registration needs kmalloc and can't be done |
| 4223 | * during early_init. Register base cftypes separately. |
| 4224 | */ |
| 4225 | if (ss->base_cftypes) |
| 4226 | WARN_ON(cgroup_add_cftypes(ss, ss->base_cftypes)); |
| 4227 | } |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4228 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4229 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4230 | if (!cgroup_kobj) |
| 4231 | return -ENOMEM; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4232 | |
| 4233 | err = register_filesystem(&cgroup_fs_type); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4234 | if (err < 0) { |
| 4235 | kobject_put(cgroup_kobj); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4236 | return err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4240 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4241 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4242 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4243 | static int __init cgroup_wq_init(void) |
| 4244 | { |
| 4245 | /* |
| 4246 | * There isn't much point in executing destruction path in |
| 4247 | * parallel. Good chunk is serialized with cgroup_mutex anyway. |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4248 | * Use 1 for @max_active. |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4249 | * |
| 4250 | * We would prefer to do this in cgroup_init() above, but that |
| 4251 | * is called before init_workqueues(): so leave this until after. |
| 4252 | */ |
Tejun Heo | 1a11533 | 2014-02-12 19:06:19 -0500 | [diff] [blame] | 4253 | cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4254 | BUG_ON(!cgroup_destroy_wq); |
Tejun Heo | b1a2136 | 2013-11-29 10:42:58 -0500 | [diff] [blame] | 4255 | |
| 4256 | /* |
| 4257 | * Used to destroy pidlists and separate to serve as flush domain. |
| 4258 | * Cap @max_active to 1 too. |
| 4259 | */ |
| 4260 | cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy", |
| 4261 | 0, 1); |
| 4262 | BUG_ON(!cgroup_pidlist_destroy_wq); |
| 4263 | |
Tejun Heo | e5fca24 | 2013-11-22 17:14:39 -0500 | [diff] [blame] | 4264 | return 0; |
| 4265 | } |
| 4266 | core_initcall(cgroup_wq_init); |
| 4267 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4268 | /* |
| 4269 | * proc_cgroup_show() |
| 4270 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4271 | * - Used for /proc/<pid>/cgroup. |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4272 | */ |
| 4273 | |
| 4274 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4275 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4276 | { |
| 4277 | struct pid *pid; |
| 4278 | struct task_struct *tsk; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4279 | char *buf, *path; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4280 | int retval; |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4281 | struct cgroup_root *root; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4282 | |
| 4283 | retval = -ENOMEM; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4284 | buf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4285 | if (!buf) |
| 4286 | goto out; |
| 4287 | |
| 4288 | retval = -ESRCH; |
| 4289 | pid = m->private; |
| 4290 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4291 | if (!tsk) |
| 4292 | goto out_free; |
| 4293 | |
| 4294 | retval = 0; |
| 4295 | |
| 4296 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4297 | down_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4298 | |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4299 | for_each_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4300 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4301 | struct cgroup *cgrp; |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4302 | int ssid, count = 0; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4303 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4304 | if (root == &cgrp_dfl_root && !cgrp_dfl_root_visible) |
Tejun Heo | 985ed67 | 2014-03-19 10:23:53 -0400 | [diff] [blame] | 4305 | continue; |
| 4306 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4307 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4308 | for_each_subsys(ss, ssid) |
Tejun Heo | 3dd06ff | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 4309 | if (root->cgrp.subsys_mask & (1 << ssid)) |
Tejun Heo | b85d204 | 2013-12-06 15:11:57 -0500 | [diff] [blame] | 4310 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4311 | if (strlen(root->name)) |
| 4312 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4313 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4314 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4315 | cgrp = task_cgroup_from_root(tsk, root); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4316 | path = cgroup_path(cgrp, buf, PATH_MAX); |
| 4317 | if (!path) { |
| 4318 | retval = -ENAMETOOLONG; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4319 | goto out_unlock; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4320 | } |
| 4321 | seq_puts(m, path); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4322 | seq_putc(m, '\n'); |
| 4323 | } |
| 4324 | |
| 4325 | out_unlock: |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4326 | up_read(&css_set_rwsem); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4327 | mutex_unlock(&cgroup_mutex); |
| 4328 | put_task_struct(tsk); |
| 4329 | out_free: |
| 4330 | kfree(buf); |
| 4331 | out: |
| 4332 | return retval; |
| 4333 | } |
| 4334 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4335 | /* Display information about each subsystem and each hierarchy */ |
| 4336 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4337 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4338 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4339 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4340 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4341 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4342 | /* |
| 4343 | * ideally we don't want subsystems moving around while we do this. |
| 4344 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4345 | * subsys/hierarchy state. |
| 4346 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4347 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4348 | |
| 4349 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4350 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4351 | ss->name, ss->root->hierarchy_id, |
Tejun Heo | 3c9c825 | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4352 | atomic_read(&ss->root->nr_cgrps), !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4353 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4354 | mutex_unlock(&cgroup_mutex); |
| 4355 | return 0; |
| 4356 | } |
| 4357 | |
| 4358 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4359 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4360 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4363 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4364 | .open = cgroupstats_open, |
| 4365 | .read = seq_read, |
| 4366 | .llseek = seq_lseek, |
| 4367 | .release = single_release, |
| 4368 | }; |
| 4369 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4370 | /** |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4371 | * cgroup_fork - initialize cgroup related fields during copy_process() |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4372 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4373 | * |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4374 | * A task is associated with the init_css_set until cgroup_post_fork() |
| 4375 | * attaches it to the parent's css_set. Empty cg_list indicates that |
| 4376 | * @child isn't holding reference to its css_set. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4377 | */ |
| 4378 | void cgroup_fork(struct task_struct *child) |
| 4379 | { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4380 | RCU_INIT_POINTER(child->cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4381 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4385 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4386 | * @child: the task in question |
| 4387 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4388 | * Adds the task to the list running through its css_set if necessary and |
| 4389 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4390 | * visible on the task list in case we race with the first call to |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 4391 | * cgroup_task_iter_start() - to guarantee that the new task ends up on its |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4392 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4393 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4394 | void cgroup_post_fork(struct task_struct *child) |
| 4395 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4396 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4397 | int i; |
| 4398 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4399 | /* |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4400 | * This may race against cgroup_enable_task_cg_links(). As that |
| 4401 | * function sets use_task_css_set_links before grabbing |
| 4402 | * tasklist_lock and we just went through tasklist_lock to add |
| 4403 | * @child, it's guaranteed that either we see the set |
| 4404 | * use_task_css_set_links or cgroup_enable_task_cg_lists() sees |
| 4405 | * @child during its iteration. |
| 4406 | * |
| 4407 | * If we won the race, @child is associated with %current's |
| 4408 | * css_set. Grabbing css_set_rwsem guarantees both that the |
| 4409 | * association is stable, and, on completion of the parent's |
| 4410 | * migration, @child is visible in the source of migration or |
| 4411 | * already in the destination cgroup. This guarantee is necessary |
| 4412 | * when implementing operations which need to migrate all tasks of |
| 4413 | * a cgroup to another. |
| 4414 | * |
| 4415 | * Note that if we lose to cgroup_enable_task_cg_links(), @child |
| 4416 | * will remain in init_css_set. This is safe because all tasks are |
| 4417 | * in the init_css_set before cg_links is enabled and there's no |
| 4418 | * operation which transfers all tasks out of init_css_set. |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4419 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4420 | if (use_task_css_set_links) { |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4421 | struct css_set *cset; |
| 4422 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4423 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4424 | cset = task_css_set(current); |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4425 | if (list_empty(&child->cg_list)) { |
| 4426 | rcu_assign_pointer(child->cgroups, cset); |
| 4427 | list_add(&child->cg_list, &cset->tasks); |
| 4428 | get_css_set(cset); |
| 4429 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4430 | up_write(&css_set_rwsem); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4431 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4432 | |
| 4433 | /* |
| 4434 | * Call ss->fork(). This must happen after @child is linked on |
| 4435 | * css_set; otherwise, @child might change state between ->fork() |
| 4436 | * and addition to css_set. |
| 4437 | */ |
| 4438 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4439 | for_each_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4440 | if (ss->fork) |
| 4441 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4442 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4443 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4444 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4445 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4446 | * cgroup_exit - detach cgroup from exiting task |
| 4447 | * @tsk: pointer to task_struct of exiting process |
| 4448 | * |
| 4449 | * Description: Detach cgroup from @tsk and release it. |
| 4450 | * |
| 4451 | * Note that cgroups marked notify_on_release force every task in |
| 4452 | * them to take the global cgroup_mutex mutex when exiting. |
| 4453 | * This could impact scaling on very large systems. Be reluctant to |
| 4454 | * use notify_on_release cgroups where very high task exit scaling |
| 4455 | * is required on large systems. |
| 4456 | * |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4457 | * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We |
| 4458 | * call cgroup_exit() while the task is still competent to handle |
| 4459 | * notify_on_release(), then leave the task attached to the root cgroup in |
| 4460 | * each hierarchy for the remainder of its exit. No need to bother with |
| 4461 | * init_css_set refcnting. init_css_set never goes away and we can't race |
Li Zefan | e8604cb | 2014-03-28 15:18:27 +0800 | [diff] [blame] | 4462 | * with migration path - PF_EXITING is visible to migration path. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4463 | */ |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 4464 | void cgroup_exit(struct task_struct *tsk) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4465 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4466 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4467 | struct css_set *cset; |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4468 | bool put_cset = false; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4469 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4470 | |
| 4471 | /* |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4472 | * Unlink from @tsk from its css_set. As migration path can't race |
| 4473 | * with us, we can check cg_list without grabbing css_set_rwsem. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4474 | */ |
| 4475 | if (!list_empty(&tsk->cg_list)) { |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4476 | down_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4477 | list_del_init(&tsk->cg_list); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4478 | up_write(&css_set_rwsem); |
Tejun Heo | 0e1d768 | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4479 | put_cset = true; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4480 | } |
| 4481 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4482 | /* Reassign the task to the init_css_set. */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4483 | cset = task_css_set(tsk); |
| 4484 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4485 | |
Li Zefan | 1ec4183 | 2014-03-28 15:22:19 +0800 | [diff] [blame] | 4486 | if (need_forkexit_callback) { |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4487 | /* see cgroup_post_fork() for details */ |
| 4488 | for_each_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4489 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4490 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 4491 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4492 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4493 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | } |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4497 | |
Tejun Heo | eaf797a | 2014-02-25 10:04:03 -0500 | [diff] [blame] | 4498 | if (put_cset) |
| 4499 | put_css_set(cset, true); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4500 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4501 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4502 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4503 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4504 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4505 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4506 | /* |
| 4507 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4508 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4509 | * it now |
| 4510 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4511 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4512 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4513 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4514 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4515 | list_empty(&cgrp->release_list)) { |
| 4516 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4517 | need_schedule_work = 1; |
| 4518 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4519 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4520 | if (need_schedule_work) |
| 4521 | schedule_work(&release_agent_work); |
| 4522 | } |
| 4523 | } |
| 4524 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4525 | /* |
| 4526 | * Notify userspace when a cgroup is released, by running the |
| 4527 | * configured release agent with the name of the cgroup (path |
| 4528 | * relative to the root of cgroup file system) as the argument. |
| 4529 | * |
| 4530 | * Most likely, this user command will try to rmdir this cgroup. |
| 4531 | * |
| 4532 | * This races with the possibility that some other task will be |
| 4533 | * attached to this cgroup before it is removed, or that some other |
| 4534 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4535 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4536 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4537 | * to continue to serve a useful existence. Next time it's released, |
| 4538 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4539 | * |
| 4540 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4541 | * means only wait until the task is successfully execve()'d. The |
| 4542 | * separate release agent task is forked by call_usermodehelper(), |
| 4543 | * then control in this thread returns here, without waiting for the |
| 4544 | * release agent task. We don't bother to wait because the caller of |
| 4545 | * this routine has no use for the exit status of the release agent |
| 4546 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4547 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4548 | static void cgroup_release_agent(struct work_struct *work) |
| 4549 | { |
| 4550 | BUG_ON(work != &release_agent_work); |
| 4551 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4552 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4553 | while (!list_empty(&release_list)) { |
| 4554 | char *argv[3], *envp[3]; |
| 4555 | int i; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4556 | char *pathbuf = NULL, *agentbuf = NULL, *path; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4557 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4558 | struct cgroup, |
| 4559 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4560 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4561 | raw_spin_unlock(&release_list_lock); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4562 | pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4563 | if (!pathbuf) |
| 4564 | goto continue_free; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4565 | path = cgroup_path(cgrp, pathbuf, PATH_MAX); |
| 4566 | if (!path) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4567 | goto continue_free; |
| 4568 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 4569 | if (!agentbuf) |
| 4570 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4571 | |
| 4572 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4573 | argv[i++] = agentbuf; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4574 | argv[i++] = path; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4575 | argv[i] = NULL; |
| 4576 | |
| 4577 | i = 0; |
| 4578 | /* minimal command environment */ |
| 4579 | envp[i++] = "HOME=/"; |
| 4580 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 4581 | envp[i] = NULL; |
| 4582 | |
| 4583 | /* Drop the lock while we invoke the usermode helper, |
| 4584 | * since the exec could involve hitting disk and hence |
| 4585 | * be a slow process */ |
| 4586 | mutex_unlock(&cgroup_mutex); |
| 4587 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4588 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 4589 | continue_free: |
| 4590 | kfree(pathbuf); |
| 4591 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4592 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4593 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4594 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4595 | mutex_unlock(&cgroup_mutex); |
| 4596 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4597 | |
| 4598 | static int __init cgroup_disable(char *str) |
| 4599 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4600 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4601 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4602 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4603 | |
| 4604 | while ((token = strsep(&str, ",")) != NULL) { |
| 4605 | if (!*token) |
| 4606 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4607 | |
Tejun Heo | 3ed80a6 | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4608 | for_each_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4609 | if (!strcmp(token, ss->name)) { |
| 4610 | ss->disabled = 1; |
| 4611 | printk(KERN_INFO "Disabling %s control group" |
| 4612 | " subsystem\n", ss->name); |
| 4613 | break; |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | return 1; |
| 4618 | } |
| 4619 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4620 | |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4621 | /** |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4622 | * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4623 | * @dentry: directory dentry of interest |
| 4624 | * @ss: subsystem of interest |
Tejun Heo | b77d7b6 | 2013-08-13 11:01:54 -0400 | [diff] [blame] | 4625 | * |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4626 | * If @dentry is a directory for a cgroup which has @ss enabled on it, try |
| 4627 | * to get the corresponding css and return it. If such css doesn't exist |
| 4628 | * or can't be pinned, an ERR_PTR value is returned. |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4629 | */ |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4630 | struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry, |
| 4631 | struct cgroup_subsys *ss) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4632 | { |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4633 | struct kernfs_node *kn = kernfs_node_from_dentry(dentry); |
| 4634 | struct cgroup_subsys_state *css = NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4635 | struct cgroup *cgrp; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4636 | |
Tejun Heo | 35cf083 | 2013-08-26 18:40:56 -0400 | [diff] [blame] | 4637 | /* is @dentry a cgroup dir? */ |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4638 | if (dentry->d_sb->s_type != &cgroup_fs_type || !kn || |
| 4639 | kernfs_type(kn) != KERNFS_DIR) |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4640 | return ERR_PTR(-EBADF); |
| 4641 | |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4642 | rcu_read_lock(); |
| 4643 | |
Tejun Heo | 2bd59d4 | 2014-02-11 11:52:49 -0500 | [diff] [blame] | 4644 | /* |
| 4645 | * This path doesn't originate from kernfs and @kn could already |
| 4646 | * have been or be removed at any point. @kn->priv is RCU |
| 4647 | * protected for this access. See destroy_locked() for details. |
| 4648 | */ |
| 4649 | cgrp = rcu_dereference(kn->priv); |
| 4650 | if (cgrp) |
| 4651 | css = cgroup_css(cgrp, ss); |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4652 | |
| 4653 | if (!css || !css_tryget(css)) |
| 4654 | css = ERR_PTR(-ENOENT); |
| 4655 | |
| 4656 | rcu_read_unlock(); |
| 4657 | return css; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4658 | } |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4659 | |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4660 | /** |
| 4661 | * css_from_id - lookup css by id |
| 4662 | * @id: the cgroup id |
| 4663 | * @ss: cgroup subsys to be looked into |
| 4664 | * |
| 4665 | * Returns the css if there's valid one with @id, otherwise returns NULL. |
| 4666 | * Should be called under rcu_read_lock(). |
| 4667 | */ |
| 4668 | struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) |
| 4669 | { |
| 4670 | struct cgroup *cgrp; |
| 4671 | |
Tejun Heo | ace2bee | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 4672 | cgroup_assert_mutexes_or_rcu_locked(); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4673 | |
| 4674 | cgrp = idr_find(&ss->root->cgroup_idr, id); |
| 4675 | if (cgrp) |
Tejun Heo | d162596 | 2013-08-27 14:27:23 -0400 | [diff] [blame] | 4676 | return cgroup_css(cgrp, ss); |
Li Zefan | 1cb650b | 2013-08-19 10:05:24 +0800 | [diff] [blame] | 4677 | return NULL; |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 4678 | } |
| 4679 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4680 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4681 | static struct cgroup_subsys_state * |
| 4682 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4683 | { |
| 4684 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 4685 | |
| 4686 | if (!css) |
| 4687 | return ERR_PTR(-ENOMEM); |
| 4688 | |
| 4689 | return css; |
| 4690 | } |
| 4691 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4692 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4693 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4694 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4695 | } |
| 4696 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4697 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 4698 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4699 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4700 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4701 | } |
| 4702 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4703 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 4704 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4705 | { |
| 4706 | return (u64)(unsigned long)current->cgroups; |
| 4707 | } |
| 4708 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4709 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 4710 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4711 | { |
| 4712 | u64 count; |
| 4713 | |
| 4714 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 4715 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4716 | rcu_read_unlock(); |
| 4717 | return count; |
| 4718 | } |
| 4719 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4720 | static int current_css_set_cg_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4721 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4722 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4723 | struct css_set *cset; |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4724 | char *name_buf; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4725 | |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4726 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 4727 | if (!name_buf) |
| 4728 | return -ENOMEM; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4729 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4730 | down_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4731 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4732 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4733 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4734 | struct cgroup *c = link->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4735 | |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4736 | cgroup_name(c, name_buf, NAME_MAX + 1); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4737 | seq_printf(seq, "Root %d group %s\n", |
Tejun Heo | a2dd424 | 2014-03-19 10:23:55 -0400 | [diff] [blame] | 4738 | c->root->hierarchy_id, name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4739 | } |
| 4740 | rcu_read_unlock(); |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4741 | up_read(&css_set_rwsem); |
Tejun Heo | e61734c | 2014-02-12 09:29:50 -0500 | [diff] [blame] | 4742 | kfree(name_buf); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4743 | return 0; |
| 4744 | } |
| 4745 | |
| 4746 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4747 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4748 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4749 | struct cgroup_subsys_state *css = seq_css(seq); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4750 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4751 | |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4752 | down_read(&css_set_rwsem); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4753 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4754 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4755 | struct task_struct *task; |
| 4756 | int count = 0; |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4757 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4758 | seq_printf(seq, "css_set %p\n", cset); |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4759 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4760 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4761 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 4762 | goto overflow; |
| 4763 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4764 | } |
Tejun Heo | c756112 | 2014-02-25 10:04:01 -0500 | [diff] [blame] | 4765 | |
| 4766 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
| 4767 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) |
| 4768 | goto overflow; |
| 4769 | seq_printf(seq, " task %d\n", task_pid_vnr(task)); |
| 4770 | } |
| 4771 | continue; |
| 4772 | overflow: |
| 4773 | seq_puts(seq, " ...\n"); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4774 | } |
Tejun Heo | 96d365e | 2014-02-13 06:58:40 -0500 | [diff] [blame] | 4775 | up_read(&css_set_rwsem); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4776 | return 0; |
| 4777 | } |
| 4778 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4779 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4780 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4781 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | static struct cftype debug_files[] = { |
| 4785 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4786 | .name = "taskcount", |
| 4787 | .read_u64 = debug_taskcount_read, |
| 4788 | }, |
| 4789 | |
| 4790 | { |
| 4791 | .name = "current_css_set", |
| 4792 | .read_u64 = current_css_set_read, |
| 4793 | }, |
| 4794 | |
| 4795 | { |
| 4796 | .name = "current_css_set_refcount", |
| 4797 | .read_u64 = current_css_set_refcount_read, |
| 4798 | }, |
| 4799 | |
| 4800 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4801 | .name = "current_css_set_cg_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4802 | .seq_show = current_css_set_cg_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4803 | }, |
| 4804 | |
| 4805 | { |
| 4806 | .name = "cgroup_css_links", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4807 | .seq_show = cgroup_css_links_read, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4808 | }, |
| 4809 | |
| 4810 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4811 | .name = "releasable", |
| 4812 | .read_u64 = releasable_read, |
| 4813 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4814 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4815 | { } /* terminate */ |
| 4816 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4817 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 4818 | struct cgroup_subsys debug_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4819 | .css_alloc = debug_css_alloc, |
| 4820 | .css_free = debug_css_free, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4821 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 4822 | }; |
| 4823 | #endif /* CONFIG_CGROUP_DEBUG */ |