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 | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/magic.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 54 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 55 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 56 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 57 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 58 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 59 | #include <linux/eventfd.h> |
| 60 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 61 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 62 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* |
| 67 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 68 | * hierarchy must be performed while holding it. |
| 69 | * |
| 70 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 71 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 72 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 73 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 74 | * break the following locking order cycle. |
| 75 | * |
| 76 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 77 | * B. namespace_sem -> cgroup_mutex |
| 78 | * |
| 79 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 80 | * breaks it. |
| 81 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_RCU |
| 83 | DEFINE_MUTEX(cgroup_mutex); |
| 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 85 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 93 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | * registered after that. The mutable section of this array is protected by |
| 95 | * cgroup_mutex. |
| 96 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 97 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 98 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 99 | static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 104 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 105 | * unattached - it never has more than a single cgroup, and all tasks are |
| 106 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 107 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 108 | static struct cgroupfs_root cgroup_dummy_root; |
| 109 | |
| 110 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 111 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 113 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 114 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 115 | */ |
| 116 | struct cfent { |
| 117 | struct list_head node; |
| 118 | struct dentry *dentry; |
| 119 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 120 | |
| 121 | /* file xattrs */ |
| 122 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 126 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 127 | * cgroup_subsys->use_id != 0. |
| 128 | */ |
| 129 | #define CSS_ID_MAX (65535) |
| 130 | struct css_id { |
| 131 | /* |
| 132 | * The css to which this ID points. This pointer is set to valid value |
| 133 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 134 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 135 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 136 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 137 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 138 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 139 | /* |
| 140 | * ID of this css. |
| 141 | */ |
| 142 | unsigned short id; |
| 143 | /* |
| 144 | * Depth in hierarchy which this ID belongs to. |
| 145 | */ |
| 146 | unsigned short depth; |
| 147 | /* |
| 148 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 149 | */ |
| 150 | struct rcu_head rcu_head; |
| 151 | /* |
| 152 | * Hierarchy of CSS ID belongs to. |
| 153 | */ |
| 154 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 155 | }; |
| 156 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 157 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 158 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 159 | */ |
| 160 | struct cgroup_event { |
| 161 | /* |
| 162 | * Cgroup which the event belongs to. |
| 163 | */ |
| 164 | struct cgroup *cgrp; |
| 165 | /* |
| 166 | * Control file which the event associated. |
| 167 | */ |
| 168 | struct cftype *cft; |
| 169 | /* |
| 170 | * eventfd to signal userspace about the event. |
| 171 | */ |
| 172 | struct eventfd_ctx *eventfd; |
| 173 | /* |
| 174 | * Each of these stored in a list by the cgroup. |
| 175 | */ |
| 176 | struct list_head list; |
| 177 | /* |
| 178 | * All fields below needed to unregister event when |
| 179 | * userspace closes eventfd. |
| 180 | */ |
| 181 | poll_table pt; |
| 182 | wait_queue_head_t *wqh; |
| 183 | wait_queue_t wait; |
| 184 | struct work_struct remove; |
| 185 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | /* The list of hierarchy roots */ |
| 188 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 189 | static LIST_HEAD(cgroup_roots); |
| 190 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 191 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 194 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 195 | * writes, either for reads. |
| 196 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 197 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 198 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 199 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 200 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 201 | /* |
| 202 | * Assign a monotonically increasing serial number to cgroups. It |
| 203 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 204 | * numbers. Also, as cgroups are always appended to the parent's |
| 205 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 206 | * the ascending serial number order on the list. Protected by |
| 207 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 208 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 209 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 210 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 211 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 212 | * check for fork/exit handlers to call. This avoids us having to do |
| 213 | * extra work in the fork/exit path if none of the subsystems need to |
| 214 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 215 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 216 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 217 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 218 | static struct cftype cgroup_base_files[]; |
| 219 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 220 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 221 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 222 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 223 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 224 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 226 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 228 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 231 | /** |
| 232 | * cgroup_is_descendant - test ancestry |
| 233 | * @cgrp: the cgroup to be tested |
| 234 | * @ancestor: possible ancestor of @cgrp |
| 235 | * |
| 236 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 237 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 238 | * and @ancestor are accessible. |
| 239 | */ |
| 240 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 241 | { |
| 242 | while (cgrp) { |
| 243 | if (cgrp == ancestor) |
| 244 | return true; |
| 245 | cgrp = cgrp->parent; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 250 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 251 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 252 | { |
| 253 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 254 | (1 << CGRP_RELEASABLE) | |
| 255 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 256 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 259 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 261 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 264 | /** |
| 265 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 266 | * @ss: the iteration cursor |
| 267 | * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 268 | * |
| 269 | * Should be called under cgroup_mutex. |
| 270 | */ |
| 271 | #define for_each_subsys(ss, i) \ |
| 272 | for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ |
| 273 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 274 | !((ss) = cgroup_subsys[i]); })) { } \ |
| 275 | else |
| 276 | |
| 277 | /** |
| 278 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 279 | * @ss: the iteration cursor |
| 280 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 281 | * |
| 282 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 283 | * require any synchronization. |
| 284 | */ |
| 285 | #define for_each_builtin_subsys(ss, i) \ |
| 286 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 287 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 288 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 289 | /* iterate each subsystem attached to a hierarchy */ |
| 290 | #define for_each_root_subsys(root, ss) \ |
| 291 | list_for_each_entry((ss), &(root)->subsys_list, sibling) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 292 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 293 | /* iterate across the active hierarchies */ |
| 294 | #define for_each_active_root(root) \ |
| 295 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 296 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 297 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 298 | { |
| 299 | return dentry->d_fsdata; |
| 300 | } |
| 301 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 302 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 303 | { |
| 304 | return dentry->d_fsdata; |
| 305 | } |
| 306 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 307 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 308 | { |
| 309 | return __d_cfe(dentry)->type; |
| 310 | } |
| 311 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 312 | /** |
| 313 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 314 | * @cgrp: the cgroup to be checked for liveness |
| 315 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 316 | * On success, returns true; the mutex should be later unlocked. On |
| 317 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 318 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 319 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 320 | { |
| 321 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 322 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 323 | mutex_unlock(&cgroup_mutex); |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 328 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 329 | /* the list of cgroups eligible for automatic release. Protected by |
| 330 | * release_list_lock */ |
| 331 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 332 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 333 | static void cgroup_release_agent(struct work_struct *work); |
| 334 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 335 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 336 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 337 | /* |
| 338 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 339 | * belong to different cgroups on different hierarchies. In the other |
| 340 | * direction, a css_set is naturally associated with multiple cgroups. |
| 341 | * This M:N relationship is represented by the following link structure |
| 342 | * which exists for each association and allows traversing the associations |
| 343 | * from both sides. |
| 344 | */ |
| 345 | struct cgrp_cset_link { |
| 346 | /* the cgroup and css_set this link associates */ |
| 347 | struct cgroup *cgrp; |
| 348 | struct css_set *cset; |
| 349 | |
| 350 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 351 | struct list_head cset_link; |
| 352 | |
| 353 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 354 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | /* The default css_set - used by init and its children prior to any |
| 358 | * hierarchies being mounted. It contains a pointer to the root state |
| 359 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 360 | * reference-counted, to improve performance when child cgroups |
| 361 | * haven't been created. |
| 362 | */ |
| 363 | |
| 364 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 365 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 366 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 367 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 368 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 369 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 370 | /* css_set_lock protects the list of css_set objects, and the |
| 371 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 372 | * due to cgroup_iter_start() */ |
| 373 | static DEFINE_RWLOCK(css_set_lock); |
| 374 | static int css_set_count; |
| 375 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 376 | /* |
| 377 | * hash table for cgroup groups. This improves the performance to find |
| 378 | * an existing css_set. This hash doesn't (currently) take into |
| 379 | * account cgroups in empty hierarchies. |
| 380 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 381 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 382 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 383 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 384 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 385 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 386 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 387 | struct cgroup_subsys *ss; |
| 388 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 389 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 390 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 391 | key += (unsigned long)css[i]; |
| 392 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 393 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 394 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 397 | /* We don't maintain the lists running through each css_set to its |
| 398 | * task until after the first call to cgroup_iter_start(). This |
| 399 | * reduces the fork()/exit() overhead for people who have cgroups |
| 400 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 401 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 402 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 403 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 404 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 405 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 406 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Ensure that the refcount doesn't hit zero while any readers |
| 409 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 410 | * rwlock |
| 411 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 412 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 413 | return; |
| 414 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 415 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 416 | write_unlock(&css_set_lock); |
| 417 | return; |
| 418 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 419 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 420 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 421 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 422 | css_set_count--; |
| 423 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 424 | 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] | 425 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 426 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 427 | list_del(&link->cset_link); |
| 428 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 429 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 430 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 431 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 432 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 433 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 434 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 435 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 436 | |
| 437 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 438 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 439 | |
| 440 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 441 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * refcounted get/put for css_set objects |
| 446 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 448 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 449 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 452 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 453 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 454 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 457 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 458 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 459 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 462 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 463 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 464 | * @cset: candidate css_set being tested |
| 465 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 466 | * @new_cgrp: cgroup that's being entered by the task |
| 467 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 468 | * |
| 469 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 470 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 471 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 472 | static bool compare_css_sets(struct css_set *cset, |
| 473 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | struct cgroup *new_cgrp, |
| 475 | struct cgroup_subsys_state *template[]) |
| 476 | { |
| 477 | struct list_head *l1, *l2; |
| 478 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 479 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | /* Not all subsystems matched */ |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Compare cgroup pointers in order to distinguish between |
| 486 | * different cgroups in heirarchies with no subsystems. We |
| 487 | * could get by with just this check alone (and skip the |
| 488 | * memcmp above) but on most setups the memcmp check will |
| 489 | * avoid the need for this more expensive check on almost all |
| 490 | * candidates. |
| 491 | */ |
| 492 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 493 | l1 = &cset->cgrp_links; |
| 494 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 495 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 496 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 497 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | |
| 499 | l1 = l1->next; |
| 500 | l2 = l2->next; |
| 501 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 502 | if (l1 == &cset->cgrp_links) { |
| 503 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 504 | break; |
| 505 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 506 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 507 | } |
| 508 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 509 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 510 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 511 | cgrp1 = link1->cgrp; |
| 512 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 513 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 514 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * If this hierarchy is the hierarchy of the cgroup |
| 518 | * that's changing, then we need to check that this |
| 519 | * css_set points to the new cgroup; if it's any other |
| 520 | * hierarchy, then this css_set should point to the |
| 521 | * same cgroup as the old css_set. |
| 522 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 523 | if (cgrp1->root == new_cgrp->root) { |
| 524 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | return true; |
| 532 | } |
| 533 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 534 | /** |
| 535 | * find_existing_css_set - init css array and find the matching css_set |
| 536 | * @old_cset: the css_set that we're using before the cgroup transition |
| 537 | * @cgrp: the cgroup that we're moving into |
| 538 | * @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] | 539 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 541 | struct cgroup *cgrp, |
| 542 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 544 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 545 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 546 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 547 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 548 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 549 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 550 | /* |
| 551 | * Build the set of subsystem state objects that we want to see in the |
| 552 | * new css_set. while subsystems can change globally, the entries here |
| 553 | * won't change, so no need for locking. |
| 554 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 555 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 556 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | /* Subsystem is in this hierarchy. So we want |
| 558 | * the subsystem state from the new |
| 559 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 560 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | } else { |
| 562 | /* Subsystem is not in this hierarchy, so we |
| 563 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 564 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 568 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 569 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 570 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 571 | continue; |
| 572 | |
| 573 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 574 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 575 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* No existing cgroup group matched */ |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 582 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 584 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 586 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 587 | kfree(link); |
| 588 | } |
| 589 | } |
| 590 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 591 | /** |
| 592 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 593 | * @count: the number of links to allocate |
| 594 | * @tmp_links: list_head the allocated links are put on |
| 595 | * |
| 596 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 597 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 598 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | 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] | 600 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 601 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 602 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | |
| 604 | INIT_LIST_HEAD(tmp_links); |
| 605 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 606 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 607 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 608 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 609 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 610 | return -ENOMEM; |
| 611 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 612 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 617 | /** |
| 618 | * 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] | 619 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 620 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | * @cgrp: the destination cgroup |
| 622 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 623 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 624 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 625 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 626 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 627 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 628 | BUG_ON(list_empty(tmp_links)); |
| 629 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 630 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 631 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 632 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 633 | /* |
| 634 | * Always add links to the tail of the list so that the list |
| 635 | * is sorted by order of hierarchy creation |
| 636 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 637 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 640 | /** |
| 641 | * find_css_set - return a new css_set with one cgroup updated |
| 642 | * @old_cset: the baseline css_set |
| 643 | * @cgrp: the cgroup to be updated |
| 644 | * |
| 645 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 646 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 648 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 649 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 651 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 652 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 653 | struct list_head tmp_links; |
| 654 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 655 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 656 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 657 | lockdep_assert_held(&cgroup_mutex); |
| 658 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 | /* First see if we already have a cgroup group that matches |
| 660 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 661 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 662 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 663 | if (cset) |
| 664 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 665 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 667 | if (cset) |
| 668 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 669 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 670 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 671 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | return NULL; |
| 673 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 675 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 676 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | return NULL; |
| 678 | } |
| 679 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 680 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 681 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | INIT_LIST_HEAD(&cset->tasks); |
| 683 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 684 | |
| 685 | /* Copy the set of subsystem state objects generated in |
| 686 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 | |
| 689 | write_lock(&css_set_lock); |
| 690 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 691 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 692 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 693 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 694 | if (c->root == cgrp->root) |
| 695 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 696 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 697 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 698 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 699 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 700 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 701 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 702 | |
| 703 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 704 | key = css_set_hash(cset->subsys); |
| 705 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 706 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 707 | write_unlock(&css_set_lock); |
| 708 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 709 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 712 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 713 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 714 | * called with cgroup_mutex held. |
| 715 | */ |
| 716 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 717 | struct cgroupfs_root *root) |
| 718 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 719 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 720 | struct cgroup *res = NULL; |
| 721 | |
| 722 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 723 | read_lock(&css_set_lock); |
| 724 | /* |
| 725 | * No need to lock the task - since we hold cgroup_mutex the |
| 726 | * task can't change groups, so the only thing that can happen |
| 727 | * is that it exits and its css is set back to init_css_set. |
| 728 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 729 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 730 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 731 | res = &root->top_cgroup; |
| 732 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 733 | struct cgrp_cset_link *link; |
| 734 | |
| 735 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 736 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 737 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 738 | if (c->root == root) { |
| 739 | res = c; |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | read_unlock(&css_set_lock); |
| 745 | BUG_ON(!res); |
| 746 | return res; |
| 747 | } |
| 748 | |
| 749 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 750 | * There is one global cgroup mutex. We also require taking |
| 751 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 752 | * See "The task_lock() exception", at the end of this comment. |
| 753 | * |
| 754 | * A task must hold cgroup_mutex to modify cgroups. |
| 755 | * |
| 756 | * Any task can increment and decrement the count field without lock. |
| 757 | * So in general, code holding cgroup_mutex can't rely on the count |
| 758 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 759 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 760 | * means that no tasks are currently attached, therefore there is no |
| 761 | * way a task attached to that cgroup can fork (the other way to |
| 762 | * increment the count). So code holding cgroup_mutex can safely |
| 763 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 764 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 765 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 766 | * needs that mutex. |
| 767 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 768 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 769 | * (usually) take cgroup_mutex. These are the two most performance |
| 770 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 771 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 772 | * 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] | 773 | * to the release agent with the name of the cgroup (path relative to |
| 774 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 | * |
| 776 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 777 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 778 | * tasks in the system use _some_ cgroup, and since there is always at |
| 779 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 780 | * always has either children cgroups and/or using tasks. So we don't |
| 781 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 782 | * |
| 783 | * The task_lock() exception |
| 784 | * |
| 785 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 786 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 787 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 788 | * several performance critical places that need to reference |
| 789 | * task->cgroup without the expense of grabbing a system global |
| 790 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 791 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 792 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 793 | * the task_struct routinely used for such matters. |
| 794 | * |
| 795 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 796 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 797 | */ |
| 798 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | /* |
| 800 | * A couple of forward declarations required, due to cyclic reference loop: |
| 801 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 802 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 803 | * -> cgroup_mkdir. |
| 804 | */ |
| 805 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 806 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 807 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 808 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 809 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 810 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 811 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 812 | |
| 813 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 814 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 815 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 816 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 817 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 818 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 819 | struct cgroup *parent, struct cgroup *child); |
| 820 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 821 | static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 822 | { |
| 823 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 824 | |
| 825 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 826 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 827 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 828 | inode->i_uid = current_fsuid(); |
| 829 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 830 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 831 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 832 | } |
| 833 | return inode; |
| 834 | } |
| 835 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 836 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 837 | { |
| 838 | struct cgroup_name *name; |
| 839 | |
| 840 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 841 | if (!name) |
| 842 | return NULL; |
| 843 | strcpy(name->name, dentry->d_name.name); |
| 844 | return name; |
| 845 | } |
| 846 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 847 | static void cgroup_free_fn(struct work_struct *work) |
| 848 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 849 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 850 | struct cgroup_subsys *ss; |
| 851 | |
| 852 | mutex_lock(&cgroup_mutex); |
| 853 | /* |
| 854 | * Release the subsystem state objects. |
| 855 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 856 | for_each_root_subsys(cgrp->root, ss) |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 857 | ss->css_free(cgrp); |
| 858 | |
| 859 | cgrp->root->number_of_cgroups--; |
| 860 | mutex_unlock(&cgroup_mutex); |
| 861 | |
| 862 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 863 | * We get a ref to the parent's dentry, and put the ref when |
| 864 | * this cgroup is being freed, so it's guaranteed that the |
| 865 | * parent won't be destroyed before its children. |
| 866 | */ |
| 867 | dput(cgrp->parent->dentry); |
| 868 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 869 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 870 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 871 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 872 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 873 | * created the cgroup. This will free cgrp->root, if we are |
| 874 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 875 | */ |
| 876 | deactivate_super(cgrp->root->sb); |
| 877 | |
| 878 | /* |
| 879 | * if we're getting rid of the cgroup, refcount should ensure |
| 880 | * that there are no pidlists left. |
| 881 | */ |
| 882 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 883 | |
| 884 | simple_xattrs_free(&cgrp->xattrs); |
| 885 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 886 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 887 | kfree(cgrp); |
| 888 | } |
| 889 | |
| 890 | static void cgroup_free_rcu(struct rcu_head *head) |
| 891 | { |
| 892 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 893 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 894 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 895 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 896 | } |
| 897 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 898 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 899 | { |
| 900 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 901 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 902 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 903 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 904 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 905 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 906 | } else { |
| 907 | struct cfent *cfe = __d_cfe(dentry); |
| 908 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 909 | |
| 910 | WARN_ONCE(!list_empty(&cfe->node) && |
| 911 | cgrp != &cgrp->root->top_cgroup, |
| 912 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 913 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 914 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 915 | } |
| 916 | iput(inode); |
| 917 | } |
| 918 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 919 | static int cgroup_delete(const struct dentry *d) |
| 920 | { |
| 921 | return 1; |
| 922 | } |
| 923 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 924 | static void remove_dir(struct dentry *d) |
| 925 | { |
| 926 | struct dentry *parent = dget(d->d_parent); |
| 927 | |
| 928 | d_delete(d); |
| 929 | simple_rmdir(parent->d_inode, d); |
| 930 | dput(parent); |
| 931 | } |
| 932 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 933 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 935 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 936 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 937 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 938 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 939 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 940 | /* |
| 941 | * If we're doing cleanup due to failure of cgroup_create(), |
| 942 | * the corresponding @cfe may not exist. |
| 943 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 944 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 945 | struct dentry *d = cfe->dentry; |
| 946 | |
| 947 | if (cft && cfe->type != cft) |
| 948 | continue; |
| 949 | |
| 950 | dget(d); |
| 951 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 952 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 953 | list_del_init(&cfe->node); |
| 954 | dput(d); |
| 955 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 956 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 957 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 960 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 961 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 962 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 963 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 964 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 965 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 966 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 967 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 968 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 969 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 970 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 971 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 972 | |
| 973 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 974 | continue; |
| 975 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 976 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 977 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * NOTE : the dentry must have been dget()'ed |
| 982 | */ |
| 983 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 984 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 985 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 987 | parent = dentry->d_parent; |
| 988 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 989 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 991 | spin_unlock(&dentry->d_lock); |
| 992 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | remove_dir(dentry); |
| 994 | } |
| 995 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 996 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 997 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 998 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 999 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1000 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1002 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1004 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1005 | struct cgroup_subsys *ss; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1006 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1007 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1008 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1009 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1010 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1011 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1012 | for_each_subsys(ss, i) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1013 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1014 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1015 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1016 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1017 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1018 | if (ss->root != &cgroup_dummy_root) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1019 | /* Subsystem isn't free */ |
| 1020 | return -EBUSY; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* Currently we don't handle adding/removing subsystems when |
| 1025 | * any child cgroups exist. This is theoretically supportable |
| 1026 | * but involves complex error handling, so it's being left until |
| 1027 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1028 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1029 | return -EBUSY; |
| 1030 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1031 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1032 | if (ret) |
| 1033 | return ret; |
| 1034 | |
| 1035 | /* |
| 1036 | * Nothing can fail from this point on. Remove files for the |
| 1037 | * removed subsystems and rebind each subsystem. |
| 1038 | */ |
| 1039 | cgroup_clear_dir(cgrp, removed_mask); |
| 1040 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1041 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1042 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1043 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1044 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1046 | BUG_ON(cgrp->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1047 | BUG_ON(!cgroup_dummy_top->subsys[i]); |
| 1048 | BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1049 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1050 | cgrp->subsys[i] = cgroup_dummy_top->subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1051 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1052 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1053 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1054 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1055 | ss->bind(cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1056 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1057 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1058 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1059 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1060 | /* We're removing this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1061 | BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1062 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1063 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1064 | if (ss->bind) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1065 | ss->bind(cgroup_dummy_top); |
| 1066 | cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1067 | cgrp->subsys[i] = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1068 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1069 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1070 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1071 | /* subsystem is now free - drop reference on module */ |
| 1072 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1073 | root->subsys_mask &= ~bit; |
| 1074 | } else if (bit & root->subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | /* Subsystem state should already exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1076 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1077 | /* |
| 1078 | * a refcount was taken, but we already had one, so |
| 1079 | * drop the extra reference. |
| 1080 | */ |
| 1081 | module_put(ss->module); |
| 1082 | #ifdef CONFIG_MODULE_UNLOAD |
| 1083 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1084 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1085 | } else { |
| 1086 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1087 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1090 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1091 | /* |
| 1092 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1093 | * now matches the bound subsystems. |
| 1094 | */ |
| 1095 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1096 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1100 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1102 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1103 | struct cgroup_subsys *ss; |
| 1104 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1105 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1106 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1107 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1108 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1109 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1110 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1111 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1112 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1113 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1114 | if (strlen(root->release_agent_path)) |
| 1115 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1116 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1117 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1118 | if (strlen(root->name)) |
| 1119 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1120 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1125 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1126 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1127 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1128 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1129 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1130 | /* User explicitly requested empty subsystem */ |
| 1131 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1132 | |
| 1133 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1134 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1135 | }; |
| 1136 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1137 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1138 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1139 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1140 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1141 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1142 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1143 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1144 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1145 | char *token, *o = data; |
| 1146 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1147 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1148 | bool module_pin_failed = false; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1149 | struct cgroup_subsys *ss; |
| 1150 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1151 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1152 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1153 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1154 | #ifdef CONFIG_CPUSETS |
| 1155 | mask = ~(1UL << cpuset_subsys_id); |
| 1156 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1157 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1158 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1159 | |
| 1160 | while ((token = strsep(&o, ",")) != NULL) { |
| 1161 | if (!*token) |
| 1162 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1163 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1164 | /* Explicitly have no subsystems */ |
| 1165 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1166 | continue; |
| 1167 | } |
| 1168 | if (!strcmp(token, "all")) { |
| 1169 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1170 | if (one_ss) |
| 1171 | return -EINVAL; |
| 1172 | all_ss = true; |
| 1173 | continue; |
| 1174 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1175 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1176 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1177 | continue; |
| 1178 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1179 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1180 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1181 | continue; |
| 1182 | } |
| 1183 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1184 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1185 | continue; |
| 1186 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1187 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1188 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1191 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1192 | /* Specifying two release agents is forbidden */ |
| 1193 | if (opts->release_agent) |
| 1194 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1195 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1196 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1197 | if (!opts->release_agent) |
| 1198 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1199 | continue; |
| 1200 | } |
| 1201 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1202 | const char *name = token + 5; |
| 1203 | /* Can't specify an empty name */ |
| 1204 | if (!strlen(name)) |
| 1205 | return -EINVAL; |
| 1206 | /* Must match [\w.-]+ */ |
| 1207 | for (i = 0; i < strlen(name); i++) { |
| 1208 | char c = name[i]; |
| 1209 | if (isalnum(c)) |
| 1210 | continue; |
| 1211 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1212 | continue; |
| 1213 | return -EINVAL; |
| 1214 | } |
| 1215 | /* Specifying two names is forbidden */ |
| 1216 | if (opts->name) |
| 1217 | return -EINVAL; |
| 1218 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1219 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1220 | GFP_KERNEL); |
| 1221 | if (!opts->name) |
| 1222 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1223 | |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1227 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1228 | if (strcmp(token, ss->name)) |
| 1229 | continue; |
| 1230 | if (ss->disabled) |
| 1231 | continue; |
| 1232 | |
| 1233 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1234 | if (all_ss) |
| 1235 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1236 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1237 | one_ss = true; |
| 1238 | |
| 1239 | break; |
| 1240 | } |
| 1241 | if (i == CGROUP_SUBSYS_COUNT) |
| 1242 | return -ENOENT; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1247 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1248 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1249 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1250 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1251 | for_each_subsys(ss, i) |
| 1252 | if (!ss->disabled) |
| 1253 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1254 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1255 | /* Consistency checks */ |
| 1256 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1257 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1258 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1259 | |
| 1260 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1261 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1262 | return -EINVAL; |
| 1263 | } |
| 1264 | |
| 1265 | if (opts->cpuset_clone_children) { |
| 1266 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1267 | return -EINVAL; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1271 | /* |
| 1272 | * Option noprefix was introduced just for backward compatibility |
| 1273 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1274 | * the cpuset subsystem. |
| 1275 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1276 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1277 | return -EINVAL; |
| 1278 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1279 | |
| 1280 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1281 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1282 | return -EINVAL; |
| 1283 | |
| 1284 | /* |
| 1285 | * We either have to specify by name or by subsystems. (So all |
| 1286 | * empty hierarchies must have a name). |
| 1287 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1288 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1289 | return -EINVAL; |
| 1290 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1291 | /* |
| 1292 | * Grab references on all the modules we'll need, so the subsystems |
| 1293 | * don't dance around before rebind_subsystems attaches them. This may |
| 1294 | * take duplicate reference counts on a subsystem that's already used, |
| 1295 | * but rebind_subsystems handles this case. |
| 1296 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1297 | for_each_subsys(ss, i) { |
| 1298 | if (!(opts->subsys_mask & (1UL << i))) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1299 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1300 | if (!try_module_get(cgroup_subsys[i]->module)) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1301 | module_pin_failed = true; |
| 1302 | break; |
| 1303 | } |
| 1304 | } |
| 1305 | if (module_pin_failed) { |
| 1306 | /* |
| 1307 | * oops, one of the modules was going away. this means that we |
| 1308 | * raced with a module_delete call, and to the user this is |
| 1309 | * essentially a "subsystem doesn't exist" case. |
| 1310 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1311 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | /* drop refcounts only on the ones we took */ |
| 1313 | unsigned long bit = 1UL << i; |
| 1314 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1315 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1316 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1317 | module_put(cgroup_subsys[i]->module); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1318 | } |
| 1319 | return -ENOENT; |
| 1320 | } |
| 1321 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1325 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1326 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1327 | struct cgroup_subsys *ss; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1328 | int i; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1329 | |
Tejun Heo | eb178d0 | 2013-06-25 18:05:21 -0700 | [diff] [blame] | 1330 | mutex_lock(&cgroup_mutex); |
| 1331 | for_each_subsys(ss, i) |
| 1332 | if (subsys_mask & (1UL << i)) |
| 1333 | module_put(cgroup_subsys[i]->module); |
| 1334 | mutex_unlock(&cgroup_mutex); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1337 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1338 | { |
| 1339 | int ret = 0; |
| 1340 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1341 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1342 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1343 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1345 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1346 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1347 | return -EINVAL; |
| 1348 | } |
| 1349 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1350 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1351 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1352 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1353 | |
| 1354 | /* See what subsystems are wanted */ |
| 1355 | ret = parse_cgroupfs_options(data, &opts); |
| 1356 | if (ret) |
| 1357 | goto out_unlock; |
| 1358 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1359 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1360 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1361 | task_tgid_nr(current), current->comm); |
| 1362 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1363 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1364 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1365 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1366 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1367 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1368 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1369 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1370 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1371 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1372 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1373 | goto out_unlock; |
| 1374 | } |
| 1375 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1376 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1377 | if (ret) |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1378 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1379 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1380 | if (opts.release_agent) |
| 1381 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1382 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1383 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1384 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1385 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1386 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1387 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | e2bd416 | 2013-06-27 19:37:23 -0700 | [diff] [blame] | 1388 | if (ret) |
| 1389 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1390 | return ret; |
| 1391 | } |
| 1392 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1393 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1394 | .statfs = simple_statfs, |
| 1395 | .drop_inode = generic_delete_inode, |
| 1396 | .show_options = cgroup_show_options, |
| 1397 | .remount_fs = cgroup_remount, |
| 1398 | }; |
| 1399 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1400 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1401 | { |
| 1402 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1403 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1404 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1405 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1406 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1407 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1408 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1409 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1410 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1411 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1412 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1413 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1414 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1415 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1416 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1417 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1418 | INIT_LIST_HEAD(&root->subsys_list); |
| 1419 | INIT_LIST_HEAD(&root->root_list); |
| 1420 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1421 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1422 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1423 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1426 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1427 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1428 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1429 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1430 | lockdep_assert_held(&cgroup_mutex); |
| 1431 | lockdep_assert_held(&cgroup_root_mutex); |
| 1432 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1433 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1434 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1435 | if (id < 0) |
| 1436 | return id; |
| 1437 | |
| 1438 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1443 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1444 | lockdep_assert_held(&cgroup_mutex); |
| 1445 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1446 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1447 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1448 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1449 | root->hierarchy_id = 0; |
| 1450 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1453 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1454 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1455 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1456 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1457 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1458 | /* If we asked for a name then it must match */ |
| 1459 | if (opts->name && strcmp(opts->name, root->name)) |
| 1460 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1461 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1462 | /* |
| 1463 | * If we asked for subsystems (or explicitly for no |
| 1464 | * subsystems) then they must match |
| 1465 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1466 | if ((opts->subsys_mask || opts->none) |
| 1467 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1468 | return 0; |
| 1469 | |
| 1470 | return 1; |
| 1471 | } |
| 1472 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1473 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1474 | { |
| 1475 | struct cgroupfs_root *root; |
| 1476 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1477 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1478 | return NULL; |
| 1479 | |
| 1480 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1481 | if (!root) |
| 1482 | return ERR_PTR(-ENOMEM); |
| 1483 | |
| 1484 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1485 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1486 | /* |
| 1487 | * We need to set @root->subsys_mask now so that @root can be |
| 1488 | * matched by cgroup_test_super() before it finishes |
| 1489 | * initialization; otherwise, competing mounts with the same |
| 1490 | * options may try to bind the same subsystems instead of waiting |
| 1491 | * for the first one leading to unexpected mount errors. |
| 1492 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1493 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1494 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1495 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1496 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1497 | if (opts->release_agent) |
| 1498 | strcpy(root->release_agent_path, opts->release_agent); |
| 1499 | if (opts->name) |
| 1500 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1501 | if (opts->cpuset_clone_children) |
| 1502 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1503 | return root; |
| 1504 | } |
| 1505 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1506 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1507 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1508 | if (root) { |
| 1509 | /* hierarhcy ID shoulid already have been released */ |
| 1510 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1511 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1512 | ida_destroy(&root->cgroup_ida); |
| 1513 | kfree(root); |
| 1514 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1517 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1518 | { |
| 1519 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1520 | struct cgroup_sb_opts *opts = data; |
| 1521 | |
| 1522 | /* If we don't have a new root, we can't set up a new sb */ |
| 1523 | if (!opts->new_root) |
| 1524 | return -EINVAL; |
| 1525 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1526 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1527 | |
| 1528 | ret = set_anon_super(sb, NULL); |
| 1529 | if (ret) |
| 1530 | return ret; |
| 1531 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1532 | sb->s_fs_info = opts->new_root; |
| 1533 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1534 | |
| 1535 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1536 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1537 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1538 | sb->s_op = &cgroup_ops; |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1544 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1545 | static const struct dentry_operations cgroup_dops = { |
| 1546 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1547 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1548 | }; |
| 1549 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | struct inode *inode = |
| 1551 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1552 | |
| 1553 | if (!inode) |
| 1554 | return -ENOMEM; |
| 1555 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1556 | inode->i_fop = &simple_dir_operations; |
| 1557 | inode->i_op = &cgroup_dir_inode_operations; |
| 1558 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1559 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1560 | sb->s_root = d_make_root(inode); |
| 1561 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1562 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1563 | /* for everything else we want ->d_op set */ |
| 1564 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1565 | return 0; |
| 1566 | } |
| 1567 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1568 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1569 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1570 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1571 | { |
| 1572 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1573 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1574 | int ret = 0; |
| 1575 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1576 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1577 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1578 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1579 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | |
| 1581 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1582 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1583 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1584 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1585 | if (ret) |
| 1586 | goto out_err; |
| 1587 | |
| 1588 | /* |
| 1589 | * Allocate a new cgroup root. We may not need it if we're |
| 1590 | * reusing an existing hierarchy. |
| 1591 | */ |
| 1592 | new_root = cgroup_root_from_opts(&opts); |
| 1593 | if (IS_ERR(new_root)) { |
| 1594 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1595 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1596 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1598 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1599 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1600 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1602 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1603 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1604 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1607 | root = sb->s_fs_info; |
| 1608 | BUG_ON(!root); |
| 1609 | if (root == opts.new_root) { |
| 1610 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1611 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1612 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1613 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1614 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1615 | |
| 1616 | BUG_ON(sb->s_root != NULL); |
| 1617 | |
| 1618 | ret = cgroup_get_rootdir(sb); |
| 1619 | if (ret) |
| 1620 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1621 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1622 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1623 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1624 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1625 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1626 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1627 | /* Check for name clashes with existing mounts */ |
| 1628 | ret = -EBUSY; |
| 1629 | if (strlen(root->name)) |
| 1630 | for_each_active_root(existing_root) |
| 1631 | if (!strcmp(existing_root->name, root->name)) |
| 1632 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1633 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1634 | /* |
| 1635 | * We're accessing css_set_count without locking |
| 1636 | * css_set_lock here, but that's OK - it can only be |
| 1637 | * increased by someone holding cgroup_lock, and |
| 1638 | * that's us. The worst that can happen is that we |
| 1639 | * have some link structures left over |
| 1640 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1641 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1642 | if (ret) |
| 1643 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1644 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1645 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1646 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1647 | if (ret) |
| 1648 | goto unlock_drop; |
| 1649 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1650 | sb->s_root->d_fsdata = root_cgrp; |
| 1651 | root_cgrp->dentry = sb->s_root; |
| 1652 | |
| 1653 | /* |
| 1654 | * We're inside get_sb() and will call lookup_one_len() to |
| 1655 | * create the root files, which doesn't work if SELinux is |
| 1656 | * in use. The following cred dancing somehow works around |
| 1657 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1658 | * populating new cgroupfs mount") for more details. |
| 1659 | */ |
| 1660 | cred = override_creds(&init_cred); |
| 1661 | |
| 1662 | ret = cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true); |
| 1663 | if (ret) |
| 1664 | goto rm_base_files; |
| 1665 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1666 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1667 | if (ret) |
| 1668 | goto rm_base_files; |
| 1669 | |
| 1670 | revert_creds(cred); |
| 1671 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1672 | /* |
| 1673 | * There must be no failure case after here, since rebinding |
| 1674 | * takes care of subsystems' refcounts, which are explicitly |
| 1675 | * dropped in the failure exit path. |
| 1676 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1677 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1678 | list_add(&root->root_list, &cgroup_roots); |
| 1679 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1680 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1681 | /* Link the top cgroup in this hierarchy into all |
| 1682 | * the css_set objects */ |
| 1683 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1684 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1685 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1686 | write_unlock(&css_set_lock); |
| 1687 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1688 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1689 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1690 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1691 | BUG_ON(root->number_of_cgroups != 1); |
| 1692 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1693 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1694 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1695 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1696 | } else { |
| 1697 | /* |
| 1698 | * We re-used an existing hierarchy - the new root (if |
| 1699 | * any) is not needed |
| 1700 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1701 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1702 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1703 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1704 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1705 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1706 | ret = -EINVAL; |
| 1707 | goto drop_new_super; |
| 1708 | } else { |
| 1709 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1710 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1713 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1714 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1717 | kfree(opts.release_agent); |
| 1718 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1719 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1720 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1721 | rm_base_files: |
| 1722 | free_cgrp_cset_links(&tmp_links); |
| 1723 | cgroup_addrm_files(&root->top_cgroup, NULL, cgroup_base_files, false); |
| 1724 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1725 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1726 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1727 | mutex_unlock(&cgroup_root_mutex); |
| 1728 | mutex_unlock(&cgroup_mutex); |
| 1729 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1730 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1731 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1732 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1733 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1734 | out_err: |
| 1735 | kfree(opts.release_agent); |
| 1736 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1737 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1741 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1742 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1743 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1744 | int ret; |
| 1745 | |
| 1746 | BUG_ON(!root); |
| 1747 | |
| 1748 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1749 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1750 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1751 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1752 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1753 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1754 | |
| 1755 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1756 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1757 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1758 | /* Shouldn't be able to fail ... */ |
| 1759 | BUG_ON(ret); |
| 1760 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1762 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1763 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1764 | * root cgroup |
| 1765 | */ |
| 1766 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1767 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1768 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1769 | list_del(&link->cset_link); |
| 1770 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1771 | kfree(link); |
| 1772 | } |
| 1773 | write_unlock(&css_set_lock); |
| 1774 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1775 | if (!list_empty(&root->root_list)) { |
| 1776 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1777 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1778 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1779 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1780 | cgroup_exit_root_id(root); |
| 1781 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1782 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame^] | 1784 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1786 | simple_xattrs_free(&cgrp->xattrs); |
| 1787 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1788 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1789 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static struct file_system_type cgroup_fs_type = { |
| 1793 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1794 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1795 | .kill_sb = cgroup_kill_sb, |
| 1796 | }; |
| 1797 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1798 | static struct kobject *cgroup_kobj; |
| 1799 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1800 | /** |
| 1801 | * cgroup_path - generate the path of a cgroup |
| 1802 | * @cgrp: the cgroup in question |
| 1803 | * @buf: the buffer to write the path into |
| 1804 | * @buflen: the length of the buffer |
| 1805 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1806 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1807 | * |
| 1808 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1809 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1810 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1811 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1813 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1814 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1815 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1816 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1817 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1818 | if (!cgrp->parent) { |
| 1819 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1820 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1821 | return 0; |
| 1822 | } |
| 1823 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1824 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1825 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1826 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1827 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1828 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1829 | const char *name = cgroup_name(cgrp); |
| 1830 | int len; |
| 1831 | |
| 1832 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1833 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1834 | goto out; |
| 1835 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1836 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1837 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1838 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1839 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1840 | |
| 1841 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1842 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1843 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1844 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1845 | out: |
| 1846 | rcu_read_unlock(); |
| 1847 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1848 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1849 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1850 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1851 | /** |
| 1852 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1853 | * @task: target task |
| 1854 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1855 | * @buf: the buffer to write the path into |
| 1856 | * @buflen: the length of the buffer |
| 1857 | * |
| 1858 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1859 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1860 | * be used inside locks used by cgroup controller callbacks. |
| 1861 | */ |
| 1862 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1863 | char *buf, size_t buflen) |
| 1864 | { |
| 1865 | struct cgroupfs_root *root; |
| 1866 | struct cgroup *cgrp = NULL; |
| 1867 | int ret = -ENOENT; |
| 1868 | |
| 1869 | mutex_lock(&cgroup_mutex); |
| 1870 | |
| 1871 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1872 | if (root) { |
| 1873 | cgrp = task_cgroup_from_root(task, root); |
| 1874 | ret = cgroup_path(cgrp, buf, buflen); |
| 1875 | } |
| 1876 | |
| 1877 | mutex_unlock(&cgroup_mutex); |
| 1878 | |
| 1879 | return ret; |
| 1880 | } |
| 1881 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1882 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1883 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1884 | * Control Group taskset |
| 1885 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1886 | struct task_and_cgroup { |
| 1887 | struct task_struct *task; |
| 1888 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1889 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1890 | }; |
| 1891 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1892 | struct cgroup_taskset { |
| 1893 | struct task_and_cgroup single; |
| 1894 | struct flex_array *tc_array; |
| 1895 | int tc_array_len; |
| 1896 | int idx; |
| 1897 | struct cgroup *cur_cgrp; |
| 1898 | }; |
| 1899 | |
| 1900 | /** |
| 1901 | * cgroup_taskset_first - reset taskset and return the first task |
| 1902 | * @tset: taskset of interest |
| 1903 | * |
| 1904 | * @tset iteration is initialized and the first task is returned. |
| 1905 | */ |
| 1906 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1907 | { |
| 1908 | if (tset->tc_array) { |
| 1909 | tset->idx = 0; |
| 1910 | return cgroup_taskset_next(tset); |
| 1911 | } else { |
| 1912 | tset->cur_cgrp = tset->single.cgrp; |
| 1913 | return tset->single.task; |
| 1914 | } |
| 1915 | } |
| 1916 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1917 | |
| 1918 | /** |
| 1919 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1920 | * @tset: taskset of interest |
| 1921 | * |
| 1922 | * Return the next task in @tset. Iteration must have been initialized |
| 1923 | * with cgroup_taskset_first(). |
| 1924 | */ |
| 1925 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1926 | { |
| 1927 | struct task_and_cgroup *tc; |
| 1928 | |
| 1929 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1930 | return NULL; |
| 1931 | |
| 1932 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1933 | tset->cur_cgrp = tc->cgrp; |
| 1934 | return tc->task; |
| 1935 | } |
| 1936 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1937 | |
| 1938 | /** |
| 1939 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1940 | * @tset: taskset of interest |
| 1941 | * |
| 1942 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1943 | * function must be preceded by either cgroup_taskset_first() or |
| 1944 | * cgroup_taskset_next(). |
| 1945 | */ |
| 1946 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1947 | { |
| 1948 | return tset->cur_cgrp; |
| 1949 | } |
| 1950 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1951 | |
| 1952 | /** |
| 1953 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1954 | * @tset: taskset of interest |
| 1955 | */ |
| 1956 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1957 | { |
| 1958 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1959 | } |
| 1960 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1961 | |
| 1962 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | /* |
| 1964 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1965 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1966 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1967 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1968 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1969 | struct task_struct *tsk, |
| 1970 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1971 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1972 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1973 | |
| 1974 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1975 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1976 | * setting such that we can't race against cgroup_exit() changing the |
| 1977 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1978 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1979 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1980 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1981 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1982 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1983 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 | task_unlock(tsk); |
| 1985 | |
| 1986 | /* Update the css_set linked lists if we're using them */ |
| 1987 | write_lock(&css_set_lock); |
| 1988 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1989 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1990 | write_unlock(&css_set_lock); |
| 1991 | |
| 1992 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1993 | * We just gained a reference on old_cset by taking it from the |
| 1994 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1995 | * 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] | 1996 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1997 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1998 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2001 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2002 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2003 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2004 | * @tsk: the task or the leader of the threadgroup to be attached |
| 2005 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2007 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2008 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2009 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2010 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 2011 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | { |
| 2013 | int retval, i, group_size; |
| 2014 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2015 | struct cgroupfs_root *root = cgrp->root; |
| 2016 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2017 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2018 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2019 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2020 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2021 | |
| 2022 | /* |
| 2023 | * step 0: in order to do expensive, possibly blocking operations for |
| 2024 | * every thread, we cannot iterate the thread group list, since it needs |
| 2025 | * rcu or tasklist locked. instead, build an array of all threads in the |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2026 | * group - group_rwsem prevents new threads from appearing, and if |
| 2027 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2028 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2029 | if (threadgroup) |
| 2030 | group_size = get_nr_threads(tsk); |
| 2031 | else |
| 2032 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2033 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2034 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 | if (!group) |
| 2036 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2037 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2038 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2039 | if (retval) |
| 2040 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2041 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2043 | /* |
| 2044 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2045 | * already PF_EXITING could be freed from underneath us unless we |
| 2046 | * take an rcu_read_lock. |
| 2047 | */ |
| 2048 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2050 | struct task_and_cgroup ent; |
| 2051 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2052 | /* @tsk either already exited or can't exit until the end */ |
| 2053 | if (tsk->flags & PF_EXITING) |
| 2054 | continue; |
| 2055 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2056 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2057 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2058 | ent.task = tsk; |
| 2059 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2060 | /* nothing to do if this task is already in the cgroup */ |
| 2061 | if (ent.cgrp == cgrp) |
| 2062 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2063 | /* |
| 2064 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2065 | * earlier, but it's good form to communicate our expectations. |
| 2066 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2067 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2068 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2069 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2070 | |
| 2071 | if (!threadgroup) |
| 2072 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2073 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2074 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | /* remember the number of threads in the array for later. */ |
| 2076 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2077 | tset.tc_array = group; |
| 2078 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2080 | /* methods shouldn't be called if no task is actually migrating */ |
| 2081 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2082 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2083 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2084 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | /* |
| 2086 | * step 1: check that we can legitimately attach to the cgroup. |
| 2087 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2088 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2089 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2090 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2091 | if (retval) { |
| 2092 | failed_ss = ss; |
| 2093 | goto out_cancel_attach; |
| 2094 | } |
| 2095 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2100 | * we use find_css_set, which allocates a new one if necessary. |
| 2101 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2102 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2103 | struct css_set *old_cset; |
| 2104 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2105 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2106 | old_cset = task_css_set(tc->task); |
| 2107 | tc->cg = find_css_set(old_cset, cgrp); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2108 | if (!tc->cg) { |
| 2109 | retval = -ENOMEM; |
| 2110 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2115 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2116 | * proceed to move all tasks to the new cgroup. There are no |
| 2117 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2118 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2119 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2120 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2121 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | } |
| 2123 | /* nothing is sensitive to fork() after this point. */ |
| 2124 | |
| 2125 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2126 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2127 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2128 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2129 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2130 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /* |
| 2134 | * step 5: success! and cleanup |
| 2135 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2136 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2137 | out_put_css_set_refs: |
| 2138 | if (retval) { |
| 2139 | for (i = 0; i < group_size; i++) { |
| 2140 | tc = flex_array_get(group, i); |
| 2141 | if (!tc->cg) |
| 2142 | break; |
| 2143 | put_css_set(tc->cg); |
| 2144 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2145 | } |
| 2146 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2147 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2148 | for_each_root_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2149 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2150 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2151 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2152 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2153 | } |
| 2154 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2156 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2157 | return retval; |
| 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * 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] | 2162 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2163 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2164 | */ |
| 2165 | 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] | 2166 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2167 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2168 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2169 | int ret; |
| 2170 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2171 | if (!cgroup_lock_live_group(cgrp)) |
| 2172 | return -ENODEV; |
| 2173 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2174 | retry_find_task: |
| 2175 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2176 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2177 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2178 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2179 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2180 | ret= -ESRCH; |
| 2181 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2182 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2183 | /* |
| 2184 | * even if we're attaching all tasks in the thread group, we |
| 2185 | * only need to check permissions on one of them. |
| 2186 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2187 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2188 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2189 | !uid_eq(cred->euid, tcred->uid) && |
| 2190 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2191 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2192 | ret = -EACCES; |
| 2193 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2194 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2195 | } else |
| 2196 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2197 | |
| 2198 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2199 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2200 | |
| 2201 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2202 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2203 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2204 | * with no rt_runtime allocated. Just say no. |
| 2205 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2206 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2207 | ret = -EINVAL; |
| 2208 | rcu_read_unlock(); |
| 2209 | goto out_unlock_cgroup; |
| 2210 | } |
| 2211 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2212 | get_task_struct(tsk); |
| 2213 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2214 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2215 | threadgroup_lock(tsk); |
| 2216 | if (threadgroup) { |
| 2217 | if (!thread_group_leader(tsk)) { |
| 2218 | /* |
| 2219 | * a race with de_thread from another thread's exec() |
| 2220 | * may strip us of our leadership, if this happens, |
| 2221 | * there is no choice but to throw this task away and |
| 2222 | * try again; this is |
| 2223 | * "double-double-toil-and-trouble-check locking". |
| 2224 | */ |
| 2225 | threadgroup_unlock(tsk); |
| 2226 | put_task_struct(tsk); |
| 2227 | goto retry_find_task; |
| 2228 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2232 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2233 | threadgroup_unlock(tsk); |
| 2234 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2235 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2236 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2237 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2238 | return ret; |
| 2239 | } |
| 2240 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2241 | /** |
| 2242 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2243 | * @from: attach to all cgroups of a given task |
| 2244 | * @tsk: the task to be attached |
| 2245 | */ |
| 2246 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2247 | { |
| 2248 | struct cgroupfs_root *root; |
| 2249 | int retval = 0; |
| 2250 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2251 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2252 | for_each_active_root(root) { |
| 2253 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2254 | |
| 2255 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2256 | if (retval) |
| 2257 | break; |
| 2258 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2259 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2260 | |
| 2261 | return retval; |
| 2262 | } |
| 2263 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2264 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2265 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2266 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2267 | return attach_task_by_pid(cgrp, pid, false); |
| 2268 | } |
| 2269 | |
| 2270 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2271 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2272 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2275 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2276 | const char *buffer) |
| 2277 | { |
| 2278 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2279 | if (strlen(buffer) >= PATH_MAX) |
| 2280 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2281 | if (!cgroup_lock_live_group(cgrp)) |
| 2282 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2283 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2284 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2285 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2286 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2287 | return 0; |
| 2288 | } |
| 2289 | |
| 2290 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2291 | struct seq_file *seq) |
| 2292 | { |
| 2293 | if (!cgroup_lock_live_group(cgrp)) |
| 2294 | return -ENODEV; |
| 2295 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2296 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2297 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2298 | return 0; |
| 2299 | } |
| 2300 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2301 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2302 | struct seq_file *seq) |
| 2303 | { |
| 2304 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2305 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2308 | /* A buffer size big enough for numbers or short strings */ |
| 2309 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2310 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2311 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2312 | struct file *file, |
| 2313 | const char __user *userbuf, |
| 2314 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2315 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2316 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2317 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2318 | char *end; |
| 2319 | |
| 2320 | if (!nbytes) |
| 2321 | return -EINVAL; |
| 2322 | if (nbytes >= sizeof(buffer)) |
| 2323 | return -E2BIG; |
| 2324 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2325 | return -EFAULT; |
| 2326 | |
| 2327 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2328 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2329 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2330 | if (*end) |
| 2331 | return -EINVAL; |
| 2332 | retval = cft->write_u64(cgrp, cft, val); |
| 2333 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2334 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2335 | if (*end) |
| 2336 | return -EINVAL; |
| 2337 | retval = cft->write_s64(cgrp, cft, val); |
| 2338 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2339 | if (!retval) |
| 2340 | retval = nbytes; |
| 2341 | return retval; |
| 2342 | } |
| 2343 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2344 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2345 | struct file *file, |
| 2346 | const char __user *userbuf, |
| 2347 | size_t nbytes, loff_t *unused_ppos) |
| 2348 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2349 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2350 | int retval = 0; |
| 2351 | size_t max_bytes = cft->max_write_len; |
| 2352 | char *buffer = local_buffer; |
| 2353 | |
| 2354 | if (!max_bytes) |
| 2355 | max_bytes = sizeof(local_buffer) - 1; |
| 2356 | if (nbytes >= max_bytes) |
| 2357 | return -E2BIG; |
| 2358 | /* Allocate a dynamic buffer if we need one */ |
| 2359 | if (nbytes >= sizeof(local_buffer)) { |
| 2360 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2361 | if (buffer == NULL) |
| 2362 | return -ENOMEM; |
| 2363 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2364 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2365 | retval = -EFAULT; |
| 2366 | goto out; |
| 2367 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2368 | |
| 2369 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2370 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2371 | if (!retval) |
| 2372 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2373 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2374 | if (buffer != local_buffer) |
| 2375 | kfree(buffer); |
| 2376 | return retval; |
| 2377 | } |
| 2378 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2379 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2380 | size_t nbytes, loff_t *ppos) |
| 2381 | { |
| 2382 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2383 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2384 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2385 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2386 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2387 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2388 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2389 | if (cft->write_u64 || cft->write_s64) |
| 2390 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2391 | if (cft->write_string) |
| 2392 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2393 | if (cft->trigger) { |
| 2394 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2395 | return ret ? ret : nbytes; |
| 2396 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2397 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2400 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2401 | struct file *file, |
| 2402 | char __user *buf, size_t nbytes, |
| 2403 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2404 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2405 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2406 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2407 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2408 | |
| 2409 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2410 | } |
| 2411 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2412 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2413 | struct file *file, |
| 2414 | char __user *buf, size_t nbytes, |
| 2415 | loff_t *ppos) |
| 2416 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2417 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2418 | s64 val = cft->read_s64(cgrp, cft); |
| 2419 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2420 | |
| 2421 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2422 | } |
| 2423 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2424 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2425 | size_t nbytes, loff_t *ppos) |
| 2426 | { |
| 2427 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2428 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2429 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2430 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2431 | return -ENODEV; |
| 2432 | |
| 2433 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2434 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2435 | if (cft->read_u64) |
| 2436 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2437 | if (cft->read_s64) |
| 2438 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2439 | return -EINVAL; |
| 2440 | } |
| 2441 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * seqfile ops/methods for returning structured data. Currently just |
| 2444 | * supports string->u64 maps, but can be extended in future. |
| 2445 | */ |
| 2446 | |
| 2447 | struct cgroup_seqfile_state { |
| 2448 | struct cftype *cft; |
| 2449 | struct cgroup *cgroup; |
| 2450 | }; |
| 2451 | |
| 2452 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2453 | { |
| 2454 | struct seq_file *sf = cb->state; |
| 2455 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2456 | } |
| 2457 | |
| 2458 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2459 | { |
| 2460 | struct cgroup_seqfile_state *state = m->private; |
| 2461 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2462 | if (cft->read_map) { |
| 2463 | struct cgroup_map_cb cb = { |
| 2464 | .fill = cgroup_map_add, |
| 2465 | .state = m, |
| 2466 | }; |
| 2467 | return cft->read_map(state->cgroup, cft, &cb); |
| 2468 | } |
| 2469 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2472 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2473 | { |
| 2474 | struct seq_file *seq = file->private_data; |
| 2475 | kfree(seq->private); |
| 2476 | return single_release(inode, file); |
| 2477 | } |
| 2478 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2479 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2480 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2481 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2482 | .llseek = seq_lseek, |
| 2483 | .release = cgroup_seqfile_release, |
| 2484 | }; |
| 2485 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2486 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2487 | { |
| 2488 | int err; |
| 2489 | struct cftype *cft; |
| 2490 | |
| 2491 | err = generic_file_open(inode, file); |
| 2492 | if (err) |
| 2493 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2494 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2495 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2496 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2497 | struct cgroup_seqfile_state *state; |
| 2498 | |
| 2499 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2500 | if (!state) |
| 2501 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2502 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2503 | state->cft = cft; |
| 2504 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2505 | file->f_op = &cgroup_seqfile_operations; |
| 2506 | err = single_open(file, cgroup_seqfile_show, state); |
| 2507 | if (err < 0) |
| 2508 | kfree(state); |
| 2509 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2510 | err = cft->open(inode, file); |
| 2511 | else |
| 2512 | err = 0; |
| 2513 | |
| 2514 | return err; |
| 2515 | } |
| 2516 | |
| 2517 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2518 | { |
| 2519 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2520 | if (cft->release) |
| 2521 | return cft->release(inode, file); |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2527 | */ |
| 2528 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2529 | struct inode *new_dir, struct dentry *new_dentry) |
| 2530 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2531 | int ret; |
| 2532 | struct cgroup_name *name, *old_name; |
| 2533 | struct cgroup *cgrp; |
| 2534 | |
| 2535 | /* |
| 2536 | * It's convinient to use parent dir's i_mutex to protected |
| 2537 | * cgrp->name. |
| 2538 | */ |
| 2539 | lockdep_assert_held(&old_dir->i_mutex); |
| 2540 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2541 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2542 | return -ENOTDIR; |
| 2543 | if (new_dentry->d_inode) |
| 2544 | return -EEXIST; |
| 2545 | if (old_dir != new_dir) |
| 2546 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2547 | |
| 2548 | cgrp = __d_cgrp(old_dentry); |
| 2549 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2550 | /* |
| 2551 | * This isn't a proper migration and its usefulness is very |
| 2552 | * limited. Disallow if sane_behavior. |
| 2553 | */ |
| 2554 | if (cgroup_sane_behavior(cgrp)) |
| 2555 | return -EPERM; |
| 2556 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2557 | name = cgroup_alloc_name(new_dentry); |
| 2558 | if (!name) |
| 2559 | return -ENOMEM; |
| 2560 | |
| 2561 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2562 | if (ret) { |
| 2563 | kfree(name); |
| 2564 | return ret; |
| 2565 | } |
| 2566 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2567 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2568 | rcu_assign_pointer(cgrp->name, name); |
| 2569 | |
| 2570 | kfree_rcu(old_name, rcu_head); |
| 2571 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2574 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2575 | { |
| 2576 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2577 | return &__d_cgrp(dentry)->xattrs; |
| 2578 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2579 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | static inline int xattr_enabled(struct dentry *dentry) |
| 2583 | { |
| 2584 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2585 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | static bool is_valid_xattr(const char *name) |
| 2589 | { |
| 2590 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2591 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2592 | return true; |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
| 2596 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2597 | const void *val, size_t size, int flags) |
| 2598 | { |
| 2599 | if (!xattr_enabled(dentry)) |
| 2600 | return -EOPNOTSUPP; |
| 2601 | if (!is_valid_xattr(name)) |
| 2602 | return -EINVAL; |
| 2603 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2604 | } |
| 2605 | |
| 2606 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2607 | { |
| 2608 | if (!xattr_enabled(dentry)) |
| 2609 | return -EOPNOTSUPP; |
| 2610 | if (!is_valid_xattr(name)) |
| 2611 | return -EINVAL; |
| 2612 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2613 | } |
| 2614 | |
| 2615 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2616 | void *buf, size_t size) |
| 2617 | { |
| 2618 | if (!xattr_enabled(dentry)) |
| 2619 | return -EOPNOTSUPP; |
| 2620 | if (!is_valid_xattr(name)) |
| 2621 | return -EINVAL; |
| 2622 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2623 | } |
| 2624 | |
| 2625 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2626 | { |
| 2627 | if (!xattr_enabled(dentry)) |
| 2628 | return -EOPNOTSUPP; |
| 2629 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2630 | } |
| 2631 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2632 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2633 | .read = cgroup_file_read, |
| 2634 | .write = cgroup_file_write, |
| 2635 | .llseek = generic_file_llseek, |
| 2636 | .open = cgroup_file_open, |
| 2637 | .release = cgroup_file_release, |
| 2638 | }; |
| 2639 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2640 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2641 | .setxattr = cgroup_setxattr, |
| 2642 | .getxattr = cgroup_getxattr, |
| 2643 | .listxattr = cgroup_listxattr, |
| 2644 | .removexattr = cgroup_removexattr, |
| 2645 | }; |
| 2646 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2647 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2648 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2649 | .mkdir = cgroup_mkdir, |
| 2650 | .rmdir = cgroup_rmdir, |
| 2651 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2652 | .setxattr = cgroup_setxattr, |
| 2653 | .getxattr = cgroup_getxattr, |
| 2654 | .listxattr = cgroup_listxattr, |
| 2655 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2656 | }; |
| 2657 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2658 | static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2659 | { |
| 2660 | if (dentry->d_name.len > NAME_MAX) |
| 2661 | return ERR_PTR(-ENAMETOOLONG); |
| 2662 | d_add(dentry, NULL); |
| 2663 | return NULL; |
| 2664 | } |
| 2665 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2666 | /* |
| 2667 | * Check if a file is a control file |
| 2668 | */ |
| 2669 | static inline struct cftype *__file_cft(struct file *file) |
| 2670 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2671 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2672 | return ERR_PTR(-EINVAL); |
| 2673 | return __d_cft(file->f_dentry); |
| 2674 | } |
| 2675 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2676 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2677 | struct super_block *sb) |
| 2678 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2679 | struct inode *inode; |
| 2680 | |
| 2681 | if (!dentry) |
| 2682 | return -ENOENT; |
| 2683 | if (dentry->d_inode) |
| 2684 | return -EEXIST; |
| 2685 | |
| 2686 | inode = cgroup_new_inode(mode, sb); |
| 2687 | if (!inode) |
| 2688 | return -ENOMEM; |
| 2689 | |
| 2690 | if (S_ISDIR(mode)) { |
| 2691 | inode->i_op = &cgroup_dir_inode_operations; |
| 2692 | inode->i_fop = &simple_dir_operations; |
| 2693 | |
| 2694 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2695 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2696 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2697 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2698 | /* |
| 2699 | * Control reaches here with cgroup_mutex held. |
| 2700 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2701 | * want to populate it immediately without releasing |
| 2702 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2703 | * yet, trylock will always succeed without affecting |
| 2704 | * lockdep checks. |
| 2705 | */ |
| 2706 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2707 | } else if (S_ISREG(mode)) { |
| 2708 | inode->i_size = 0; |
| 2709 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2710 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2711 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2712 | d_instantiate(dentry, inode); |
| 2713 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2714 | return 0; |
| 2715 | } |
| 2716 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2717 | /** |
| 2718 | * cgroup_file_mode - deduce file mode of a control file |
| 2719 | * @cft: the control file in question |
| 2720 | * |
| 2721 | * returns cft->mode if ->mode is not 0 |
| 2722 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2723 | * returns S_IRUGO if it has only a read handler |
| 2724 | * returns S_IWUSR if it has only a write hander |
| 2725 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2726 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2727 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2728 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2729 | |
| 2730 | if (cft->mode) |
| 2731 | return cft->mode; |
| 2732 | |
| 2733 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2734 | cft->read_map || cft->read_seq_string) |
| 2735 | mode |= S_IRUGO; |
| 2736 | |
| 2737 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2738 | cft->write_string || cft->trigger) |
| 2739 | mode |= S_IWUSR; |
| 2740 | |
| 2741 | return mode; |
| 2742 | } |
| 2743 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2744 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2745 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2746 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2747 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2748 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2749 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2750 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2751 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2752 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2753 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2754 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2755 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2756 | strcpy(name, subsys->name); |
| 2757 | strcat(name, "."); |
| 2758 | } |
| 2759 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2760 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2761 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2762 | |
| 2763 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2764 | if (!cfe) |
| 2765 | return -ENOMEM; |
| 2766 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2767 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2768 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2769 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2770 | goto out; |
| 2771 | } |
| 2772 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2773 | cfe->type = (void *)cft; |
| 2774 | cfe->dentry = dentry; |
| 2775 | dentry->d_fsdata = cfe; |
| 2776 | simple_xattrs_init(&cfe->xattrs); |
| 2777 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2778 | mode = cgroup_file_mode(cft); |
| 2779 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2780 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2781 | list_add_tail(&cfe->node, &parent->files); |
| 2782 | cfe = NULL; |
| 2783 | } |
| 2784 | dput(dentry); |
| 2785 | out: |
| 2786 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2787 | return error; |
| 2788 | } |
| 2789 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2790 | /** |
| 2791 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2792 | * @cgrp: the target cgroup |
| 2793 | * @subsys: the subsystem of files to be added |
| 2794 | * @cfts: array of cftypes to be added |
| 2795 | * @is_add: whether to add or remove |
| 2796 | * |
| 2797 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
| 2798 | * All @cfts should belong to @subsys. For removals, this function never |
| 2799 | * fails. If addition fails, this function doesn't remove files already |
| 2800 | * added. The caller is responsible for cleaning up. |
| 2801 | */ |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2802 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2803 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2804 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2805 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2806 | int ret; |
| 2807 | |
| 2808 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2809 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2810 | |
| 2811 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2812 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2813 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2814 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2815 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2816 | continue; |
| 2817 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2818 | continue; |
| 2819 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2820 | if (is_add) { |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2821 | ret = cgroup_add_file(cgrp, subsys, cft); |
| 2822 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2823 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2824 | cft->name, ret); |
| 2825 | return ret; |
| 2826 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2827 | } else { |
| 2828 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2829 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2830 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2831 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2834 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2835 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2836 | { |
| 2837 | /* |
| 2838 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2839 | * the existing cgroups under cgroup_mutex and create files. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2840 | * Instead, we use cgroup_for_each_descendant_pre() and drop RCU |
| 2841 | * read lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2842 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2843 | mutex_lock(&cgroup_mutex); |
| 2844 | } |
| 2845 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2846 | static int cgroup_cfts_commit(struct cgroup_subsys *ss, |
| 2847 | struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2848 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2849 | { |
| 2850 | LIST_HEAD(pending); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2851 | struct cgroup *cgrp, *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2852 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | struct dentry *prev = NULL; |
| 2854 | struct inode *inode; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2855 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2856 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2857 | |
| 2858 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 2859 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2860 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2861 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2862 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2865 | /* |
| 2866 | * All cgroups which are created after we drop cgroup_mutex will |
| 2867 | * have the updated set of files, so we only need to update the |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2868 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2869 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2870 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2871 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2872 | mutex_unlock(&cgroup_mutex); |
| 2873 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2874 | /* @root always needs to be updated */ |
| 2875 | inode = root->dentry->d_inode; |
| 2876 | mutex_lock(&inode->i_mutex); |
| 2877 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2878 | ret = cgroup_addrm_files(root, ss, cfts, is_add); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2879 | mutex_unlock(&cgroup_mutex); |
| 2880 | mutex_unlock(&inode->i_mutex); |
| 2881 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2882 | if (ret) |
| 2883 | goto out_deact; |
| 2884 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2885 | /* add/rm files for all cgroups created before */ |
| 2886 | rcu_read_lock(); |
| 2887 | cgroup_for_each_descendant_pre(cgrp, root) { |
| 2888 | if (cgroup_is_dead(cgrp)) |
| 2889 | continue; |
| 2890 | |
| 2891 | inode = cgrp->dentry->d_inode; |
| 2892 | dget(cgrp->dentry); |
| 2893 | rcu_read_unlock(); |
| 2894 | |
| 2895 | dput(prev); |
| 2896 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2897 | |
| 2898 | mutex_lock(&inode->i_mutex); |
| 2899 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2900 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2901 | ret = cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2902 | mutex_unlock(&cgroup_mutex); |
| 2903 | mutex_unlock(&inode->i_mutex); |
| 2904 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2905 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2906 | if (ret) |
| 2907 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2908 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2909 | rcu_read_unlock(); |
| 2910 | dput(prev); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2911 | out_deact: |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2912 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2913 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | /** |
| 2917 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2918 | * @ss: target cgroup subsystem |
| 2919 | * @cfts: zero-length name terminated array of cftypes |
| 2920 | * |
| 2921 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2922 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2923 | * have them too. This function can be called anytime whether @ss is |
| 2924 | * attached or not. |
| 2925 | * |
| 2926 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2927 | * function currently returns 0 as long as @cfts registration is successful |
| 2928 | * even if some file creation attempts on existing cgroups fail. |
| 2929 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2930 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2931 | { |
| 2932 | struct cftype_set *set; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2933 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2934 | |
| 2935 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2936 | if (!set) |
| 2937 | return -ENOMEM; |
| 2938 | |
| 2939 | cgroup_cfts_prepare(); |
| 2940 | set->cfts = cfts; |
| 2941 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2942 | ret = cgroup_cfts_commit(ss, cfts, true); |
| 2943 | if (ret) |
| 2944 | cgroup_rm_cftypes(ss, cfts); |
| 2945 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2946 | } |
| 2947 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2948 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2949 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2950 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2951 | * @ss: target cgroup subsystem |
| 2952 | * @cfts: zero-length name terminated array of cftypes |
| 2953 | * |
| 2954 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2955 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2956 | * won't have them either. This function can be called anytime whether @ss |
| 2957 | * is attached or not. |
| 2958 | * |
| 2959 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2960 | * registered with @ss. |
| 2961 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2962 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2963 | { |
| 2964 | struct cftype_set *set; |
| 2965 | |
| 2966 | cgroup_cfts_prepare(); |
| 2967 | |
| 2968 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2969 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2970 | list_del(&set->node); |
| 2971 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2972 | cgroup_cfts_commit(ss, cfts, false); |
| 2973 | return 0; |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | cgroup_cfts_commit(ss, NULL, false); |
| 2978 | return -ENOENT; |
| 2979 | } |
| 2980 | |
| 2981 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2982 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2983 | * @cgrp: the cgroup in question |
| 2984 | * |
| 2985 | * Return the number of tasks in the cgroup. |
| 2986 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2987 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2988 | { |
| 2989 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2990 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2991 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2992 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2993 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2994 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2995 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2996 | return count; |
| 2997 | } |
| 2998 | |
| 2999 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3000 | * Advance a list_head iterator. The iterator should be positioned at |
| 3001 | * the start of a css_set |
| 3002 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3003 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3004 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3005 | struct list_head *l = it->cset_link; |
| 3006 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3007 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3008 | |
| 3009 | /* Advance to the next non-empty css_set */ |
| 3010 | do { |
| 3011 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3012 | if (l == &cgrp->cset_links) { |
| 3013 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3014 | return; |
| 3015 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3016 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3017 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3018 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3019 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3020 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3023 | /* |
| 3024 | * To reduce the fork() overhead for systems that are not actually |
| 3025 | * using their cgroups capability, we don't maintain the lists running |
| 3026 | * through each css_set to its tasks until we see the list actually |
| 3027 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3028 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 3029 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3030 | { |
| 3031 | struct task_struct *p, *g; |
| 3032 | write_lock(&css_set_lock); |
| 3033 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3034 | /* |
| 3035 | * We need tasklist_lock because RCU is not safe against |
| 3036 | * while_each_thread(). Besides, a forking task that has passed |
| 3037 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 3038 | * is not guaranteed to have its child immediately visible in the |
| 3039 | * tasklist if we walk through it with RCU. |
| 3040 | */ |
| 3041 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3042 | do_each_thread(g, p) { |
| 3043 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 3044 | /* |
| 3045 | * We should check if the process is exiting, otherwise |
| 3046 | * it will race with cgroup_exit() in that the list |
| 3047 | * entry won't be deleted though the process has exited. |
| 3048 | */ |
| 3049 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 3050 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3051 | task_unlock(p); |
| 3052 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3053 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3054 | write_unlock(&css_set_lock); |
| 3055 | } |
| 3056 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3057 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3058 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 3059 | * @pos: the current cgroup |
| 3060 | * |
| 3061 | * This function returns the next sibling of @pos and should be called |
| 3062 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 3063 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 3064 | * state. |
| 3065 | */ |
| 3066 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 3067 | { |
| 3068 | struct cgroup *next; |
| 3069 | |
| 3070 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3071 | |
| 3072 | /* |
| 3073 | * @pos could already have been removed. Once a cgroup is removed, |
| 3074 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3075 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3076 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3077 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3078 | * finished its grace period even if it's already removed, and thus |
| 3079 | * safe to dereference from this RCU critical section. If |
| 3080 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3081 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3082 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3083 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3084 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3085 | if (&next->sibling != &pos->parent->children) |
| 3086 | return next; |
| 3087 | return NULL; |
| 3088 | } |
| 3089 | |
| 3090 | /* |
| 3091 | * Can't dereference the next pointer. Each cgroup is given a |
| 3092 | * monotonically increasing unique serial number and always |
| 3093 | * appended to the sibling list, so the next one can be found by |
| 3094 | * walking the parent's children until we see a cgroup with higher |
| 3095 | * serial number than @pos's. |
| 3096 | * |
| 3097 | * While this path can be slow, it's taken only when either the |
| 3098 | * current cgroup is removed or iteration and removal race. |
| 3099 | */ |
| 3100 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3101 | if (next->serial_nr > pos->serial_nr) |
| 3102 | return next; |
| 3103 | return NULL; |
| 3104 | } |
| 3105 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3106 | |
| 3107 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3108 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3109 | * @pos: the current position (%NULL to initiate traversal) |
| 3110 | * @cgroup: cgroup whose descendants to walk |
| 3111 | * |
| 3112 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3113 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3114 | * |
| 3115 | * While this function requires RCU read locking, it doesn't require the |
| 3116 | * whole traversal to be contained in a single RCU critical section. This |
| 3117 | * function will return the correct next descendant as long as both @pos |
| 3118 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3119 | */ |
| 3120 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3121 | struct cgroup *cgroup) |
| 3122 | { |
| 3123 | struct cgroup *next; |
| 3124 | |
| 3125 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3126 | |
| 3127 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3128 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3129 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3130 | |
| 3131 | /* visit the first child if exists */ |
| 3132 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3133 | if (next) |
| 3134 | return next; |
| 3135 | |
| 3136 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3137 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3138 | next = cgroup_next_sibling(pos); |
| 3139 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3140 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3141 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3142 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3143 | |
| 3144 | return NULL; |
| 3145 | } |
| 3146 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3147 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3148 | /** |
| 3149 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3150 | * @pos: cgroup of interest |
| 3151 | * |
| 3152 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3153 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3154 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3155 | * |
| 3156 | * While this function requires RCU read locking, it doesn't require the |
| 3157 | * whole traversal to be contained in a single RCU critical section. This |
| 3158 | * function will return the correct rightmost descendant as long as @pos is |
| 3159 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3160 | */ |
| 3161 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3162 | { |
| 3163 | struct cgroup *last, *tmp; |
| 3164 | |
| 3165 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3166 | |
| 3167 | do { |
| 3168 | last = pos; |
| 3169 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3170 | pos = NULL; |
| 3171 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3172 | pos = tmp; |
| 3173 | } while (pos); |
| 3174 | |
| 3175 | return last; |
| 3176 | } |
| 3177 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3178 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3179 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3180 | { |
| 3181 | struct cgroup *last; |
| 3182 | |
| 3183 | do { |
| 3184 | last = pos; |
| 3185 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3186 | sibling); |
| 3187 | } while (pos); |
| 3188 | |
| 3189 | return last; |
| 3190 | } |
| 3191 | |
| 3192 | /** |
| 3193 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3194 | * @pos: the current position (%NULL to initiate traversal) |
| 3195 | * @cgroup: cgroup whose descendants to walk |
| 3196 | * |
| 3197 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3198 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3199 | * |
| 3200 | * While this function requires RCU read locking, it doesn't require the |
| 3201 | * whole traversal to be contained in a single RCU critical section. This |
| 3202 | * function will return the correct next descendant as long as both @pos |
| 3203 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3204 | */ |
| 3205 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3206 | struct cgroup *cgroup) |
| 3207 | { |
| 3208 | struct cgroup *next; |
| 3209 | |
| 3210 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3211 | |
| 3212 | /* if first iteration, visit the leftmost descendant */ |
| 3213 | if (!pos) { |
| 3214 | next = cgroup_leftmost_descendant(cgroup); |
| 3215 | return next != cgroup ? next : NULL; |
| 3216 | } |
| 3217 | |
| 3218 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3219 | next = cgroup_next_sibling(pos); |
| 3220 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3221 | return cgroup_leftmost_descendant(next); |
| 3222 | |
| 3223 | /* no sibling left, visit parent */ |
| 3224 | next = pos->parent; |
| 3225 | return next != cgroup ? next : NULL; |
| 3226 | } |
| 3227 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3228 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3229 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3230 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3231 | { |
| 3232 | /* |
| 3233 | * The first time anyone tries to iterate across a cgroup, |
| 3234 | * we need to enable the list linking each css_set to its |
| 3235 | * tasks, and fix up all existing tasks. |
| 3236 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3237 | if (!use_task_css_set_links) |
| 3238 | cgroup_enable_task_cg_lists(); |
| 3239 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3240 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3241 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3242 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3245 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3246 | struct cgroup_iter *it) |
| 3247 | { |
| 3248 | struct task_struct *res; |
| 3249 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3250 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3251 | |
| 3252 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3253 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3254 | return NULL; |
| 3255 | res = list_entry(l, struct task_struct, cg_list); |
| 3256 | /* Advance iterator to find next entry */ |
| 3257 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3258 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3259 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3260 | /* We reached the end of this task list - move on to |
| 3261 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3262 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3263 | } else { |
| 3264 | it->task = l; |
| 3265 | } |
| 3266 | return res; |
| 3267 | } |
| 3268 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3269 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3270 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3271 | { |
| 3272 | read_unlock(&css_set_lock); |
| 3273 | } |
| 3274 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3275 | static inline int started_after_time(struct task_struct *t1, |
| 3276 | struct timespec *time, |
| 3277 | struct task_struct *t2) |
| 3278 | { |
| 3279 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3280 | if (start_diff > 0) { |
| 3281 | return 1; |
| 3282 | } else if (start_diff < 0) { |
| 3283 | return 0; |
| 3284 | } else { |
| 3285 | /* |
| 3286 | * Arbitrarily, if two processes started at the same |
| 3287 | * time, we'll say that the lower pointer value |
| 3288 | * started first. Note that t2 may have exited by now |
| 3289 | * so this may not be a valid pointer any longer, but |
| 3290 | * that's fine - it still serves to distinguish |
| 3291 | * between two tasks started (effectively) simultaneously. |
| 3292 | */ |
| 3293 | return t1 > t2; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /* |
| 3298 | * This function is a callback from heap_insert() and is used to order |
| 3299 | * the heap. |
| 3300 | * In this case we order the heap in descending task start time. |
| 3301 | */ |
| 3302 | static inline int started_after(void *p1, void *p2) |
| 3303 | { |
| 3304 | struct task_struct *t1 = p1; |
| 3305 | struct task_struct *t2 = p2; |
| 3306 | return started_after_time(t1, &t2->start_time, t2); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
| 3310 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3311 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3312 | * |
| 3313 | * Arguments include pointers to callback functions test_task() and |
| 3314 | * process_task(). |
| 3315 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3316 | * and if it returns true, call process_task() for it also. |
| 3317 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3318 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3319 | * but does not lock css_set_lock for the call to process_task(). |
| 3320 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3321 | * creation. |
| 3322 | * It is guaranteed that process_task() will act on every task that |
| 3323 | * is a member of the cgroup for the duration of this call. This |
| 3324 | * function may or may not call process_task() for tasks that exit |
| 3325 | * or move to a different cgroup during the call, or are forked or |
| 3326 | * move into the cgroup during the call. |
| 3327 | * |
| 3328 | * Note that test_task() may be called with locks held, and may in some |
| 3329 | * situations be called multiple times for the same task, so it should |
| 3330 | * be cheap. |
| 3331 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3332 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3333 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3334 | * may cause this function to fail). |
| 3335 | */ |
| 3336 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3337 | { |
| 3338 | int retval, i; |
| 3339 | struct cgroup_iter it; |
| 3340 | struct task_struct *p, *dropped; |
| 3341 | /* Never dereference latest_task, since it's not refcounted */ |
| 3342 | struct task_struct *latest_task = NULL; |
| 3343 | struct ptr_heap tmp_heap; |
| 3344 | struct ptr_heap *heap; |
| 3345 | struct timespec latest_time = { 0, 0 }; |
| 3346 | |
| 3347 | if (scan->heap) { |
| 3348 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3349 | heap = scan->heap; |
| 3350 | heap->gt = &started_after; |
| 3351 | } else { |
| 3352 | /* We need to allocate our own heap memory */ |
| 3353 | heap = &tmp_heap; |
| 3354 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3355 | if (retval) |
| 3356 | /* cannot allocate the heap */ |
| 3357 | return retval; |
| 3358 | } |
| 3359 | |
| 3360 | again: |
| 3361 | /* |
| 3362 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3363 | * to determine which are of interest, and using the scanner's |
| 3364 | * "process_task" callback to process any of them that need an update. |
| 3365 | * Since we don't want to hold any locks during the task updates, |
| 3366 | * gather tasks to be processed in a heap structure. |
| 3367 | * The heap is sorted by descending task start time. |
| 3368 | * If the statically-sized heap fills up, we overflow tasks that |
| 3369 | * started later, and in future iterations only consider tasks that |
| 3370 | * started after the latest task in the previous pass. This |
| 3371 | * guarantees forward progress and that we don't miss any tasks. |
| 3372 | */ |
| 3373 | heap->size = 0; |
| 3374 | cgroup_iter_start(scan->cg, &it); |
| 3375 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3376 | /* |
| 3377 | * Only affect tasks that qualify per the caller's callback, |
| 3378 | * if he provided one |
| 3379 | */ |
| 3380 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3381 | continue; |
| 3382 | /* |
| 3383 | * Only process tasks that started after the last task |
| 3384 | * we processed |
| 3385 | */ |
| 3386 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3387 | continue; |
| 3388 | dropped = heap_insert(heap, p); |
| 3389 | if (dropped == NULL) { |
| 3390 | /* |
| 3391 | * The new task was inserted; the heap wasn't |
| 3392 | * previously full |
| 3393 | */ |
| 3394 | get_task_struct(p); |
| 3395 | } else if (dropped != p) { |
| 3396 | /* |
| 3397 | * The new task was inserted, and pushed out a |
| 3398 | * different task |
| 3399 | */ |
| 3400 | get_task_struct(p); |
| 3401 | put_task_struct(dropped); |
| 3402 | } |
| 3403 | /* |
| 3404 | * Else the new task was newer than anything already in |
| 3405 | * the heap and wasn't inserted |
| 3406 | */ |
| 3407 | } |
| 3408 | cgroup_iter_end(scan->cg, &it); |
| 3409 | |
| 3410 | if (heap->size) { |
| 3411 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3412 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3413 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3414 | latest_time = q->start_time; |
| 3415 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3416 | } |
| 3417 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3418 | scan->process_task(q, scan); |
| 3419 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3420 | } |
| 3421 | /* |
| 3422 | * If we had to process any tasks at all, scan again |
| 3423 | * in case some of them were in the middle of forking |
| 3424 | * children that didn't get processed. |
| 3425 | * Not the most efficient way to do it, but it avoids |
| 3426 | * having to take callback_mutex in the fork path |
| 3427 | */ |
| 3428 | goto again; |
| 3429 | } |
| 3430 | if (heap == &tmp_heap) |
| 3431 | heap_free(&tmp_heap); |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3435 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3436 | struct cgroup_scanner *scan) |
| 3437 | { |
| 3438 | struct cgroup *new_cgroup = scan->data; |
| 3439 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3440 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3441 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3442 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | /** |
| 3446 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3447 | * @to: cgroup to which the tasks will be moved |
| 3448 | * @from: cgroup in which the tasks currently reside |
| 3449 | */ |
| 3450 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3451 | { |
| 3452 | struct cgroup_scanner scan; |
| 3453 | |
| 3454 | scan.cg = from; |
| 3455 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3456 | scan.process_task = cgroup_transfer_one_task; |
| 3457 | scan.heap = NULL; |
| 3458 | scan.data = to; |
| 3459 | |
| 3460 | return cgroup_scan_tasks(&scan); |
| 3461 | } |
| 3462 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3463 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3464 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3465 | * |
| 3466 | * Reading this file can return large amounts of data if a cgroup has |
| 3467 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3468 | * but we cannot guarantee that the information we produce is correct |
| 3469 | * unless we produce it entirely atomically. |
| 3470 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3471 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3472 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3473 | /* which pidlist file are we talking about? */ |
| 3474 | enum cgroup_filetype { |
| 3475 | CGROUP_FILE_PROCS, |
| 3476 | CGROUP_FILE_TASKS, |
| 3477 | }; |
| 3478 | |
| 3479 | /* |
| 3480 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3481 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3482 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3483 | * to the cgroup. |
| 3484 | */ |
| 3485 | struct cgroup_pidlist { |
| 3486 | /* |
| 3487 | * used to find which pidlist is wanted. doesn't change as long as |
| 3488 | * this particular list stays in the list. |
| 3489 | */ |
| 3490 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3491 | /* array of xids */ |
| 3492 | pid_t *list; |
| 3493 | /* how many elements the above list has */ |
| 3494 | int length; |
| 3495 | /* how many files are using the current array */ |
| 3496 | int use_count; |
| 3497 | /* each of these stored in a list by its cgroup */ |
| 3498 | struct list_head links; |
| 3499 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3500 | struct cgroup *owner; |
| 3501 | /* protects the other fields */ |
| 3502 | struct rw_semaphore mutex; |
| 3503 | }; |
| 3504 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3505 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3506 | * The following two functions "fix" the issue where there are more pids |
| 3507 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3508 | * TODO: replace with a kernel-wide solution to this problem |
| 3509 | */ |
| 3510 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3511 | static void *pidlist_allocate(int count) |
| 3512 | { |
| 3513 | if (PIDLIST_TOO_LARGE(count)) |
| 3514 | return vmalloc(count * sizeof(pid_t)); |
| 3515 | else |
| 3516 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3517 | } |
| 3518 | static void pidlist_free(void *p) |
| 3519 | { |
| 3520 | if (is_vmalloc_addr(p)) |
| 3521 | vfree(p); |
| 3522 | else |
| 3523 | kfree(p); |
| 3524 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3525 | |
| 3526 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3527 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3528 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3529 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3530 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3531 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3532 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3533 | |
| 3534 | /* |
| 3535 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3536 | * edge cases first; no work needs to be done for either |
| 3537 | */ |
| 3538 | if (length == 0 || length == 1) |
| 3539 | return length; |
| 3540 | /* src and dest walk down the list; dest counts unique elements */ |
| 3541 | for (src = 1; src < length; src++) { |
| 3542 | /* find next unique element */ |
| 3543 | while (list[src] == list[src-1]) { |
| 3544 | src++; |
| 3545 | if (src == length) |
| 3546 | goto after; |
| 3547 | } |
| 3548 | /* dest always points to where the next unique element goes */ |
| 3549 | list[dest] = list[src]; |
| 3550 | dest++; |
| 3551 | } |
| 3552 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3553 | return dest; |
| 3554 | } |
| 3555 | |
| 3556 | static int cmppid(const void *a, const void *b) |
| 3557 | { |
| 3558 | return *(pid_t *)a - *(pid_t *)b; |
| 3559 | } |
| 3560 | |
| 3561 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3562 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3563 | * returns with the lock on that pidlist already held, and takes care |
| 3564 | * of the use count, or returns NULL with no locks held if we're out of |
| 3565 | * memory. |
| 3566 | */ |
| 3567 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3568 | enum cgroup_filetype type) |
| 3569 | { |
| 3570 | struct cgroup_pidlist *l; |
| 3571 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3572 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3573 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3574 | /* |
| 3575 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3576 | * the last ref-holder is trying to remove l from the list at the same |
| 3577 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3578 | * list we find out from under us - compare release_pid_array(). |
| 3579 | */ |
| 3580 | mutex_lock(&cgrp->pidlist_mutex); |
| 3581 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3582 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3583 | /* make sure l doesn't vanish out from under us */ |
| 3584 | down_write(&l->mutex); |
| 3585 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3586 | return l; |
| 3587 | } |
| 3588 | } |
| 3589 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3590 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3591 | if (!l) { |
| 3592 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3593 | return l; |
| 3594 | } |
| 3595 | init_rwsem(&l->mutex); |
| 3596 | down_write(&l->mutex); |
| 3597 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3598 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3599 | l->owner = cgrp; |
| 3600 | list_add(&l->links, &cgrp->pidlists); |
| 3601 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3602 | return l; |
| 3603 | } |
| 3604 | |
| 3605 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3606 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3607 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3608 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3609 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3610 | { |
| 3611 | pid_t *array; |
| 3612 | int length; |
| 3613 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3614 | struct cgroup_iter it; |
| 3615 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3616 | struct cgroup_pidlist *l; |
| 3617 | |
| 3618 | /* |
| 3619 | * If cgroup gets more users after we read count, we won't have |
| 3620 | * enough space - tough. This race is indistinguishable to the |
| 3621 | * caller from the case that the additional cgroup users didn't |
| 3622 | * show up until sometime later on. |
| 3623 | */ |
| 3624 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3625 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3626 | if (!array) |
| 3627 | return -ENOMEM; |
| 3628 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3629 | cgroup_iter_start(cgrp, &it); |
| 3630 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3631 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3632 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3633 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3634 | if (type == CGROUP_FILE_PROCS) |
| 3635 | pid = task_tgid_vnr(tsk); |
| 3636 | else |
| 3637 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3638 | if (pid > 0) /* make sure to only use valid results */ |
| 3639 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3640 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3641 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3642 | length = n; |
| 3643 | /* now sort & (if procs) strip out duplicates */ |
| 3644 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3645 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3646 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3647 | l = cgroup_pidlist_find(cgrp, type); |
| 3648 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3649 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3650 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3651 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3652 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3653 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3654 | l->list = array; |
| 3655 | l->length = length; |
| 3656 | l->use_count++; |
| 3657 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3658 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3660 | } |
| 3661 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3662 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3663 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3664 | * @stats: cgroupstats to fill information into |
| 3665 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3666 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3667 | * |
| 3668 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3669 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3670 | */ |
| 3671 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3672 | { |
| 3673 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3674 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3675 | struct cgroup_iter it; |
| 3676 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3677 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3678 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3679 | * Validate dentry by checking the superblock operations, |
| 3680 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3681 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3682 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3683 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3684 | goto err; |
| 3685 | |
| 3686 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3687 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3688 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3689 | cgroup_iter_start(cgrp, &it); |
| 3690 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3691 | switch (tsk->state) { |
| 3692 | case TASK_RUNNING: |
| 3693 | stats->nr_running++; |
| 3694 | break; |
| 3695 | case TASK_INTERRUPTIBLE: |
| 3696 | stats->nr_sleeping++; |
| 3697 | break; |
| 3698 | case TASK_UNINTERRUPTIBLE: |
| 3699 | stats->nr_uninterruptible++; |
| 3700 | break; |
| 3701 | case TASK_STOPPED: |
| 3702 | stats->nr_stopped++; |
| 3703 | break; |
| 3704 | default: |
| 3705 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3706 | stats->nr_io_wait++; |
| 3707 | break; |
| 3708 | } |
| 3709 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3710 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3711 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3712 | err: |
| 3713 | return ret; |
| 3714 | } |
| 3715 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3716 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3717 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3718 | * 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] | 3719 | * 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] | 3720 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3721 | */ |
| 3722 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3723 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3724 | { |
| 3725 | /* |
| 3726 | * Initially we receive a position value that corresponds to |
| 3727 | * one more than the last pid shown (or 0 on the first call or |
| 3728 | * after a seek to the start). Use a binary-search to find the |
| 3729 | * next pid to display, if any |
| 3730 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3731 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3732 | int index = 0, pid = *pos; |
| 3733 | int *iter; |
| 3734 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3735 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3736 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3737 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3738 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3739 | while (index < end) { |
| 3740 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3741 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3742 | index = mid; |
| 3743 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3744 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3745 | index = mid + 1; |
| 3746 | else |
| 3747 | end = mid; |
| 3748 | } |
| 3749 | } |
| 3750 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3751 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3752 | return NULL; |
| 3753 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3754 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3755 | *pos = *iter; |
| 3756 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3759 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3760 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3761 | struct cgroup_pidlist *l = s->private; |
| 3762 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3765 | 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] | 3766 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3767 | struct cgroup_pidlist *l = s->private; |
| 3768 | pid_t *p = v; |
| 3769 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3770 | /* |
| 3771 | * Advance to the next pid in the array. If this goes off the |
| 3772 | * end, we're done |
| 3773 | */ |
| 3774 | p++; |
| 3775 | if (p >= end) { |
| 3776 | return NULL; |
| 3777 | } else { |
| 3778 | *pos = *p; |
| 3779 | return p; |
| 3780 | } |
| 3781 | } |
| 3782 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3783 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3784 | { |
| 3785 | return seq_printf(s, "%d\n", *(int *)v); |
| 3786 | } |
| 3787 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3788 | /* |
| 3789 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3790 | * independent of whether it's tasks or procs |
| 3791 | */ |
| 3792 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3793 | .start = cgroup_pidlist_start, |
| 3794 | .stop = cgroup_pidlist_stop, |
| 3795 | .next = cgroup_pidlist_next, |
| 3796 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3797 | }; |
| 3798 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3799 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3800 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3801 | /* |
| 3802 | * the case where we're the last user of this particular pidlist will |
| 3803 | * have us remove it from the cgroup's list, which entails taking the |
| 3804 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3805 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3806 | */ |
| 3807 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3808 | down_write(&l->mutex); |
| 3809 | BUG_ON(!l->use_count); |
| 3810 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3811 | /* we're the last user if refcount is 0; remove and free */ |
| 3812 | list_del(&l->links); |
| 3813 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3814 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3815 | put_pid_ns(l->key.ns); |
| 3816 | up_write(&l->mutex); |
| 3817 | kfree(l); |
| 3818 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3819 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3820 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3821 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3824 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3825 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3826 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3827 | if (!(file->f_mode & FMODE_READ)) |
| 3828 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3829 | /* |
| 3830 | * the seq_file will only be initialized if the file was opened for |
| 3831 | * reading; hence we check if it's not null only in that case. |
| 3832 | */ |
| 3833 | l = ((struct seq_file *)file->private_data)->private; |
| 3834 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3835 | return seq_release(inode, file); |
| 3836 | } |
| 3837 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3838 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3839 | .read = seq_read, |
| 3840 | .llseek = seq_lseek, |
| 3841 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3842 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3843 | }; |
| 3844 | |
| 3845 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3846 | * The following functions handle opens on a file that displays a pidlist |
| 3847 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3848 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3849 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3850 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3851 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3852 | { |
| 3853 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3854 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3855 | int retval; |
| 3856 | |
| 3857 | /* Nothing to do for write-only files */ |
| 3858 | if (!(file->f_mode & FMODE_READ)) |
| 3859 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3860 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3861 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3862 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3863 | if (retval) |
| 3864 | return retval; |
| 3865 | /* configure file information */ |
| 3866 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3867 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3868 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3869 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3870 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3871 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3872 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3873 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3874 | return 0; |
| 3875 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3876 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3877 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3878 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3879 | } |
| 3880 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3881 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3882 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3883 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3884 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3885 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3886 | struct cftype *cft) |
| 3887 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3888 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3891 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3892 | struct cftype *cft, |
| 3893 | u64 val) |
| 3894 | { |
| 3895 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3896 | if (val) |
| 3897 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3898 | else |
| 3899 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3900 | return 0; |
| 3901 | } |
| 3902 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3903 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3904 | * When dput() is called asynchronously, if umount has been done and |
| 3905 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3906 | * there's a small window that vfs will see the root dentry with non-zero |
| 3907 | * refcnt and trigger BUG(). |
| 3908 | * |
| 3909 | * That's why we hold a reference before dput() and drop it right after. |
| 3910 | */ |
| 3911 | static void cgroup_dput(struct cgroup *cgrp) |
| 3912 | { |
| 3913 | struct super_block *sb = cgrp->root->sb; |
| 3914 | |
| 3915 | atomic_inc(&sb->s_active); |
| 3916 | dput(cgrp->dentry); |
| 3917 | deactivate_super(sb); |
| 3918 | } |
| 3919 | |
| 3920 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3921 | * Unregister event and free resources. |
| 3922 | * |
| 3923 | * Gets called from workqueue. |
| 3924 | */ |
| 3925 | static void cgroup_event_remove(struct work_struct *work) |
| 3926 | { |
| 3927 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3928 | remove); |
| 3929 | struct cgroup *cgrp = event->cgrp; |
| 3930 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3931 | remove_wait_queue(event->wqh, &event->wait); |
| 3932 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3933 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3934 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3935 | /* Notify userspace the event is going away. */ |
| 3936 | eventfd_signal(event->eventfd, 1); |
| 3937 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3938 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3939 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3940 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | /* |
| 3944 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3945 | * |
| 3946 | * Called with wqh->lock held and interrupts disabled. |
| 3947 | */ |
| 3948 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3949 | int sync, void *key) |
| 3950 | { |
| 3951 | struct cgroup_event *event = container_of(wait, |
| 3952 | struct cgroup_event, wait); |
| 3953 | struct cgroup *cgrp = event->cgrp; |
| 3954 | unsigned long flags = (unsigned long)key; |
| 3955 | |
| 3956 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3957 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3958 | * If the event has been detached at cgroup removal, we |
| 3959 | * can simply return knowing the other side will cleanup |
| 3960 | * for us. |
| 3961 | * |
| 3962 | * We can't race against event freeing since the other |
| 3963 | * side will require wqh->lock via remove_wait_queue(), |
| 3964 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3965 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3966 | spin_lock(&cgrp->event_list_lock); |
| 3967 | if (!list_empty(&event->list)) { |
| 3968 | list_del_init(&event->list); |
| 3969 | /* |
| 3970 | * We are in atomic context, but cgroup_event_remove() |
| 3971 | * may sleep, so we have to call it in workqueue. |
| 3972 | */ |
| 3973 | schedule_work(&event->remove); |
| 3974 | } |
| 3975 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | return 0; |
| 3979 | } |
| 3980 | |
| 3981 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3982 | wait_queue_head_t *wqh, poll_table *pt) |
| 3983 | { |
| 3984 | struct cgroup_event *event = container_of(pt, |
| 3985 | struct cgroup_event, pt); |
| 3986 | |
| 3987 | event->wqh = wqh; |
| 3988 | add_wait_queue(wqh, &event->wait); |
| 3989 | } |
| 3990 | |
| 3991 | /* |
| 3992 | * Parse input and register new cgroup event handler. |
| 3993 | * |
| 3994 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3995 | * Interpretation of args is defined by control file implementation. |
| 3996 | */ |
| 3997 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3998 | const char *buffer) |
| 3999 | { |
| 4000 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4001 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4002 | unsigned int efd, cfd; |
| 4003 | struct file *efile = NULL; |
| 4004 | struct file *cfile = NULL; |
| 4005 | char *endp; |
| 4006 | int ret; |
| 4007 | |
| 4008 | efd = simple_strtoul(buffer, &endp, 10); |
| 4009 | if (*endp != ' ') |
| 4010 | return -EINVAL; |
| 4011 | buffer = endp + 1; |
| 4012 | |
| 4013 | cfd = simple_strtoul(buffer, &endp, 10); |
| 4014 | if ((*endp != ' ') && (*endp != '\0')) |
| 4015 | return -EINVAL; |
| 4016 | buffer = endp + 1; |
| 4017 | |
| 4018 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 4019 | if (!event) |
| 4020 | return -ENOMEM; |
| 4021 | event->cgrp = cgrp; |
| 4022 | INIT_LIST_HEAD(&event->list); |
| 4023 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 4024 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 4025 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 4026 | |
| 4027 | efile = eventfd_fget(efd); |
| 4028 | if (IS_ERR(efile)) { |
| 4029 | ret = PTR_ERR(efile); |
| 4030 | goto fail; |
| 4031 | } |
| 4032 | |
| 4033 | event->eventfd = eventfd_ctx_fileget(efile); |
| 4034 | if (IS_ERR(event->eventfd)) { |
| 4035 | ret = PTR_ERR(event->eventfd); |
| 4036 | goto fail; |
| 4037 | } |
| 4038 | |
| 4039 | cfile = fget(cfd); |
| 4040 | if (!cfile) { |
| 4041 | ret = -EBADF; |
| 4042 | goto fail; |
| 4043 | } |
| 4044 | |
| 4045 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 4046 | /* AV: shouldn't we check that it's been opened for read instead? */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4047 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4048 | if (ret < 0) |
| 4049 | goto fail; |
| 4050 | |
| 4051 | event->cft = __file_cft(cfile); |
| 4052 | if (IS_ERR(event->cft)) { |
| 4053 | ret = PTR_ERR(event->cft); |
| 4054 | goto fail; |
| 4055 | } |
| 4056 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4057 | /* |
| 4058 | * The file to be monitored must be in the same cgroup as |
| 4059 | * cgroup.event_control is. |
| 4060 | */ |
| 4061 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4062 | if (cgrp_cfile != cgrp) { |
| 4063 | ret = -EINVAL; |
| 4064 | goto fail; |
| 4065 | } |
| 4066 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4067 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4068 | ret = -EINVAL; |
| 4069 | goto fail; |
| 4070 | } |
| 4071 | |
| 4072 | ret = event->cft->register_event(cgrp, event->cft, |
| 4073 | event->eventfd, buffer); |
| 4074 | if (ret) |
| 4075 | goto fail; |
| 4076 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4077 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4078 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4079 | /* |
| 4080 | * Events should be removed after rmdir of cgroup directory, but before |
| 4081 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4082 | * directory dentry to do that. |
| 4083 | */ |
| 4084 | dget(cgrp->dentry); |
| 4085 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4086 | spin_lock(&cgrp->event_list_lock); |
| 4087 | list_add(&event->list, &cgrp->event_list); |
| 4088 | spin_unlock(&cgrp->event_list_lock); |
| 4089 | |
| 4090 | fput(cfile); |
| 4091 | fput(efile); |
| 4092 | |
| 4093 | return 0; |
| 4094 | |
| 4095 | fail: |
| 4096 | if (cfile) |
| 4097 | fput(cfile); |
| 4098 | |
| 4099 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4100 | eventfd_ctx_put(event->eventfd); |
| 4101 | |
| 4102 | if (!IS_ERR_OR_NULL(efile)) |
| 4103 | fput(efile); |
| 4104 | |
| 4105 | kfree(event); |
| 4106 | |
| 4107 | return ret; |
| 4108 | } |
| 4109 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4110 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4111 | struct cftype *cft) |
| 4112 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4113 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4117 | struct cftype *cft, |
| 4118 | u64 val) |
| 4119 | { |
| 4120 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4121 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4122 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4123 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4124 | return 0; |
| 4125 | } |
| 4126 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4127 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4128 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4129 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4130 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4131 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4132 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4133 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4134 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4135 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4136 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4137 | .write_string = cgroup_write_event_control, |
| 4138 | .mode = S_IWUGO, |
| 4139 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4140 | { |
| 4141 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4142 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4143 | .read_u64 = cgroup_clone_children_read, |
| 4144 | .write_u64 = cgroup_clone_children_write, |
| 4145 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4146 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4147 | .name = "cgroup.sane_behavior", |
| 4148 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4149 | .read_seq_string = cgroup_sane_behavior_show, |
| 4150 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4151 | |
| 4152 | /* |
| 4153 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4154 | * don't exist if sane_behavior. If you're depending on these, be |
| 4155 | * prepared to be burned. |
| 4156 | */ |
| 4157 | { |
| 4158 | .name = "tasks", |
| 4159 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4160 | .open = cgroup_tasks_open, |
| 4161 | .write_u64 = cgroup_tasks_write, |
| 4162 | .release = cgroup_pidlist_release, |
| 4163 | .mode = S_IRUGO | S_IWUSR, |
| 4164 | }, |
| 4165 | { |
| 4166 | .name = "notify_on_release", |
| 4167 | .flags = CFTYPE_INSANE, |
| 4168 | .read_u64 = cgroup_read_notify_on_release, |
| 4169 | .write_u64 = cgroup_write_notify_on_release, |
| 4170 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4171 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4172 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4173 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4174 | .read_seq_string = cgroup_release_agent_show, |
| 4175 | .write_string = cgroup_release_agent_write, |
| 4176 | .max_write_len = PATH_MAX, |
| 4177 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4178 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4179 | }; |
| 4180 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4181 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4182 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4183 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4184 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4185 | * |
| 4186 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4187 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4188 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4189 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4190 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4191 | int i, ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4192 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4193 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4194 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4195 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4196 | |
| 4197 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4198 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4199 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4200 | list_for_each_entry(set, &ss->cftsets, node) { |
| 4201 | ret = cgroup_addrm_files(cgrp, ss, set->cfts, true); |
| 4202 | if (ret < 0) |
| 4203 | goto err; |
| 4204 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4205 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4206 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4207 | /* This cgroup is ready now */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4208 | for_each_root_subsys(cgrp->root, ss) { |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4209 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4210 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 4211 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4212 | /* |
| 4213 | * Update id->css pointer and make this css visible from |
| 4214 | * CSS ID functions. This pointer will be dereferened |
| 4215 | * from RCU-read-side without locks. |
| 4216 | */ |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4217 | if (id) |
| 4218 | rcu_assign_pointer(id->css, css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4219 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4220 | |
| 4221 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4222 | err: |
| 4223 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4224 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4225 | } |
| 4226 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4227 | static void css_dput_fn(struct work_struct *work) |
| 4228 | { |
| 4229 | struct cgroup_subsys_state *css = |
| 4230 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4231 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4232 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4233 | } |
| 4234 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4235 | static void css_release(struct percpu_ref *ref) |
| 4236 | { |
| 4237 | struct cgroup_subsys_state *css = |
| 4238 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4239 | |
| 4240 | schedule_work(&css->dput_work); |
| 4241 | } |
| 4242 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4243 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4244 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4245 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4246 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4247 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4248 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4249 | css->id = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4250 | if (cgrp == cgroup_dummy_top) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4251 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4252 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4253 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4254 | |
| 4255 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4256 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4257 | * css_put(). dput() requires process context, which css_put() may |
| 4258 | * be called without. @css->dput_work will be used to invoke |
| 4259 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4260 | */ |
| 4261 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4262 | } |
| 4263 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4264 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4265 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4266 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4267 | int ret = 0; |
| 4268 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4269 | lockdep_assert_held(&cgroup_mutex); |
| 4270 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4271 | if (ss->css_online) |
| 4272 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4273 | if (!ret) |
| 4274 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4275 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4279 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4280 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4281 | { |
| 4282 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4283 | |
| 4284 | lockdep_assert_held(&cgroup_mutex); |
| 4285 | |
| 4286 | if (!(css->flags & CSS_ONLINE)) |
| 4287 | return; |
| 4288 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4289 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4290 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4291 | |
| 4292 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4293 | } |
| 4294 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4295 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4296 | * cgroup_create - create a cgroup |
| 4297 | * @parent: cgroup that will be parent of the new cgroup |
| 4298 | * @dentry: dentry of the new cgroup |
| 4299 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4300 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4301 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4302 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4303 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4304 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4305 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4306 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4307 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4308 | struct cgroupfs_root *root = parent->root; |
| 4309 | int err = 0; |
| 4310 | struct cgroup_subsys *ss; |
| 4311 | struct super_block *sb = root->sb; |
| 4312 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4313 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4314 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4315 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4316 | return -ENOMEM; |
| 4317 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4318 | name = cgroup_alloc_name(dentry); |
| 4319 | if (!name) |
| 4320 | goto err_free_cgrp; |
| 4321 | rcu_assign_pointer(cgrp->name, name); |
| 4322 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4323 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4324 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4325 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4326 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4327 | /* |
| 4328 | * Only live parents can have children. Note that the liveliness |
| 4329 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4330 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4331 | * anyway so that locking is contained inside cgroup proper and we |
| 4332 | * don't get nasty surprises if we ever grow another caller. |
| 4333 | */ |
| 4334 | if (!cgroup_lock_live_group(parent)) { |
| 4335 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4336 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4337 | } |
| 4338 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4339 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4340 | * get deleted on unmount if there are child cgroups. This |
| 4341 | * can be done outside cgroup_mutex, since the sb can't |
| 4342 | * disappear while someone has an open control file on the |
| 4343 | * fs */ |
| 4344 | atomic_inc(&sb->s_active); |
| 4345 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4346 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4347 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4348 | dentry->d_fsdata = cgrp; |
| 4349 | cgrp->dentry = dentry; |
| 4350 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4351 | cgrp->parent = parent; |
| 4352 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4353 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4354 | if (notify_on_release(parent)) |
| 4355 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4356 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4357 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4358 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4359 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4360 | for_each_root_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4361 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4362 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4363 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4364 | if (IS_ERR(css)) { |
| 4365 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4366 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4367 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4368 | |
| 4369 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4370 | if (err) |
| 4371 | goto err_free_all; |
| 4372 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4373 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4374 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4375 | if (ss->use_id) { |
| 4376 | err = alloc_css_id(ss, parent, cgrp); |
| 4377 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4378 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4379 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4382 | /* |
| 4383 | * Create directory. cgroup_create_file() returns with the new |
| 4384 | * directory locked on success so that it can be populated without |
| 4385 | * dropping cgroup_mutex. |
| 4386 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4387 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4388 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4389 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4390 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4391 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4392 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4393 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4394 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4395 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4396 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4397 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4398 | /* each css holds a ref to the cgroup's dentry */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4399 | for_each_root_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4400 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4401 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4402 | /* hold a ref to the parent's dentry */ |
| 4403 | dget(parent->dentry); |
| 4404 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4405 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4406 | for_each_root_subsys(root, ss) { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4407 | err = online_css(ss, cgrp); |
| 4408 | if (err) |
| 4409 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4410 | |
| 4411 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4412 | parent->parent) { |
| 4413 | 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", |
| 4414 | current->comm, current->pid, ss->name); |
| 4415 | if (!strcmp(ss->name, "memory")) |
| 4416 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4417 | ss->warned_broken_hierarchy = true; |
| 4418 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4419 | } |
| 4420 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4421 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
| 4422 | if (err) |
| 4423 | goto err_destroy; |
| 4424 | |
| 4425 | err = cgroup_populate_dir(cgrp, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4426 | if (err) |
| 4427 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4428 | |
| 4429 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4430 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4431 | |
| 4432 | return 0; |
| 4433 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4434 | err_free_all: |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4435 | for_each_root_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4436 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4437 | |
| 4438 | if (css) { |
| 4439 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4440 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4441 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4442 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4443 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4444 | /* Release the reference count that we took on the superblock */ |
| 4445 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4446 | err_free_id: |
| 4447 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4448 | err_free_name: |
| 4449 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4450 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4451 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4452 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4453 | |
| 4454 | err_destroy: |
| 4455 | cgroup_destroy_locked(cgrp); |
| 4456 | mutex_unlock(&cgroup_mutex); |
| 4457 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4458 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4461 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4462 | { |
| 4463 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4464 | |
| 4465 | /* the vfs holds inode->i_mutex already */ |
| 4466 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4467 | } |
| 4468 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4469 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4470 | { |
| 4471 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4472 | return; |
| 4473 | |
| 4474 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4475 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4476 | schedule_work(&cgrp->destroy_work); |
| 4477 | } |
| 4478 | |
| 4479 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4480 | { |
| 4481 | struct cgroup_subsys_state *css = |
| 4482 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4483 | |
| 4484 | cgroup_css_killed(css->cgroup); |
| 4485 | } |
| 4486 | |
| 4487 | /** |
| 4488 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4489 | * @cgrp: cgroup to be destroyed |
| 4490 | * |
| 4491 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4492 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4493 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4494 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4495 | * the following two steps. |
| 4496 | * |
| 4497 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4498 | * userland visible parts and start killing the percpu refcnts of |
| 4499 | * css's. Set up so that the next stage will be kicked off once all |
| 4500 | * the percpu refcnts are confirmed to be killed. |
| 4501 | * |
| 4502 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4503 | * rest of destruction. Once all cgroup references are gone, the |
| 4504 | * cgroup is RCU-freed. |
| 4505 | * |
| 4506 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4507 | * the userland is concerned and a new cgroup with the same name may be |
| 4508 | * created. As cgroup doesn't care about the names internally, this |
| 4509 | * doesn't cause any problem. |
| 4510 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4511 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4512 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4513 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4514 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4515 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4516 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4517 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4518 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4519 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4520 | lockdep_assert_held(&cgroup_mutex); |
| 4521 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4522 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4523 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4524 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4525 | */ |
| 4526 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4527 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4528 | read_unlock(&css_set_lock); |
| 4529 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4530 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4531 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4532 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4533 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4534 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4535 | * css reference will be given out via css_tryget(). We can't |
| 4536 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4537 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4538 | * as killed on all CPUs on return. |
| 4539 | * |
| 4540 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4541 | * css is confirmed to be seen as killed on all CPUs. The |
| 4542 | * notification callback keeps track of the number of css's to be |
| 4543 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4544 | * destruction once the percpu refs of all css's are confirmed to |
| 4545 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4546 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4547 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4548 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4549 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4550 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4551 | /* |
| 4552 | * Killing would put the base ref, but we need to keep it |
| 4553 | * alive until after ->css_offline. |
| 4554 | */ |
| 4555 | percpu_ref_get(&css->refcnt); |
| 4556 | |
| 4557 | atomic_inc(&cgrp->css_kill_cnt); |
| 4558 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4559 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4560 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4561 | |
| 4562 | /* |
| 4563 | * Mark @cgrp dead. This prevents further task migration and child |
| 4564 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4565 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4566 | * resume iteration after dropping RCU read lock. See |
| 4567 | * cgroup_next_sibling() for details. |
| 4568 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4569 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4570 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4571 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4572 | raw_spin_lock(&release_list_lock); |
| 4573 | if (!list_empty(&cgrp->release_list)) |
| 4574 | list_del_init(&cgrp->release_list); |
| 4575 | raw_spin_unlock(&release_list_lock); |
| 4576 | |
| 4577 | /* |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4578 | * Clear and remove @cgrp directory. The removal puts the base ref |
| 4579 | * but we aren't quite done with @cgrp yet, so hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4580 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4581 | cgroup_clear_dir(cgrp, cgrp->root->subsys_mask); |
| 4582 | cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4583 | dget(d); |
| 4584 | cgroup_d_remove_dir(d); |
| 4585 | |
| 4586 | /* |
| 4587 | * Unregister events and notify userspace. |
| 4588 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4589 | * directory to avoid race between userspace and kernelspace. |
| 4590 | */ |
| 4591 | spin_lock(&cgrp->event_list_lock); |
| 4592 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4593 | list_del_init(&event->list); |
| 4594 | schedule_work(&event->remove); |
| 4595 | } |
| 4596 | spin_unlock(&cgrp->event_list_lock); |
| 4597 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4598 | return 0; |
| 4599 | }; |
| 4600 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4601 | /** |
| 4602 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4603 | * @work: cgroup->destroy_free_work |
| 4604 | * |
| 4605 | * This function is invoked from a work item for a cgroup which is being |
| 4606 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4607 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4608 | * is the second step of destruction described in the comment above |
| 4609 | * cgroup_destroy_locked(). |
| 4610 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4611 | static void cgroup_offline_fn(struct work_struct *work) |
| 4612 | { |
| 4613 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4614 | struct cgroup *parent = cgrp->parent; |
| 4615 | struct dentry *d = cgrp->dentry; |
| 4616 | struct cgroup_subsys *ss; |
| 4617 | |
| 4618 | mutex_lock(&cgroup_mutex); |
| 4619 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4620 | /* |
| 4621 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4622 | * initate destruction. |
| 4623 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4624 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4625 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4626 | |
| 4627 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4628 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4629 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4630 | * proceeds regardless of css refs. On the last put of each css, |
| 4631 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4632 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4633 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4634 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4635 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4636 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4637 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4638 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4639 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4640 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4641 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4642 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4643 | check_for_release(parent); |
| 4644 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4645 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4648 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4649 | { |
| 4650 | int ret; |
| 4651 | |
| 4652 | mutex_lock(&cgroup_mutex); |
| 4653 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4654 | mutex_unlock(&cgroup_mutex); |
| 4655 | |
| 4656 | return ret; |
| 4657 | } |
| 4658 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4659 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4660 | { |
| 4661 | INIT_LIST_HEAD(&ss->cftsets); |
| 4662 | |
| 4663 | /* |
| 4664 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4665 | * deregistration. |
| 4666 | */ |
| 4667 | if (ss->base_cftypes) { |
| 4668 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4669 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4670 | } |
| 4671 | } |
| 4672 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4673 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4674 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4675 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4676 | |
| 4677 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4678 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4679 | mutex_lock(&cgroup_mutex); |
| 4680 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4681 | /* init base cftset */ |
| 4682 | cgroup_init_cftsets(ss); |
| 4683 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4684 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4685 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4686 | ss->root = &cgroup_dummy_root; |
| 4687 | css = ss->css_alloc(cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4688 | /* We don't handle early failures gracefully */ |
| 4689 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4690 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4691 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4692 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4693 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4694 | * newly registered, all tasks and hence the |
| 4695 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4696 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4697 | |
| 4698 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4699 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4700 | /* At system boot, before all subsystems have been |
| 4701 | * registered, no tasks have been forked, so we don't |
| 4702 | * need to invoke fork callbacks here. */ |
| 4703 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4704 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4705 | BUG_ON(online_css(ss, cgroup_dummy_top)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4706 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4707 | mutex_unlock(&cgroup_mutex); |
| 4708 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4709 | /* this function shouldn't be used with modular subsystems, since they |
| 4710 | * need to register a subsys_id, among other things */ |
| 4711 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4715 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4716 | * @ss: the subsystem to load |
| 4717 | * |
| 4718 | * This function should be called in a modular subsystem's initcall. If the |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 4719 | * subsystem is built as a module, it will be assigned a new subsys_id and set |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4720 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4721 | * simpler cgroup_init_subsys. |
| 4722 | */ |
| 4723 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4724 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4725 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4726 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4727 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4728 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4729 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4730 | |
| 4731 | /* check name and function validity */ |
| 4732 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4733 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4734 | return -EINVAL; |
| 4735 | |
| 4736 | /* |
| 4737 | * we don't support callbacks in modular subsystems. this check is |
| 4738 | * before the ss->module check for consistency; a subsystem that could |
| 4739 | * be a module should still have no callbacks even if the user isn't |
| 4740 | * compiling it as one. |
| 4741 | */ |
| 4742 | if (ss->fork || ss->exit) |
| 4743 | return -EINVAL; |
| 4744 | |
| 4745 | /* |
| 4746 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4747 | * since cgroup_init_subsys will have already taken care of it. |
| 4748 | */ |
| 4749 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4750 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4751 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4752 | return 0; |
| 4753 | } |
| 4754 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4755 | /* init base cftset */ |
| 4756 | cgroup_init_cftsets(ss); |
| 4757 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4758 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4759 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4760 | |
| 4761 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4762 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4763 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4764 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4765 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4766 | css = ss->css_alloc(cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4767 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4768 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4769 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4770 | mutex_unlock(&cgroup_mutex); |
| 4771 | return PTR_ERR(css); |
| 4772 | } |
| 4773 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4774 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4775 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4776 | |
| 4777 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4778 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4779 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4780 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4781 | ret = cgroup_init_idr(ss, css); |
| 4782 | if (ret) |
| 4783 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | /* |
| 4787 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4788 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4789 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4790 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4791 | * table state, so each changed css_set will need its hash recomputed. |
| 4792 | * this is all done under the css_set_lock. |
| 4793 | */ |
| 4794 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4795 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4796 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4797 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4798 | continue; |
| 4799 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4800 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4801 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4802 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4803 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4804 | key = css_set_hash(cset->subsys); |
| 4805 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4806 | } |
| 4807 | write_unlock(&css_set_lock); |
| 4808 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4809 | ret = online_css(ss, cgroup_dummy_top); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4810 | if (ret) |
| 4811 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4812 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4813 | /* success! */ |
| 4814 | mutex_unlock(&cgroup_mutex); |
| 4815 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4816 | |
| 4817 | err_unload: |
| 4818 | mutex_unlock(&cgroup_mutex); |
| 4819 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4820 | cgroup_unload_subsys(ss); |
| 4821 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4822 | } |
| 4823 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4824 | |
| 4825 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4826 | * cgroup_unload_subsys: unload a modular subsystem |
| 4827 | * @ss: the subsystem to unload |
| 4828 | * |
| 4829 | * This function should be called in a modular subsystem's exitcall. When this |
| 4830 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4831 | * the subsystem will not be attached to any hierarchy. |
| 4832 | */ |
| 4833 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4834 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4835 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4836 | |
| 4837 | BUG_ON(ss->module == NULL); |
| 4838 | |
| 4839 | /* |
| 4840 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4841 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4842 | * doesn't start being used while we're killing it off. |
| 4843 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4844 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4845 | |
| 4846 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4847 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4848 | offline_css(ss, cgroup_dummy_top); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4849 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4850 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4851 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4852 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4853 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4854 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4855 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4856 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4857 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4858 | |
| 4859 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4860 | * disentangle the css from all css_sets attached to the dummy |
| 4861 | * top. as in loading, we need to pay our respects to the hashtable |
| 4862 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4863 | */ |
| 4864 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4865 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4866 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4867 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4868 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4869 | hash_del(&cset->hlist); |
| 4870 | cset->subsys[ss->subsys_id] = NULL; |
| 4871 | key = css_set_hash(cset->subsys); |
| 4872 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4873 | } |
| 4874 | write_unlock(&css_set_lock); |
| 4875 | |
| 4876 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4877 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4878 | * need to free before marking as null because ss->css_free needs |
| 4879 | * the cgrp->subsys pointer to find their state. note that this |
| 4880 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4881 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4882 | ss->css_free(cgroup_dummy_top); |
| 4883 | cgroup_dummy_top->subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4884 | |
| 4885 | mutex_unlock(&cgroup_mutex); |
| 4886 | } |
| 4887 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4888 | |
| 4889 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4890 | * cgroup_init_early - cgroup initialization at system boot |
| 4891 | * |
| 4892 | * Initialize cgroups at system boot, and initialize any |
| 4893 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4894 | */ |
| 4895 | int __init cgroup_init_early(void) |
| 4896 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4897 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4898 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4899 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4900 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4901 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4902 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4903 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4904 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4905 | init_cgroup_root(&cgroup_dummy_root); |
| 4906 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4907 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4908 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4909 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4910 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4911 | list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4912 | list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4913 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4914 | /* at bootup time, we don't worry about modular subsystems */ |
| 4915 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4916 | BUG_ON(!ss->name); |
| 4917 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4918 | BUG_ON(!ss->css_alloc); |
| 4919 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4920 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4921 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4922 | ss->name, ss->subsys_id); |
| 4923 | BUG(); |
| 4924 | } |
| 4925 | |
| 4926 | if (ss->early_init) |
| 4927 | cgroup_init_subsys(ss); |
| 4928 | } |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
| 4932 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4933 | * cgroup_init - cgroup initialization |
| 4934 | * |
| 4935 | * Register cgroup filesystem and /proc file, and initialize |
| 4936 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4937 | */ |
| 4938 | int __init cgroup_init(void) |
| 4939 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4940 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4941 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4942 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4943 | |
| 4944 | err = bdi_init(&cgroup_backing_dev_info); |
| 4945 | if (err) |
| 4946 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4947 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4948 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4949 | if (!ss->early_init) |
| 4950 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4951 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4952 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4953 | } |
| 4954 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4955 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4956 | mutex_lock(&cgroup_mutex); |
| 4957 | mutex_lock(&cgroup_root_mutex); |
| 4958 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4959 | /* Add init_css_set to the hash table */ |
| 4960 | key = css_set_hash(init_css_set.subsys); |
| 4961 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4962 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4963 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4964 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4965 | mutex_unlock(&cgroup_root_mutex); |
| 4966 | mutex_unlock(&cgroup_mutex); |
| 4967 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4968 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4969 | if (!cgroup_kobj) { |
| 4970 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4971 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | err = register_filesystem(&cgroup_fs_type); |
| 4975 | if (err < 0) { |
| 4976 | kobject_put(cgroup_kobj); |
| 4977 | goto out; |
| 4978 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4979 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4980 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4981 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4982 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4983 | if (err) |
| 4984 | bdi_destroy(&cgroup_backing_dev_info); |
| 4985 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4986 | return err; |
| 4987 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4988 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4989 | /* |
| 4990 | * proc_cgroup_show() |
| 4991 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4992 | * - Used for /proc/<pid>/cgroup. |
| 4993 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4994 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4995 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4996 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4997 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4998 | * cgroup to top_cgroup. |
| 4999 | */ |
| 5000 | |
| 5001 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 5002 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5003 | { |
| 5004 | struct pid *pid; |
| 5005 | struct task_struct *tsk; |
| 5006 | char *buf; |
| 5007 | int retval; |
| 5008 | struct cgroupfs_root *root; |
| 5009 | |
| 5010 | retval = -ENOMEM; |
| 5011 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 5012 | if (!buf) |
| 5013 | goto out; |
| 5014 | |
| 5015 | retval = -ESRCH; |
| 5016 | pid = m->private; |
| 5017 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 5018 | if (!tsk) |
| 5019 | goto out_free; |
| 5020 | |
| 5021 | retval = 0; |
| 5022 | |
| 5023 | mutex_lock(&cgroup_mutex); |
| 5024 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 5025 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5026 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5027 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5028 | int count = 0; |
| 5029 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5030 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 5031 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5032 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5033 | if (strlen(root->name)) |
| 5034 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5035 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5036 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5037 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5038 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5039 | if (retval < 0) |
| 5040 | goto out_unlock; |
| 5041 | seq_puts(m, buf); |
| 5042 | seq_putc(m, '\n'); |
| 5043 | } |
| 5044 | |
| 5045 | out_unlock: |
| 5046 | mutex_unlock(&cgroup_mutex); |
| 5047 | put_task_struct(tsk); |
| 5048 | out_free: |
| 5049 | kfree(buf); |
| 5050 | out: |
| 5051 | return retval; |
| 5052 | } |
| 5053 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5054 | /* Display information about each subsystem and each hierarchy */ |
| 5055 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5056 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5057 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5058 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5059 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5060 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5061 | /* |
| 5062 | * ideally we don't want subsystems moving around while we do this. |
| 5063 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5064 | * subsys/hierarchy state. |
| 5065 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5066 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5067 | |
| 5068 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5069 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5070 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5071 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5072 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5073 | mutex_unlock(&cgroup_mutex); |
| 5074 | return 0; |
| 5075 | } |
| 5076 | |
| 5077 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5078 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5079 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5080 | } |
| 5081 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5082 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5083 | .open = cgroupstats_open, |
| 5084 | .read = seq_read, |
| 5085 | .llseek = seq_lseek, |
| 5086 | .release = single_release, |
| 5087 | }; |
| 5088 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5089 | /** |
| 5090 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5091 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5092 | * |
| 5093 | * Description: A task inherits its parent's cgroup at fork(). |
| 5094 | * |
| 5095 | * A pointer to the shared css_set was automatically copied in |
| 5096 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5097 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5098 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5099 | * have already changed current->cgroups, allowing the previously |
| 5100 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5101 | * |
| 5102 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5103 | * task, and the passed argument 'child' points to the child task. |
| 5104 | */ |
| 5105 | void cgroup_fork(struct task_struct *child) |
| 5106 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5107 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5108 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5109 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5110 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5111 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5115 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5116 | * @child: the task in question |
| 5117 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5118 | * Adds the task to the list running through its css_set if necessary and |
| 5119 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5120 | * visible on the task list in case we race with the first call to |
| 5121 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5122 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5123 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5124 | void cgroup_post_fork(struct task_struct *child) |
| 5125 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5126 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5127 | int i; |
| 5128 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5129 | /* |
| 5130 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5131 | * under the tasklist_lock and we read it here after we added the child |
| 5132 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5133 | * yet in the tasklist when we walked through it from |
| 5134 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5135 | * should be visible now due to the paired locking and barriers implied |
| 5136 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5137 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5138 | * lock on fork. |
| 5139 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5140 | if (use_task_css_set_links) { |
| 5141 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5142 | task_lock(child); |
| 5143 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5144 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5145 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5146 | write_unlock(&css_set_lock); |
| 5147 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5148 | |
| 5149 | /* |
| 5150 | * Call ss->fork(). This must happen after @child is linked on |
| 5151 | * css_set; otherwise, @child might change state between ->fork() |
| 5152 | * and addition to css_set. |
| 5153 | */ |
| 5154 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5155 | /* |
| 5156 | * fork/exit callbacks are supported only for builtin |
| 5157 | * subsystems, and the builtin section of the subsys |
| 5158 | * array is immutable, so we don't need to lock the |
| 5159 | * subsys array here. On the other hand, modular section |
| 5160 | * of the array can be freed at module unload, so we |
| 5161 | * can't touch that. |
| 5162 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5163 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5164 | if (ss->fork) |
| 5165 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5166 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5167 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5168 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5169 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5170 | * cgroup_exit - detach cgroup from exiting task |
| 5171 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5172 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5173 | * |
| 5174 | * Description: Detach cgroup from @tsk and release it. |
| 5175 | * |
| 5176 | * Note that cgroups marked notify_on_release force every task in |
| 5177 | * them to take the global cgroup_mutex mutex when exiting. |
| 5178 | * This could impact scaling on very large systems. Be reluctant to |
| 5179 | * use notify_on_release cgroups where very high task exit scaling |
| 5180 | * is required on large systems. |
| 5181 | * |
| 5182 | * the_top_cgroup_hack: |
| 5183 | * |
| 5184 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5185 | * |
| 5186 | * We call cgroup_exit() while the task is still competent to |
| 5187 | * handle notify_on_release(), then leave the task attached to the |
| 5188 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5189 | * |
| 5190 | * To do this properly, we would increment the reference count on |
| 5191 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5192 | * code we would add a second cgroup function call, to drop that |
| 5193 | * reference. This would just create an unnecessary hot spot on |
| 5194 | * the top_cgroup reference count, to no avail. |
| 5195 | * |
| 5196 | * Normally, holding a reference to a cgroup without bumping its |
| 5197 | * count is unsafe. The cgroup could go away, or someone could |
| 5198 | * attach us to a different cgroup, decrementing the count on |
| 5199 | * the first cgroup that we never incremented. But in this case, |
| 5200 | * top_cgroup isn't going away, and either task has PF_EXITING set, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 5201 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5202 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5203 | */ |
| 5204 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5205 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5206 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5207 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5208 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5209 | |
| 5210 | /* |
| 5211 | * Unlink from the css_set task list if necessary. |
| 5212 | * Optimistically check cg_list before taking |
| 5213 | * css_set_lock |
| 5214 | */ |
| 5215 | if (!list_empty(&tsk->cg_list)) { |
| 5216 | write_lock(&css_set_lock); |
| 5217 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5218 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5219 | write_unlock(&css_set_lock); |
| 5220 | } |
| 5221 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5222 | /* Reassign the task to the init_css_set. */ |
| 5223 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5224 | cset = task_css_set(tsk); |
| 5225 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5226 | |
| 5227 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5228 | /* |
| 5229 | * fork/exit callbacks are supported only for builtin |
| 5230 | * subsystems, see cgroup_post_fork() for details. |
| 5231 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5232 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5233 | if (ss->exit) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5234 | struct cgroup *old_cgrp = cset->subsys[i]->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5235 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5236 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5237 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5238 | } |
| 5239 | } |
| 5240 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5241 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5242 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5243 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5244 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5245 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5246 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5247 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5248 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5249 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5250 | /* |
| 5251 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5252 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5253 | * it now |
| 5254 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5255 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5256 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5257 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5258 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5259 | list_empty(&cgrp->release_list)) { |
| 5260 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5261 | need_schedule_work = 1; |
| 5262 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5263 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5264 | if (need_schedule_work) |
| 5265 | schedule_work(&release_agent_work); |
| 5266 | } |
| 5267 | } |
| 5268 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5269 | /* |
| 5270 | * Notify userspace when a cgroup is released, by running the |
| 5271 | * configured release agent with the name of the cgroup (path |
| 5272 | * relative to the root of cgroup file system) as the argument. |
| 5273 | * |
| 5274 | * Most likely, this user command will try to rmdir this cgroup. |
| 5275 | * |
| 5276 | * This races with the possibility that some other task will be |
| 5277 | * attached to this cgroup before it is removed, or that some other |
| 5278 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5279 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5280 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5281 | * to continue to serve a useful existence. Next time it's released, |
| 5282 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5283 | * |
| 5284 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5285 | * means only wait until the task is successfully execve()'d. The |
| 5286 | * separate release agent task is forked by call_usermodehelper(), |
| 5287 | * then control in this thread returns here, without waiting for the |
| 5288 | * release agent task. We don't bother to wait because the caller of |
| 5289 | * this routine has no use for the exit status of the release agent |
| 5290 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5291 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5292 | static void cgroup_release_agent(struct work_struct *work) |
| 5293 | { |
| 5294 | BUG_ON(work != &release_agent_work); |
| 5295 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5296 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5297 | while (!list_empty(&release_list)) { |
| 5298 | char *argv[3], *envp[3]; |
| 5299 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5300 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5301 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5302 | struct cgroup, |
| 5303 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5304 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5305 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5306 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5307 | if (!pathbuf) |
| 5308 | goto continue_free; |
| 5309 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5310 | goto continue_free; |
| 5311 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5312 | if (!agentbuf) |
| 5313 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5314 | |
| 5315 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5316 | argv[i++] = agentbuf; |
| 5317 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5318 | argv[i] = NULL; |
| 5319 | |
| 5320 | i = 0; |
| 5321 | /* minimal command environment */ |
| 5322 | envp[i++] = "HOME=/"; |
| 5323 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5324 | envp[i] = NULL; |
| 5325 | |
| 5326 | /* Drop the lock while we invoke the usermode helper, |
| 5327 | * since the exec could involve hitting disk and hence |
| 5328 | * be a slow process */ |
| 5329 | mutex_unlock(&cgroup_mutex); |
| 5330 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5331 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5332 | continue_free: |
| 5333 | kfree(pathbuf); |
| 5334 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5335 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5336 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5337 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5338 | mutex_unlock(&cgroup_mutex); |
| 5339 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5340 | |
| 5341 | static int __init cgroup_disable(char *str) |
| 5342 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5343 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5344 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5345 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5346 | |
| 5347 | while ((token = strsep(&str, ",")) != NULL) { |
| 5348 | if (!*token) |
| 5349 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5350 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5351 | /* |
| 5352 | * cgroup_disable, being at boot time, can't know about |
| 5353 | * module subsystems, so we don't worry about them. |
| 5354 | */ |
| 5355 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5356 | if (!strcmp(token, ss->name)) { |
| 5357 | ss->disabled = 1; |
| 5358 | printk(KERN_INFO "Disabling %s control group" |
| 5359 | " subsystem\n", ss->name); |
| 5360 | break; |
| 5361 | } |
| 5362 | } |
| 5363 | } |
| 5364 | return 1; |
| 5365 | } |
| 5366 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5367 | |
| 5368 | /* |
| 5369 | * Functons for CSS ID. |
| 5370 | */ |
| 5371 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5372 | /* to get ID other than 0, this should be called when !cgroup_is_dead() */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5373 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5374 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5375 | struct css_id *cssid; |
| 5376 | |
| 5377 | /* |
| 5378 | * This css_id() can return correct value when somone has refcnt |
| 5379 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5380 | * it's unchanged until freed. |
| 5381 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5382 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5383 | |
| 5384 | if (cssid) |
| 5385 | return cssid->id; |
| 5386 | return 0; |
| 5387 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5388 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5389 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5390 | /** |
| 5391 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5392 | * @child: the css to be tested. |
| 5393 | * @root: the css supporsed to be an ancestor of the child. |
| 5394 | * |
| 5395 | * Returns true if "root" is an ancestor of "child" in its hierarchy. Because |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5396 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5397 | * But, considering usual usage, the csses should be valid objects after test. |
| 5398 | * Assuming that the caller will do some action to the child if this returns |
| 5399 | * returns true, the caller must take "child";s reference count. |
| 5400 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5401 | */ |
| 5402 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5403 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5404 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5405 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5406 | struct css_id *child_id; |
| 5407 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5408 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5409 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5410 | if (!child_id) |
| 5411 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5412 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5413 | if (!root_id) |
| 5414 | return false; |
| 5415 | if (child_id->depth < root_id->depth) |
| 5416 | return false; |
| 5417 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5418 | return false; |
| 5419 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5420 | } |
| 5421 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5422 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5423 | { |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5424 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 5425 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5426 | /* When this is called before css_id initialization, id can be NULL */ |
| 5427 | if (!id) |
| 5428 | return; |
| 5429 | |
| 5430 | BUG_ON(!ss->use_id); |
| 5431 | |
| 5432 | rcu_assign_pointer(id->css, NULL); |
| 5433 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5434 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5435 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5436 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5437 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5438 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5439 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5440 | |
| 5441 | /* |
| 5442 | * This is called by init or create(). Then, calls to this function are |
| 5443 | * always serialized (By cgroup_mutex() at create()). |
| 5444 | */ |
| 5445 | |
| 5446 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5447 | { |
| 5448 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5449 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5450 | |
| 5451 | BUG_ON(!ss->use_id); |
| 5452 | |
| 5453 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5454 | newid = kzalloc(size, GFP_KERNEL); |
| 5455 | if (!newid) |
| 5456 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5457 | |
| 5458 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5459 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5460 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5461 | ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5462 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5463 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5464 | |
| 5465 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5466 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5467 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5468 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5469 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5470 | newid->depth = depth; |
| 5471 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5472 | err_out: |
| 5473 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5474 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5475 | |
| 5476 | } |
| 5477 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5478 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5479 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5480 | { |
| 5481 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5482 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5483 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5484 | idr_init(&ss->idr); |
| 5485 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5486 | newid = get_new_cssid(ss, 0); |
| 5487 | if (IS_ERR(newid)) |
| 5488 | return PTR_ERR(newid); |
| 5489 | |
| 5490 | newid->stack[0] = newid->id; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5491 | RCU_INIT_POINTER(newid->css, rootcss); |
| 5492 | RCU_INIT_POINTER(rootcss->id, newid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5493 | return 0; |
| 5494 | } |
| 5495 | |
| 5496 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5497 | struct cgroup *child) |
| 5498 | { |
| 5499 | int subsys_id, i, depth = 0; |
| 5500 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5501 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5502 | |
| 5503 | subsys_id = ss->subsys_id; |
| 5504 | parent_css = parent->subsys[subsys_id]; |
| 5505 | child_css = child->subsys[subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5506 | parent_id = rcu_dereference_protected(parent_css->id, true); |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5507 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5508 | |
| 5509 | child_id = get_new_cssid(ss, depth); |
| 5510 | if (IS_ERR(child_id)) |
| 5511 | return PTR_ERR(child_id); |
| 5512 | |
| 5513 | for (i = 0; i < depth; i++) |
| 5514 | child_id->stack[i] = parent_id->stack[i]; |
| 5515 | child_id->stack[depth] = child_id->id; |
| 5516 | /* |
| 5517 | * child_id->css pointer will be set after this cgroup is available |
| 5518 | * see cgroup_populate_dir() |
| 5519 | */ |
| 5520 | rcu_assign_pointer(child_css->id, child_id); |
| 5521 | |
| 5522 | return 0; |
| 5523 | } |
| 5524 | |
| 5525 | /** |
| 5526 | * css_lookup - lookup css by id |
| 5527 | * @ss: cgroup subsys to be looked into. |
| 5528 | * @id: the id |
| 5529 | * |
| 5530 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5531 | * NULL if not. Should be called under rcu_read_lock() |
| 5532 | */ |
| 5533 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5534 | { |
| 5535 | struct css_id *cssid = NULL; |
| 5536 | |
| 5537 | BUG_ON(!ss->use_id); |
| 5538 | cssid = idr_find(&ss->idr, id); |
| 5539 | |
| 5540 | if (unlikely(!cssid)) |
| 5541 | return NULL; |
| 5542 | |
| 5543 | return rcu_dereference(cssid->css); |
| 5544 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5545 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5546 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5547 | /* |
| 5548 | * get corresponding css from file open on cgroupfs directory |
| 5549 | */ |
| 5550 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5551 | { |
| 5552 | struct cgroup *cgrp; |
| 5553 | struct inode *inode; |
| 5554 | struct cgroup_subsys_state *css; |
| 5555 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5556 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5557 | /* check in cgroup filesystem dir */ |
| 5558 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5559 | return ERR_PTR(-EBADF); |
| 5560 | |
| 5561 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5562 | return ERR_PTR(-EINVAL); |
| 5563 | |
| 5564 | /* get cgroup */ |
| 5565 | cgrp = __d_cgrp(f->f_dentry); |
| 5566 | css = cgrp->subsys[id]; |
| 5567 | return css ? css : ERR_PTR(-ENOENT); |
| 5568 | } |
| 5569 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5570 | #ifdef CONFIG_CGROUP_DEBUG |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5571 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5572 | { |
| 5573 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5574 | |
| 5575 | if (!css) |
| 5576 | return ERR_PTR(-ENOMEM); |
| 5577 | |
| 5578 | return css; |
| 5579 | } |
| 5580 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5581 | static void debug_css_free(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5582 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5583 | kfree(cgrp->subsys[debug_subsys_id]); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5586 | static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5587 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5588 | return cgroup_task_count(cgrp); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5589 | } |
| 5590 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5591 | static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5592 | { |
| 5593 | return (u64)(unsigned long)current->cgroups; |
| 5594 | } |
| 5595 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5596 | static u64 current_css_set_refcount_read(struct cgroup *cgrp, |
| 5597 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5598 | { |
| 5599 | u64 count; |
| 5600 | |
| 5601 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5602 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5603 | rcu_read_unlock(); |
| 5604 | return count; |
| 5605 | } |
| 5606 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5607 | static int current_css_set_cg_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5608 | struct cftype *cft, |
| 5609 | struct seq_file *seq) |
| 5610 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5611 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5612 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5613 | |
| 5614 | read_lock(&css_set_lock); |
| 5615 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5616 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5617 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5618 | struct cgroup *c = link->cgrp; |
| 5619 | const char *name; |
| 5620 | |
| 5621 | if (c->dentry) |
| 5622 | name = c->dentry->d_name.name; |
| 5623 | else |
| 5624 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5625 | seq_printf(seq, "Root %d group %s\n", |
| 5626 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5627 | } |
| 5628 | rcu_read_unlock(); |
| 5629 | read_unlock(&css_set_lock); |
| 5630 | return 0; |
| 5631 | } |
| 5632 | |
| 5633 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5634 | static int cgroup_css_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5635 | struct cftype *cft, |
| 5636 | struct seq_file *seq) |
| 5637 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5638 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5639 | |
| 5640 | read_lock(&css_set_lock); |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5641 | list_for_each_entry(link, &cgrp->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5642 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5643 | struct task_struct *task; |
| 5644 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5645 | seq_printf(seq, "css_set %p\n", cset); |
| 5646 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5647 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5648 | seq_puts(seq, " ...\n"); |
| 5649 | break; |
| 5650 | } else { |
| 5651 | seq_printf(seq, " task %d\n", |
| 5652 | task_pid_vnr(task)); |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | read_unlock(&css_set_lock); |
| 5657 | return 0; |
| 5658 | } |
| 5659 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5660 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5661 | { |
| 5662 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5663 | } |
| 5664 | |
| 5665 | static struct cftype debug_files[] = { |
| 5666 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5667 | .name = "taskcount", |
| 5668 | .read_u64 = debug_taskcount_read, |
| 5669 | }, |
| 5670 | |
| 5671 | { |
| 5672 | .name = "current_css_set", |
| 5673 | .read_u64 = current_css_set_read, |
| 5674 | }, |
| 5675 | |
| 5676 | { |
| 5677 | .name = "current_css_set_refcount", |
| 5678 | .read_u64 = current_css_set_refcount_read, |
| 5679 | }, |
| 5680 | |
| 5681 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5682 | .name = "current_css_set_cg_links", |
| 5683 | .read_seq_string = current_css_set_cg_links_read, |
| 5684 | }, |
| 5685 | |
| 5686 | { |
| 5687 | .name = "cgroup_css_links", |
| 5688 | .read_seq_string = cgroup_css_links_read, |
| 5689 | }, |
| 5690 | |
| 5691 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5692 | .name = "releasable", |
| 5693 | .read_u64 = releasable_read, |
| 5694 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5695 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5696 | { } /* terminate */ |
| 5697 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5698 | |
| 5699 | struct cgroup_subsys debug_subsys = { |
| 5700 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5701 | .css_alloc = debug_css_alloc, |
| 5702 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5703 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5704 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5705 | }; |
| 5706 | #endif /* CONFIG_CGROUP_DEBUG */ |