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 | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 218 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 219 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 220 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 221 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 222 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 223 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 224 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 226 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 229 | /** |
| 230 | * cgroup_is_descendant - test ancestry |
| 231 | * @cgrp: the cgroup to be tested |
| 232 | * @ancestor: possible ancestor of @cgrp |
| 233 | * |
| 234 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 235 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 236 | * and @ancestor are accessible. |
| 237 | */ |
| 238 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 239 | { |
| 240 | while (cgrp) { |
| 241 | if (cgrp == ancestor) |
| 242 | return true; |
| 243 | cgrp = cgrp->parent; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 248 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 249 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 250 | { |
| 251 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 252 | (1 << CGRP_RELEASABLE) | |
| 253 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 254 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 257 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 258 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 259 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 262 | /** |
| 263 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 264 | * @ss: the iteration cursor |
| 265 | * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 266 | * |
| 267 | * Should be called under cgroup_mutex. |
| 268 | */ |
| 269 | #define for_each_subsys(ss, i) \ |
| 270 | for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ |
| 271 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 272 | !((ss) = cgroup_subsys[i]); })) { } \ |
| 273 | else |
| 274 | |
| 275 | /** |
| 276 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 277 | * @ss: the iteration cursor |
| 278 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 279 | * |
| 280 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 281 | * require any synchronization. |
| 282 | */ |
| 283 | #define for_each_builtin_subsys(ss, i) \ |
| 284 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 285 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 286 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 287 | /* iterate each subsystem attached to a hierarchy */ |
| 288 | #define for_each_root_subsys(root, ss) \ |
| 289 | list_for_each_entry((ss), &(root)->subsys_list, sibling) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 290 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 291 | /* iterate across the active hierarchies */ |
| 292 | #define for_each_active_root(root) \ |
| 293 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 294 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 295 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 296 | { |
| 297 | return dentry->d_fsdata; |
| 298 | } |
| 299 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 300 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 301 | { |
| 302 | return dentry->d_fsdata; |
| 303 | } |
| 304 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 305 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 306 | { |
| 307 | return __d_cfe(dentry)->type; |
| 308 | } |
| 309 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 310 | /** |
| 311 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 312 | * @cgrp: the cgroup to be checked for liveness |
| 313 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 314 | * On success, returns true; the mutex should be later unlocked. On |
| 315 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 316 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 317 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 318 | { |
| 319 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 320 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 321 | mutex_unlock(&cgroup_mutex); |
| 322 | return false; |
| 323 | } |
| 324 | return true; |
| 325 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 326 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 327 | /* the list of cgroups eligible for automatic release. Protected by |
| 328 | * release_list_lock */ |
| 329 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 330 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 331 | static void cgroup_release_agent(struct work_struct *work); |
| 332 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 333 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 334 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 335 | /* |
| 336 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 337 | * belong to different cgroups on different hierarchies. In the other |
| 338 | * direction, a css_set is naturally associated with multiple cgroups. |
| 339 | * This M:N relationship is represented by the following link structure |
| 340 | * which exists for each association and allows traversing the associations |
| 341 | * from both sides. |
| 342 | */ |
| 343 | struct cgrp_cset_link { |
| 344 | /* the cgroup and css_set this link associates */ |
| 345 | struct cgroup *cgrp; |
| 346 | struct css_set *cset; |
| 347 | |
| 348 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 349 | struct list_head cset_link; |
| 350 | |
| 351 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 352 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | /* The default css_set - used by init and its children prior to any |
| 356 | * hierarchies being mounted. It contains a pointer to the root state |
| 357 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 358 | * reference-counted, to improve performance when child cgroups |
| 359 | * haven't been created. |
| 360 | */ |
| 361 | |
| 362 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 363 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 364 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 365 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 366 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 367 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 368 | /* css_set_lock protects the list of css_set objects, and the |
| 369 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 370 | * due to cgroup_iter_start() */ |
| 371 | static DEFINE_RWLOCK(css_set_lock); |
| 372 | static int css_set_count; |
| 373 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 374 | /* |
| 375 | * hash table for cgroup groups. This improves the performance to find |
| 376 | * an existing css_set. This hash doesn't (currently) take into |
| 377 | * account cgroups in empty hierarchies. |
| 378 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 379 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 380 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 381 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 382 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
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 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 385 | struct cgroup_subsys *ss; |
| 386 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 387 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 388 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 389 | key += (unsigned long)css[i]; |
| 390 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 391 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 392 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 395 | /* We don't maintain the lists running through each css_set to its |
| 396 | * task until after the first call to cgroup_iter_start(). This |
| 397 | * reduces the fork()/exit() overhead for people who have cgroups |
| 398 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 399 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 400 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 401 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 402 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 403 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 404 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 405 | /* |
| 406 | * Ensure that the refcount doesn't hit zero while any readers |
| 407 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 408 | * rwlock |
| 409 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 410 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 411 | return; |
| 412 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 413 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 414 | write_unlock(&css_set_lock); |
| 415 | return; |
| 416 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 417 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 418 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 419 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 420 | css_set_count--; |
| 421 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 422 | 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] | 423 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 425 | list_del(&link->cset_link); |
| 426 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 427 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 428 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 429 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 430 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 431 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 432 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 433 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 434 | |
| 435 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 436 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 437 | |
| 438 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 439 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* |
| 443 | * refcounted get/put for css_set objects |
| 444 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 445 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 446 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 450 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 451 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 452 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 455 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 456 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 457 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 460 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 461 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 462 | * @cset: candidate css_set being tested |
| 463 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 464 | * @new_cgrp: cgroup that's being entered by the task |
| 465 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 466 | * |
| 467 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 468 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 469 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 470 | static bool compare_css_sets(struct css_set *cset, |
| 471 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 472 | struct cgroup *new_cgrp, |
| 473 | struct cgroup_subsys_state *template[]) |
| 474 | { |
| 475 | struct list_head *l1, *l2; |
| 476 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 477 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | /* Not all subsystems matched */ |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Compare cgroup pointers in order to distinguish between |
| 484 | * different cgroups in heirarchies with no subsystems. We |
| 485 | * could get by with just this check alone (and skip the |
| 486 | * memcmp above) but on most setups the memcmp check will |
| 487 | * avoid the need for this more expensive check on almost all |
| 488 | * candidates. |
| 489 | */ |
| 490 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 491 | l1 = &cset->cgrp_links; |
| 492 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 493 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 494 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 495 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 496 | |
| 497 | l1 = l1->next; |
| 498 | l2 = l2->next; |
| 499 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 500 | if (l1 == &cset->cgrp_links) { |
| 501 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 502 | break; |
| 503 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 504 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 505 | } |
| 506 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 507 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 508 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 509 | cgrp1 = link1->cgrp; |
| 510 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 511 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 512 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * If this hierarchy is the hierarchy of the cgroup |
| 516 | * that's changing, then we need to check that this |
| 517 | * css_set points to the new cgroup; if it's any other |
| 518 | * hierarchy, then this css_set should point to the |
| 519 | * same cgroup as the old css_set. |
| 520 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 521 | if (cgrp1->root == new_cgrp->root) { |
| 522 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 523 | return false; |
| 524 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 525 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 532 | /** |
| 533 | * find_existing_css_set - init css array and find the matching css_set |
| 534 | * @old_cset: the css_set that we're using before the cgroup transition |
| 535 | * @cgrp: the cgroup that we're moving into |
| 536 | * @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] | 537 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 538 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 539 | struct cgroup *cgrp, |
| 540 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 541 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 542 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 543 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 544 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 545 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 546 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 547 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 548 | /* |
| 549 | * Build the set of subsystem state objects that we want to see in the |
| 550 | * new css_set. while subsystems can change globally, the entries here |
| 551 | * won't change, so no need for locking. |
| 552 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 553 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 554 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 555 | /* Subsystem is in this hierarchy. So we want |
| 556 | * the subsystem state from the new |
| 557 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 558 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 559 | } else { |
| 560 | /* Subsystem is not in this hierarchy, so we |
| 561 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 562 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 566 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 567 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 568 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 569 | continue; |
| 570 | |
| 571 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 572 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 573 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 574 | |
| 575 | /* No existing cgroup group matched */ |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 580 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 582 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 584 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 585 | kfree(link); |
| 586 | } |
| 587 | } |
| 588 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 589 | /** |
| 590 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 591 | * @count: the number of links to allocate |
| 592 | * @tmp_links: list_head the allocated links are put on |
| 593 | * |
| 594 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 595 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 596 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 597 | 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] | 598 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 600 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 601 | |
| 602 | INIT_LIST_HEAD(tmp_links); |
| 603 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 604 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 605 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 606 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 608 | return -ENOMEM; |
| 609 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 610 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 615 | /** |
| 616 | * 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] | 617 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 618 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 619 | * @cgrp: the destination cgroup |
| 620 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 621 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 622 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 623 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 624 | struct cgrp_cset_link *link; |
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 | BUG_ON(list_empty(tmp_links)); |
| 627 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 628 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 629 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 630 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 631 | /* |
| 632 | * Always add links to the tail of the list so that the list |
| 633 | * is sorted by order of hierarchy creation |
| 634 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 635 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 638 | /** |
| 639 | * find_css_set - return a new css_set with one cgroup updated |
| 640 | * @old_cset: the baseline css_set |
| 641 | * @cgrp: the cgroup to be updated |
| 642 | * |
| 643 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 644 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 645 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 646 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 647 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 648 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 649 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 651 | struct list_head tmp_links; |
| 652 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 653 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 654 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 655 | lockdep_assert_held(&cgroup_mutex); |
| 656 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 657 | /* First see if we already have a cgroup group that matches |
| 658 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 659 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 660 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 661 | if (cset) |
| 662 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 663 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 664 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 665 | if (cset) |
| 666 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 667 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 668 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 669 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 670 | return NULL; |
| 671 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 672 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 673 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 674 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | return NULL; |
| 676 | } |
| 677 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 678 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 679 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 680 | INIT_LIST_HEAD(&cset->tasks); |
| 681 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 682 | |
| 683 | /* Copy the set of subsystem state objects generated in |
| 684 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 685 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 686 | |
| 687 | write_lock(&css_set_lock); |
| 688 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 689 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 690 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 691 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 692 | if (c->root == cgrp->root) |
| 693 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 694 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 695 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 696 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 697 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 698 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 699 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 700 | |
| 701 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 702 | key = css_set_hash(cset->subsys); |
| 703 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 704 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 705 | write_unlock(&css_set_lock); |
| 706 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 707 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 710 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 711 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 712 | * called with cgroup_mutex held. |
| 713 | */ |
| 714 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 715 | struct cgroupfs_root *root) |
| 716 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 717 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 718 | struct cgroup *res = NULL; |
| 719 | |
| 720 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 721 | read_lock(&css_set_lock); |
| 722 | /* |
| 723 | * No need to lock the task - since we hold cgroup_mutex the |
| 724 | * task can't change groups, so the only thing that can happen |
| 725 | * is that it exits and its css is set back to init_css_set. |
| 726 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 727 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 728 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 729 | res = &root->top_cgroup; |
| 730 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 731 | struct cgrp_cset_link *link; |
| 732 | |
| 733 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 734 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 735 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 736 | if (c->root == root) { |
| 737 | res = c; |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | read_unlock(&css_set_lock); |
| 743 | BUG_ON(!res); |
| 744 | return res; |
| 745 | } |
| 746 | |
| 747 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 748 | * There is one global cgroup mutex. We also require taking |
| 749 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 750 | * See "The task_lock() exception", at the end of this comment. |
| 751 | * |
| 752 | * A task must hold cgroup_mutex to modify cgroups. |
| 753 | * |
| 754 | * Any task can increment and decrement the count field without lock. |
| 755 | * So in general, code holding cgroup_mutex can't rely on the count |
| 756 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 757 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 758 | * means that no tasks are currently attached, therefore there is no |
| 759 | * way a task attached to that cgroup can fork (the other way to |
| 760 | * increment the count). So code holding cgroup_mutex can safely |
| 761 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 762 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 763 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 764 | * needs that mutex. |
| 765 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 766 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 767 | * (usually) take cgroup_mutex. These are the two most performance |
| 768 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 769 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 770 | * 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] | 771 | * to the release agent with the name of the cgroup (path relative to |
| 772 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 773 | * |
| 774 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 775 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 776 | * tasks in the system use _some_ cgroup, and since there is always at |
| 777 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 778 | * always has either children cgroups and/or using tasks. So we don't |
| 779 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 780 | * |
| 781 | * The task_lock() exception |
| 782 | * |
| 783 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 784 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 785 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | * several performance critical places that need to reference |
| 787 | * task->cgroup without the expense of grabbing a system global |
| 788 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 789 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 790 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 791 | * the task_struct routinely used for such matters. |
| 792 | * |
| 793 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 794 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 795 | */ |
| 796 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 797 | /* |
| 798 | * A couple of forward declarations required, due to cyclic reference loop: |
| 799 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 800 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 801 | * -> cgroup_mkdir. |
| 802 | */ |
| 803 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 804 | 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] | 805 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 806 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 807 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 808 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 809 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 810 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 811 | |
| 812 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 813 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 814 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 815 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 816 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 817 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 818 | struct cgroup *parent, struct cgroup *child); |
| 819 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 820 | 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] | 821 | { |
| 822 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 823 | |
| 824 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 825 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 826 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 827 | inode->i_uid = current_fsuid(); |
| 828 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 829 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 830 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 831 | } |
| 832 | return inode; |
| 833 | } |
| 834 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 835 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 836 | { |
| 837 | struct cgroup_name *name; |
| 838 | |
| 839 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 840 | if (!name) |
| 841 | return NULL; |
| 842 | strcpy(name->name, dentry->d_name.name); |
| 843 | return name; |
| 844 | } |
| 845 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 846 | static void cgroup_free_fn(struct work_struct *work) |
| 847 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 848 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 849 | struct cgroup_subsys *ss; |
| 850 | |
| 851 | mutex_lock(&cgroup_mutex); |
| 852 | /* |
| 853 | * Release the subsystem state objects. |
| 854 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 855 | for_each_root_subsys(cgrp->root, ss) |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 856 | ss->css_free(cgrp); |
| 857 | |
| 858 | cgrp->root->number_of_cgroups--; |
| 859 | mutex_unlock(&cgroup_mutex); |
| 860 | |
| 861 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 862 | * We get a ref to the parent's dentry, and put the ref when |
| 863 | * this cgroup is being freed, so it's guaranteed that the |
| 864 | * parent won't be destroyed before its children. |
| 865 | */ |
| 866 | dput(cgrp->parent->dentry); |
| 867 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 868 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 869 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 870 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 871 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 872 | * created the cgroup. This will free cgrp->root, if we are |
| 873 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 874 | */ |
| 875 | deactivate_super(cgrp->root->sb); |
| 876 | |
| 877 | /* |
| 878 | * if we're getting rid of the cgroup, refcount should ensure |
| 879 | * that there are no pidlists left. |
| 880 | */ |
| 881 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 882 | |
| 883 | simple_xattrs_free(&cgrp->xattrs); |
| 884 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 885 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 886 | kfree(cgrp); |
| 887 | } |
| 888 | |
| 889 | static void cgroup_free_rcu(struct rcu_head *head) |
| 890 | { |
| 891 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 892 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 893 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 894 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 895 | } |
| 896 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 897 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 898 | { |
| 899 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 900 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 901 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 902 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 903 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 904 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 905 | } else { |
| 906 | struct cfent *cfe = __d_cfe(dentry); |
| 907 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 908 | |
| 909 | WARN_ONCE(!list_empty(&cfe->node) && |
| 910 | cgrp != &cgrp->root->top_cgroup, |
| 911 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 912 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 913 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 914 | } |
| 915 | iput(inode); |
| 916 | } |
| 917 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 918 | static int cgroup_delete(const struct dentry *d) |
| 919 | { |
| 920 | return 1; |
| 921 | } |
| 922 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 923 | static void remove_dir(struct dentry *d) |
| 924 | { |
| 925 | struct dentry *parent = dget(d->d_parent); |
| 926 | |
| 927 | d_delete(d); |
| 928 | simple_rmdir(parent->d_inode, d); |
| 929 | dput(parent); |
| 930 | } |
| 931 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 932 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 933 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 934 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 935 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 936 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 937 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 938 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 939 | /* |
| 940 | * If we're doing cleanup due to failure of cgroup_create(), |
| 941 | * the corresponding @cfe may not exist. |
| 942 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 943 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 944 | struct dentry *d = cfe->dentry; |
| 945 | |
| 946 | if (cft && cfe->type != cft) |
| 947 | continue; |
| 948 | |
| 949 | dget(d); |
| 950 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 951 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 952 | list_del_init(&cfe->node); |
| 953 | dput(d); |
| 954 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 955 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 956 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 959 | /** |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 960 | * cgroup_clear_dir - selective removal of base and subsystem files |
| 961 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 962 | * @base_files: true if the base files should be removed |
| 963 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 964 | */ |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 965 | static void cgroup_clear_dir(struct cgroup *cgrp, bool base_files, |
| 966 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 967 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 968 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 969 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 970 | for_each_root_subsys(cgrp->root, ss) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 971 | struct cftype_set *set; |
| 972 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 973 | continue; |
| 974 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 975 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 976 | } |
| 977 | if (base_files) { |
| 978 | while (!list_empty(&cgrp->files)) |
| 979 | cgroup_rm_file(cgrp, NULL); |
| 980 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* |
| 984 | * NOTE : the dentry must have been dget()'ed |
| 985 | */ |
| 986 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 987 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 988 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 989 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 990 | parent = dentry->d_parent; |
| 991 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 992 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 994 | spin_unlock(&dentry->d_lock); |
| 995 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | remove_dir(dentry); |
| 997 | } |
| 998 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 999 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1000 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1001 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1002 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1003 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1004 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1005 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1007 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1008 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | int i; |
| 1010 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1011 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1012 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1013 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1014 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1015 | for_each_subsys(ss, i) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1016 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1017 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1018 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1019 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1020 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1021 | if (ss->root != &cgroup_dummy_root) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1022 | /* Subsystem isn't free */ |
| 1023 | return -EBUSY; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | /* Currently we don't handle adding/removing subsystems when |
| 1028 | * any child cgroups exist. This is theoretically supportable |
| 1029 | * but involves complex error handling, so it's being left until |
| 1030 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1031 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1032 | return -EBUSY; |
| 1033 | |
| 1034 | /* Process each subsystem */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1035 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1036 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1037 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1038 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1040 | BUG_ON(cgrp->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1041 | BUG_ON(!cgroup_dummy_top->subsys[i]); |
| 1042 | BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1043 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1044 | cgrp->subsys[i] = cgroup_dummy_top->subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1045 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1046 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1047 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1048 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1049 | ss->bind(cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1050 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1051 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1052 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1053 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1054 | /* We're removing this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1055 | BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1056 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1057 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | if (ss->bind) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1059 | ss->bind(cgroup_dummy_top); |
| 1060 | cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1061 | cgrp->subsys[i] = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1062 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1063 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1064 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1065 | /* subsystem is now free - drop reference on module */ |
| 1066 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1067 | root->subsys_mask &= ~bit; |
| 1068 | } else if (bit & root->subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1069 | /* Subsystem state should already exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1070 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1071 | /* |
| 1072 | * a refcount was taken, but we already had one, so |
| 1073 | * drop the extra reference. |
| 1074 | */ |
| 1075 | module_put(ss->module); |
| 1076 | #ifdef CONFIG_MODULE_UNLOAD |
| 1077 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1078 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | } else { |
| 1080 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1081 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1087 | * now matches the bound subsystems. |
| 1088 | */ |
| 1089 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1090 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1094 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1095 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1096 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | struct cgroup_subsys *ss; |
| 1098 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1099 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1100 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1102 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1103 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1104 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1105 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1106 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1107 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1108 | if (strlen(root->release_agent_path)) |
| 1109 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1110 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1111 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1112 | if (strlen(root->name)) |
| 1113 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1114 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1119 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1120 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1121 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1122 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1123 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1124 | /* User explicitly requested empty subsystem */ |
| 1125 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1126 | |
| 1127 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1128 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1129 | }; |
| 1130 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1131 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1132 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1133 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1134 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1135 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1136 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1137 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1138 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1139 | char *token, *o = data; |
| 1140 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1141 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1142 | bool module_pin_failed = false; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1143 | struct cgroup_subsys *ss; |
| 1144 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1145 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1146 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1147 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1148 | #ifdef CONFIG_CPUSETS |
| 1149 | mask = ~(1UL << cpuset_subsys_id); |
| 1150 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1151 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1152 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1153 | |
| 1154 | while ((token = strsep(&o, ",")) != NULL) { |
| 1155 | if (!*token) |
| 1156 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1157 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1158 | /* Explicitly have no subsystems */ |
| 1159 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1160 | continue; |
| 1161 | } |
| 1162 | if (!strcmp(token, "all")) { |
| 1163 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1164 | if (one_ss) |
| 1165 | return -EINVAL; |
| 1166 | all_ss = true; |
| 1167 | continue; |
| 1168 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1169 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1170 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1171 | continue; |
| 1172 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1173 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1174 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1175 | continue; |
| 1176 | } |
| 1177 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1178 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1179 | continue; |
| 1180 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1181 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1182 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1185 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1186 | /* Specifying two release agents is forbidden */ |
| 1187 | if (opts->release_agent) |
| 1188 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1189 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1190 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1191 | if (!opts->release_agent) |
| 1192 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1193 | continue; |
| 1194 | } |
| 1195 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1196 | const char *name = token + 5; |
| 1197 | /* Can't specify an empty name */ |
| 1198 | if (!strlen(name)) |
| 1199 | return -EINVAL; |
| 1200 | /* Must match [\w.-]+ */ |
| 1201 | for (i = 0; i < strlen(name); i++) { |
| 1202 | char c = name[i]; |
| 1203 | if (isalnum(c)) |
| 1204 | continue; |
| 1205 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1206 | continue; |
| 1207 | return -EINVAL; |
| 1208 | } |
| 1209 | /* Specifying two names is forbidden */ |
| 1210 | if (opts->name) |
| 1211 | return -EINVAL; |
| 1212 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1213 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1214 | GFP_KERNEL); |
| 1215 | if (!opts->name) |
| 1216 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1217 | |
| 1218 | continue; |
| 1219 | } |
| 1220 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1221 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1222 | if (strcmp(token, ss->name)) |
| 1223 | continue; |
| 1224 | if (ss->disabled) |
| 1225 | continue; |
| 1226 | |
| 1227 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1228 | if (all_ss) |
| 1229 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1230 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1231 | one_ss = true; |
| 1232 | |
| 1233 | break; |
| 1234 | } |
| 1235 | if (i == CGROUP_SUBSYS_COUNT) |
| 1236 | return -ENOENT; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1241 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1242 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1243 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1244 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1245 | for_each_subsys(ss, i) |
| 1246 | if (!ss->disabled) |
| 1247 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1248 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1249 | /* Consistency checks */ |
| 1250 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1251 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1252 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1253 | |
| 1254 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1255 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1256 | return -EINVAL; |
| 1257 | } |
| 1258 | |
| 1259 | if (opts->cpuset_clone_children) { |
| 1260 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1261 | return -EINVAL; |
| 1262 | } |
| 1263 | } |
| 1264 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * Option noprefix was introduced just for backward compatibility |
| 1267 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1268 | * the cpuset subsystem. |
| 1269 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1270 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1271 | return -EINVAL; |
| 1272 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1275 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1276 | return -EINVAL; |
| 1277 | |
| 1278 | /* |
| 1279 | * We either have to specify by name or by subsystems. (So all |
| 1280 | * empty hierarchies must have a name). |
| 1281 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1282 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1283 | return -EINVAL; |
| 1284 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | /* |
| 1286 | * Grab references on all the modules we'll need, so the subsystems |
| 1287 | * don't dance around before rebind_subsystems attaches them. This may |
| 1288 | * take duplicate reference counts on a subsystem that's already used, |
| 1289 | * but rebind_subsystems handles this case. |
| 1290 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1291 | for_each_subsys(ss, i) { |
| 1292 | if (!(opts->subsys_mask & (1UL << i))) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1293 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1294 | if (!try_module_get(cgroup_subsys[i]->module)) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1295 | module_pin_failed = true; |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | if (module_pin_failed) { |
| 1300 | /* |
| 1301 | * oops, one of the modules was going away. this means that we |
| 1302 | * raced with a module_delete call, and to the user this is |
| 1303 | * essentially a "subsystem doesn't exist" case. |
| 1304 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1305 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1306 | /* drop refcounts only on the ones we took */ |
| 1307 | unsigned long bit = 1UL << i; |
| 1308 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1309 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1310 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1311 | module_put(cgroup_subsys[i]->module); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | } |
| 1313 | return -ENOENT; |
| 1314 | } |
| 1315 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1319 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1320 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1321 | struct cgroup_subsys *ss; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1322 | int i; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1323 | |
Tejun Heo | eb178d0 | 2013-06-25 18:05:21 -0700 | [diff] [blame] | 1324 | mutex_lock(&cgroup_mutex); |
| 1325 | for_each_subsys(ss, i) |
| 1326 | if (subsys_mask & (1UL << i)) |
| 1327 | module_put(cgroup_subsys[i]->module); |
| 1328 | mutex_unlock(&cgroup_mutex); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1331 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1332 | { |
| 1333 | int ret = 0; |
| 1334 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1335 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1336 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1337 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1338 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1339 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1340 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1341 | return -EINVAL; |
| 1342 | } |
| 1343 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1344 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1345 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1346 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* See what subsystems are wanted */ |
| 1349 | ret = parse_cgroupfs_options(data, &opts); |
| 1350 | if (ret) |
| 1351 | goto out_unlock; |
| 1352 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1353 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1354 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1355 | task_tgid_nr(current), current->comm); |
| 1356 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1357 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1358 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1359 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1360 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1361 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1362 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1363 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1364 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1365 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1366 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1367 | goto out_unlock; |
| 1368 | } |
| 1369 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1370 | /* |
| 1371 | * Clear out the files of subsystems that should be removed, do |
| 1372 | * this before rebind_subsystems, since rebind_subsystems may |
| 1373 | * change this hierarchy's subsys_list. |
| 1374 | */ |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 1375 | cgroup_clear_dir(cgrp, false, removed_mask); |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1376 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1377 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1378 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1379 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1380 | cgroup_populate_dir(cgrp, false, removed_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1381 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1382 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1383 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1384 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1385 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1386 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1387 | if (opts.release_agent) |
| 1388 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1389 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1390 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1391 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1392 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1393 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1394 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Tejun Heo | e2bd416 | 2013-06-27 19:37:23 -0700 | [diff] [blame] | 1395 | if (ret) |
| 1396 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | return ret; |
| 1398 | } |
| 1399 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1400 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1401 | .statfs = simple_statfs, |
| 1402 | .drop_inode = generic_delete_inode, |
| 1403 | .show_options = cgroup_show_options, |
| 1404 | .remount_fs = cgroup_remount, |
| 1405 | }; |
| 1406 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1407 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1408 | { |
| 1409 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1410 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1411 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1412 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1413 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1414 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1415 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1416 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1417 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1418 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1419 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1420 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1422 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1423 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1424 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1425 | INIT_LIST_HEAD(&root->subsys_list); |
| 1426 | INIT_LIST_HEAD(&root->root_list); |
| 1427 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1428 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1429 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1430 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1433 | 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] | 1434 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1435 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1436 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1437 | lockdep_assert_held(&cgroup_mutex); |
| 1438 | lockdep_assert_held(&cgroup_root_mutex); |
| 1439 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1440 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1441 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1442 | if (id < 0) |
| 1443 | return id; |
| 1444 | |
| 1445 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1446 | return 0; |
| 1447 | } |
| 1448 | |
| 1449 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1450 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1451 | lockdep_assert_held(&cgroup_mutex); |
| 1452 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1453 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1454 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1455 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1456 | root->hierarchy_id = 0; |
| 1457 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1460 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1461 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1462 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1463 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1464 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1465 | /* If we asked for a name then it must match */ |
| 1466 | if (opts->name && strcmp(opts->name, root->name)) |
| 1467 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1468 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1469 | /* |
| 1470 | * If we asked for subsystems (or explicitly for no |
| 1471 | * subsystems) then they must match |
| 1472 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1473 | if ((opts->subsys_mask || opts->none) |
| 1474 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1475 | return 0; |
| 1476 | |
| 1477 | return 1; |
| 1478 | } |
| 1479 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1480 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1481 | { |
| 1482 | struct cgroupfs_root *root; |
| 1483 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1484 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1485 | return NULL; |
| 1486 | |
| 1487 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1488 | if (!root) |
| 1489 | return ERR_PTR(-ENOMEM); |
| 1490 | |
| 1491 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1492 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1493 | /* |
| 1494 | * We need to set @root->subsys_mask now so that @root can be |
| 1495 | * matched by cgroup_test_super() before it finishes |
| 1496 | * initialization; otherwise, competing mounts with the same |
| 1497 | * options may try to bind the same subsystems instead of waiting |
| 1498 | * for the first one leading to unexpected mount errors. |
| 1499 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1500 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1501 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1502 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1503 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1504 | if (opts->release_agent) |
| 1505 | strcpy(root->release_agent_path, opts->release_agent); |
| 1506 | if (opts->name) |
| 1507 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1508 | if (opts->cpuset_clone_children) |
| 1509 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1510 | return root; |
| 1511 | } |
| 1512 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1513 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1514 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1515 | if (root) { |
| 1516 | /* hierarhcy ID shoulid already have been released */ |
| 1517 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1518 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1519 | ida_destroy(&root->cgroup_ida); |
| 1520 | kfree(root); |
| 1521 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1524 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1525 | { |
| 1526 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1527 | struct cgroup_sb_opts *opts = data; |
| 1528 | |
| 1529 | /* If we don't have a new root, we can't set up a new sb */ |
| 1530 | if (!opts->new_root) |
| 1531 | return -EINVAL; |
| 1532 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1533 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1534 | |
| 1535 | ret = set_anon_super(sb, NULL); |
| 1536 | if (ret) |
| 1537 | return ret; |
| 1538 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1539 | sb->s_fs_info = opts->new_root; |
| 1540 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1541 | |
| 1542 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1543 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1544 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1545 | sb->s_op = &cgroup_ops; |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1551 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1552 | static const struct dentry_operations cgroup_dops = { |
| 1553 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1554 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1555 | }; |
| 1556 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | struct inode *inode = |
| 1558 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | |
| 1560 | if (!inode) |
| 1561 | return -ENOMEM; |
| 1562 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1563 | inode->i_fop = &simple_dir_operations; |
| 1564 | inode->i_op = &cgroup_dir_inode_operations; |
| 1565 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1566 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1567 | sb->s_root = d_make_root(inode); |
| 1568 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1569 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1570 | /* for everything else we want ->d_op set */ |
| 1571 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1572 | return 0; |
| 1573 | } |
| 1574 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1575 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1577 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1578 | { |
| 1579 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1580 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1581 | int ret = 0; |
| 1582 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1583 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1584 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1587 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1589 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1590 | if (ret) |
| 1591 | goto out_err; |
| 1592 | |
| 1593 | /* |
| 1594 | * Allocate a new cgroup root. We may not need it if we're |
| 1595 | * reusing an existing hierarchy. |
| 1596 | */ |
| 1597 | new_root = cgroup_root_from_opts(&opts); |
| 1598 | if (IS_ERR(new_root)) { |
| 1599 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1600 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1601 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1602 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1603 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1604 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1605 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1606 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1607 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1608 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1609 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1612 | root = sb->s_fs_info; |
| 1613 | BUG_ON(!root); |
| 1614 | if (root == opts.new_root) { |
| 1615 | /* We used the new root structure, so this is a new hierarchy */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1616 | struct list_head tmp_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1617 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1618 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1619 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1620 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1621 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1622 | |
| 1623 | BUG_ON(sb->s_root != NULL); |
| 1624 | |
| 1625 | ret = cgroup_get_rootdir(sb); |
| 1626 | if (ret) |
| 1627 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1628 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1629 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1630 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1632 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1633 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1634 | /* Check for name clashes with existing mounts */ |
| 1635 | ret = -EBUSY; |
| 1636 | if (strlen(root->name)) |
| 1637 | for_each_active_root(existing_root) |
| 1638 | if (!strcmp(existing_root->name, root->name)) |
| 1639 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1640 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1641 | /* |
| 1642 | * We're accessing css_set_count without locking |
| 1643 | * css_set_lock here, but that's OK - it can only be |
| 1644 | * increased by someone holding cgroup_lock, and |
| 1645 | * that's us. The worst that can happen is that we |
| 1646 | * have some link structures left over |
| 1647 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1648 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1649 | if (ret) |
| 1650 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1651 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1652 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1653 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1654 | if (ret) |
| 1655 | goto unlock_drop; |
| 1656 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1657 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1658 | if (ret == -EBUSY) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1659 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1660 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1661 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1662 | /* |
| 1663 | * There must be no failure case after here, since rebinding |
| 1664 | * takes care of subsystems' refcounts, which are explicitly |
| 1665 | * dropped in the failure exit path. |
| 1666 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1667 | |
| 1668 | /* EBUSY should be the only error here */ |
| 1669 | BUG_ON(ret); |
| 1670 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1671 | list_add(&root->root_list, &cgroup_roots); |
| 1672 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1673 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1674 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | root->top_cgroup.dentry = sb->s_root; |
| 1676 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1677 | /* Link the top cgroup in this hierarchy into all |
| 1678 | * the css_set objects */ |
| 1679 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1680 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1681 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1682 | write_unlock(&css_set_lock); |
| 1683 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1684 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1685 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1686 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1687 | BUG_ON(root->number_of_cgroups != 1); |
| 1688 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1689 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1690 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1691 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1692 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1693 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1694 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1695 | } else { |
| 1696 | /* |
| 1697 | * We re-used an existing hierarchy - the new root (if |
| 1698 | * any) is not needed |
| 1699 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1700 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1701 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1702 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1703 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1704 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1705 | ret = -EINVAL; |
| 1706 | goto drop_new_super; |
| 1707 | } else { |
| 1708 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1709 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1712 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1713 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1716 | kfree(opts.release_agent); |
| 1717 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1718 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1720 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1721 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1722 | mutex_unlock(&cgroup_root_mutex); |
| 1723 | mutex_unlock(&cgroup_mutex); |
| 1724 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1726 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1727 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1728 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1729 | out_err: |
| 1730 | kfree(opts.release_agent); |
| 1731 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1732 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1736 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1737 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1738 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1739 | int ret; |
| 1740 | |
| 1741 | BUG_ON(!root); |
| 1742 | |
| 1743 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1744 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1745 | |
| 1746 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1747 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1748 | |
| 1749 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1750 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1751 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1752 | /* Shouldn't be able to fail ... */ |
| 1753 | BUG_ON(ret); |
| 1754 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1755 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1756 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1757 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1758 | * root cgroup |
| 1759 | */ |
| 1760 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1761 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1762 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1763 | list_del(&link->cset_link); |
| 1764 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1765 | kfree(link); |
| 1766 | } |
| 1767 | write_unlock(&css_set_lock); |
| 1768 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1769 | if (!list_empty(&root->root_list)) { |
| 1770 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1771 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1772 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1773 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1774 | cgroup_exit_root_id(root); |
| 1775 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1776 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1777 | mutex_unlock(&cgroup_mutex); |
| 1778 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1779 | simple_xattrs_free(&cgrp->xattrs); |
| 1780 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1781 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1782 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | static struct file_system_type cgroup_fs_type = { |
| 1786 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1787 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1788 | .kill_sb = cgroup_kill_sb, |
| 1789 | }; |
| 1790 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1791 | static struct kobject *cgroup_kobj; |
| 1792 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1793 | /** |
| 1794 | * cgroup_path - generate the path of a cgroup |
| 1795 | * @cgrp: the cgroup in question |
| 1796 | * @buf: the buffer to write the path into |
| 1797 | * @buflen: the length of the buffer |
| 1798 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1799 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1800 | * |
| 1801 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1802 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1803 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1804 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1805 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1806 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1807 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1808 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1809 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1810 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1811 | if (!cgrp->parent) { |
| 1812 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1813 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1817 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1818 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1819 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1820 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1821 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1822 | const char *name = cgroup_name(cgrp); |
| 1823 | int len; |
| 1824 | |
| 1825 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1826 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1827 | goto out; |
| 1828 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1829 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1830 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1831 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1832 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1833 | |
| 1834 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1835 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1836 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1837 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1838 | out: |
| 1839 | rcu_read_unlock(); |
| 1840 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1841 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1842 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1843 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1844 | /** |
| 1845 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1846 | * @task: target task |
| 1847 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1848 | * @buf: the buffer to write the path into |
| 1849 | * @buflen: the length of the buffer |
| 1850 | * |
| 1851 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1852 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1853 | * be used inside locks used by cgroup controller callbacks. |
| 1854 | */ |
| 1855 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1856 | char *buf, size_t buflen) |
| 1857 | { |
| 1858 | struct cgroupfs_root *root; |
| 1859 | struct cgroup *cgrp = NULL; |
| 1860 | int ret = -ENOENT; |
| 1861 | |
| 1862 | mutex_lock(&cgroup_mutex); |
| 1863 | |
| 1864 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1865 | if (root) { |
| 1866 | cgrp = task_cgroup_from_root(task, root); |
| 1867 | ret = cgroup_path(cgrp, buf, buflen); |
| 1868 | } |
| 1869 | |
| 1870 | mutex_unlock(&cgroup_mutex); |
| 1871 | |
| 1872 | return ret; |
| 1873 | } |
| 1874 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1875 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1876 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1877 | * Control Group taskset |
| 1878 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1879 | struct task_and_cgroup { |
| 1880 | struct task_struct *task; |
| 1881 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1882 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1883 | }; |
| 1884 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1885 | struct cgroup_taskset { |
| 1886 | struct task_and_cgroup single; |
| 1887 | struct flex_array *tc_array; |
| 1888 | int tc_array_len; |
| 1889 | int idx; |
| 1890 | struct cgroup *cur_cgrp; |
| 1891 | }; |
| 1892 | |
| 1893 | /** |
| 1894 | * cgroup_taskset_first - reset taskset and return the first task |
| 1895 | * @tset: taskset of interest |
| 1896 | * |
| 1897 | * @tset iteration is initialized and the first task is returned. |
| 1898 | */ |
| 1899 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1900 | { |
| 1901 | if (tset->tc_array) { |
| 1902 | tset->idx = 0; |
| 1903 | return cgroup_taskset_next(tset); |
| 1904 | } else { |
| 1905 | tset->cur_cgrp = tset->single.cgrp; |
| 1906 | return tset->single.task; |
| 1907 | } |
| 1908 | } |
| 1909 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1910 | |
| 1911 | /** |
| 1912 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1913 | * @tset: taskset of interest |
| 1914 | * |
| 1915 | * Return the next task in @tset. Iteration must have been initialized |
| 1916 | * with cgroup_taskset_first(). |
| 1917 | */ |
| 1918 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1919 | { |
| 1920 | struct task_and_cgroup *tc; |
| 1921 | |
| 1922 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1923 | return NULL; |
| 1924 | |
| 1925 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1926 | tset->cur_cgrp = tc->cgrp; |
| 1927 | return tc->task; |
| 1928 | } |
| 1929 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1930 | |
| 1931 | /** |
| 1932 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1933 | * @tset: taskset of interest |
| 1934 | * |
| 1935 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1936 | * function must be preceded by either cgroup_taskset_first() or |
| 1937 | * cgroup_taskset_next(). |
| 1938 | */ |
| 1939 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1940 | { |
| 1941 | return tset->cur_cgrp; |
| 1942 | } |
| 1943 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1944 | |
| 1945 | /** |
| 1946 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1947 | * @tset: taskset of interest |
| 1948 | */ |
| 1949 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1950 | { |
| 1951 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1952 | } |
| 1953 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1954 | |
| 1955 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1956 | /* |
| 1957 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1958 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1959 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1960 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1961 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1962 | struct task_struct *tsk, |
| 1963 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1964 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1965 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1968 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1969 | * setting such that we can't race against cgroup_exit() changing the |
| 1970 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1971 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1972 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1973 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1976 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1977 | task_unlock(tsk); |
| 1978 | |
| 1979 | /* Update the css_set linked lists if we're using them */ |
| 1980 | write_lock(&css_set_lock); |
| 1981 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1982 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1983 | write_unlock(&css_set_lock); |
| 1984 | |
| 1985 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1986 | * We just gained a reference on old_cset by taking it from the |
| 1987 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1988 | * 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] | 1989 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1990 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1991 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1994 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1995 | * 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] | 1996 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1997 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1998 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1999 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2000 | * 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] | 2001 | * 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] | 2002 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2003 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 2004 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2005 | { |
| 2006 | int retval, i, group_size; |
| 2007 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2008 | struct cgroupfs_root *root = cgrp->root; |
| 2009 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2010 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2011 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2012 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2013 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2014 | |
| 2015 | /* |
| 2016 | * step 0: in order to do expensive, possibly blocking operations for |
| 2017 | * every thread, we cannot iterate the thread group list, since it needs |
| 2018 | * 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] | 2019 | * group - group_rwsem prevents new threads from appearing, and if |
| 2020 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2021 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2022 | if (threadgroup) |
| 2023 | group_size = get_nr_threads(tsk); |
| 2024 | else |
| 2025 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2026 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2027 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2028 | if (!group) |
| 2029 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2030 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2031 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2032 | if (retval) |
| 2033 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2034 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2036 | /* |
| 2037 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2038 | * already PF_EXITING could be freed from underneath us unless we |
| 2039 | * take an rcu_read_lock. |
| 2040 | */ |
| 2041 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2043 | struct task_and_cgroup ent; |
| 2044 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2045 | /* @tsk either already exited or can't exit until the end */ |
| 2046 | if (tsk->flags & PF_EXITING) |
| 2047 | continue; |
| 2048 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2050 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2051 | ent.task = tsk; |
| 2052 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2053 | /* nothing to do if this task is already in the cgroup */ |
| 2054 | if (ent.cgrp == cgrp) |
| 2055 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2056 | /* |
| 2057 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2058 | * earlier, but it's good form to communicate our expectations. |
| 2059 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2060 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2061 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2062 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2063 | |
| 2064 | if (!threadgroup) |
| 2065 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2066 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2067 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2068 | /* remember the number of threads in the array for later. */ |
| 2069 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2070 | tset.tc_array = group; |
| 2071 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2072 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2073 | /* methods shouldn't be called if no task is actually migrating */ |
| 2074 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2075 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2076 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2077 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2078 | /* |
| 2079 | * step 1: check that we can legitimately attach to the cgroup. |
| 2080 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2081 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2083 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | if (retval) { |
| 2085 | failed_ss = ss; |
| 2086 | goto out_cancel_attach; |
| 2087 | } |
| 2088 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | /* |
| 2092 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2093 | * we use find_css_set, which allocates a new one if necessary. |
| 2094 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2095 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2096 | struct css_set *old_cset; |
| 2097 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2098 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2099 | old_cset = task_css_set(tc->task); |
| 2100 | tc->cg = find_css_set(old_cset, cgrp); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2101 | if (!tc->cg) { |
| 2102 | retval = -ENOMEM; |
| 2103 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2108 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2109 | * proceed to move all tasks to the new cgroup. There are no |
| 2110 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2113 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2114 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2115 | } |
| 2116 | /* nothing is sensitive to fork() after this point. */ |
| 2117 | |
| 2118 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2119 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2120 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2121 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2123 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * step 5: success! and cleanup |
| 2128 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2129 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2130 | out_put_css_set_refs: |
| 2131 | if (retval) { |
| 2132 | for (i = 0; i < group_size; i++) { |
| 2133 | tc = flex_array_get(group, i); |
| 2134 | if (!tc->cg) |
| 2135 | break; |
| 2136 | put_css_set(tc->cg); |
| 2137 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2138 | } |
| 2139 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2140 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2141 | for_each_root_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2142 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2143 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2144 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2145 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2146 | } |
| 2147 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2148 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2149 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2150 | return retval; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * 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] | 2155 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2156 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2157 | */ |
| 2158 | 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] | 2159 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2160 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2161 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2162 | int ret; |
| 2163 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2164 | if (!cgroup_lock_live_group(cgrp)) |
| 2165 | return -ENODEV; |
| 2166 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2167 | retry_find_task: |
| 2168 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2169 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2170 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2171 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2172 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2173 | ret= -ESRCH; |
| 2174 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2175 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2176 | /* |
| 2177 | * even if we're attaching all tasks in the thread group, we |
| 2178 | * only need to check permissions on one of them. |
| 2179 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2180 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2181 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2182 | !uid_eq(cred->euid, tcred->uid) && |
| 2183 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2184 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2185 | ret = -EACCES; |
| 2186 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2187 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2188 | } else |
| 2189 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2190 | |
| 2191 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2192 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2193 | |
| 2194 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2195 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2196 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2197 | * with no rt_runtime allocated. Just say no. |
| 2198 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2199 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2200 | ret = -EINVAL; |
| 2201 | rcu_read_unlock(); |
| 2202 | goto out_unlock_cgroup; |
| 2203 | } |
| 2204 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2205 | get_task_struct(tsk); |
| 2206 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2207 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2208 | threadgroup_lock(tsk); |
| 2209 | if (threadgroup) { |
| 2210 | if (!thread_group_leader(tsk)) { |
| 2211 | /* |
| 2212 | * a race with de_thread from another thread's exec() |
| 2213 | * may strip us of our leadership, if this happens, |
| 2214 | * there is no choice but to throw this task away and |
| 2215 | * try again; this is |
| 2216 | * "double-double-toil-and-trouble-check locking". |
| 2217 | */ |
| 2218 | threadgroup_unlock(tsk); |
| 2219 | put_task_struct(tsk); |
| 2220 | goto retry_find_task; |
| 2221 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2225 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2226 | threadgroup_unlock(tsk); |
| 2227 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2228 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2229 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2230 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2231 | return ret; |
| 2232 | } |
| 2233 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2234 | /** |
| 2235 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2236 | * @from: attach to all cgroups of a given task |
| 2237 | * @tsk: the task to be attached |
| 2238 | */ |
| 2239 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2240 | { |
| 2241 | struct cgroupfs_root *root; |
| 2242 | int retval = 0; |
| 2243 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2244 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2245 | for_each_active_root(root) { |
| 2246 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2247 | |
| 2248 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2249 | if (retval) |
| 2250 | break; |
| 2251 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2252 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2253 | |
| 2254 | return retval; |
| 2255 | } |
| 2256 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2257 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2258 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2259 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2260 | return attach_task_by_pid(cgrp, pid, false); |
| 2261 | } |
| 2262 | |
| 2263 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2264 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2265 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2268 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2269 | const char *buffer) |
| 2270 | { |
| 2271 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2272 | if (strlen(buffer) >= PATH_MAX) |
| 2273 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2274 | if (!cgroup_lock_live_group(cgrp)) |
| 2275 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2276 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2277 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2278 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2279 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2284 | struct seq_file *seq) |
| 2285 | { |
| 2286 | if (!cgroup_lock_live_group(cgrp)) |
| 2287 | return -ENODEV; |
| 2288 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2289 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2290 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2291 | return 0; |
| 2292 | } |
| 2293 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2294 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2295 | struct seq_file *seq) |
| 2296 | { |
| 2297 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2298 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2301 | /* A buffer size big enough for numbers or short strings */ |
| 2302 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2303 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2304 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2305 | struct file *file, |
| 2306 | const char __user *userbuf, |
| 2307 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2308 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2309 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2310 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2311 | char *end; |
| 2312 | |
| 2313 | if (!nbytes) |
| 2314 | return -EINVAL; |
| 2315 | if (nbytes >= sizeof(buffer)) |
| 2316 | return -E2BIG; |
| 2317 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2318 | return -EFAULT; |
| 2319 | |
| 2320 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2321 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2322 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2323 | if (*end) |
| 2324 | return -EINVAL; |
| 2325 | retval = cft->write_u64(cgrp, cft, val); |
| 2326 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2327 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2328 | if (*end) |
| 2329 | return -EINVAL; |
| 2330 | retval = cft->write_s64(cgrp, cft, val); |
| 2331 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2332 | if (!retval) |
| 2333 | retval = nbytes; |
| 2334 | return retval; |
| 2335 | } |
| 2336 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2337 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2338 | struct file *file, |
| 2339 | const char __user *userbuf, |
| 2340 | size_t nbytes, loff_t *unused_ppos) |
| 2341 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2342 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2343 | int retval = 0; |
| 2344 | size_t max_bytes = cft->max_write_len; |
| 2345 | char *buffer = local_buffer; |
| 2346 | |
| 2347 | if (!max_bytes) |
| 2348 | max_bytes = sizeof(local_buffer) - 1; |
| 2349 | if (nbytes >= max_bytes) |
| 2350 | return -E2BIG; |
| 2351 | /* Allocate a dynamic buffer if we need one */ |
| 2352 | if (nbytes >= sizeof(local_buffer)) { |
| 2353 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2354 | if (buffer == NULL) |
| 2355 | return -ENOMEM; |
| 2356 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2357 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2358 | retval = -EFAULT; |
| 2359 | goto out; |
| 2360 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2361 | |
| 2362 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2363 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2364 | if (!retval) |
| 2365 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2366 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2367 | if (buffer != local_buffer) |
| 2368 | kfree(buffer); |
| 2369 | return retval; |
| 2370 | } |
| 2371 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2372 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2373 | size_t nbytes, loff_t *ppos) |
| 2374 | { |
| 2375 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2376 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2377 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2378 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2379 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2380 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2381 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2382 | if (cft->write_u64 || cft->write_s64) |
| 2383 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2384 | if (cft->write_string) |
| 2385 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2386 | if (cft->trigger) { |
| 2387 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2388 | return ret ? ret : nbytes; |
| 2389 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2390 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2393 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2394 | struct file *file, |
| 2395 | char __user *buf, size_t nbytes, |
| 2396 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2397 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2398 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2399 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2400 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2401 | |
| 2402 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2403 | } |
| 2404 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2405 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2406 | struct file *file, |
| 2407 | char __user *buf, size_t nbytes, |
| 2408 | loff_t *ppos) |
| 2409 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2410 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2411 | s64 val = cft->read_s64(cgrp, cft); |
| 2412 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2413 | |
| 2414 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2415 | } |
| 2416 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2417 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2418 | size_t nbytes, loff_t *ppos) |
| 2419 | { |
| 2420 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2421 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2422 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2423 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2424 | return -ENODEV; |
| 2425 | |
| 2426 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2427 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2428 | if (cft->read_u64) |
| 2429 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2430 | if (cft->read_s64) |
| 2431 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2432 | return -EINVAL; |
| 2433 | } |
| 2434 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2435 | /* |
| 2436 | * seqfile ops/methods for returning structured data. Currently just |
| 2437 | * supports string->u64 maps, but can be extended in future. |
| 2438 | */ |
| 2439 | |
| 2440 | struct cgroup_seqfile_state { |
| 2441 | struct cftype *cft; |
| 2442 | struct cgroup *cgroup; |
| 2443 | }; |
| 2444 | |
| 2445 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2446 | { |
| 2447 | struct seq_file *sf = cb->state; |
| 2448 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2449 | } |
| 2450 | |
| 2451 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2452 | { |
| 2453 | struct cgroup_seqfile_state *state = m->private; |
| 2454 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2455 | if (cft->read_map) { |
| 2456 | struct cgroup_map_cb cb = { |
| 2457 | .fill = cgroup_map_add, |
| 2458 | .state = m, |
| 2459 | }; |
| 2460 | return cft->read_map(state->cgroup, cft, &cb); |
| 2461 | } |
| 2462 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2465 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2466 | { |
| 2467 | struct seq_file *seq = file->private_data; |
| 2468 | kfree(seq->private); |
| 2469 | return single_release(inode, file); |
| 2470 | } |
| 2471 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2472 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2473 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2474 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2475 | .llseek = seq_lseek, |
| 2476 | .release = cgroup_seqfile_release, |
| 2477 | }; |
| 2478 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2479 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2480 | { |
| 2481 | int err; |
| 2482 | struct cftype *cft; |
| 2483 | |
| 2484 | err = generic_file_open(inode, file); |
| 2485 | if (err) |
| 2486 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2487 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2488 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2489 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2490 | struct cgroup_seqfile_state *state; |
| 2491 | |
| 2492 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2493 | if (!state) |
| 2494 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2495 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2496 | state->cft = cft; |
| 2497 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2498 | file->f_op = &cgroup_seqfile_operations; |
| 2499 | err = single_open(file, cgroup_seqfile_show, state); |
| 2500 | if (err < 0) |
| 2501 | kfree(state); |
| 2502 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2503 | err = cft->open(inode, file); |
| 2504 | else |
| 2505 | err = 0; |
| 2506 | |
| 2507 | return err; |
| 2508 | } |
| 2509 | |
| 2510 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2511 | { |
| 2512 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2513 | if (cft->release) |
| 2514 | return cft->release(inode, file); |
| 2515 | return 0; |
| 2516 | } |
| 2517 | |
| 2518 | /* |
| 2519 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2520 | */ |
| 2521 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2522 | struct inode *new_dir, struct dentry *new_dentry) |
| 2523 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2524 | int ret; |
| 2525 | struct cgroup_name *name, *old_name; |
| 2526 | struct cgroup *cgrp; |
| 2527 | |
| 2528 | /* |
| 2529 | * It's convinient to use parent dir's i_mutex to protected |
| 2530 | * cgrp->name. |
| 2531 | */ |
| 2532 | lockdep_assert_held(&old_dir->i_mutex); |
| 2533 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2534 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2535 | return -ENOTDIR; |
| 2536 | if (new_dentry->d_inode) |
| 2537 | return -EEXIST; |
| 2538 | if (old_dir != new_dir) |
| 2539 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2540 | |
| 2541 | cgrp = __d_cgrp(old_dentry); |
| 2542 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2543 | /* |
| 2544 | * This isn't a proper migration and its usefulness is very |
| 2545 | * limited. Disallow if sane_behavior. |
| 2546 | */ |
| 2547 | if (cgroup_sane_behavior(cgrp)) |
| 2548 | return -EPERM; |
| 2549 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2550 | name = cgroup_alloc_name(new_dentry); |
| 2551 | if (!name) |
| 2552 | return -ENOMEM; |
| 2553 | |
| 2554 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2555 | if (ret) { |
| 2556 | kfree(name); |
| 2557 | return ret; |
| 2558 | } |
| 2559 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2560 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2561 | rcu_assign_pointer(cgrp->name, name); |
| 2562 | |
| 2563 | kfree_rcu(old_name, rcu_head); |
| 2564 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2565 | } |
| 2566 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2567 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2568 | { |
| 2569 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2570 | return &__d_cgrp(dentry)->xattrs; |
| 2571 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2572 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | static inline int xattr_enabled(struct dentry *dentry) |
| 2576 | { |
| 2577 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2578 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | static bool is_valid_xattr(const char *name) |
| 2582 | { |
| 2583 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2584 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2585 | return true; |
| 2586 | return false; |
| 2587 | } |
| 2588 | |
| 2589 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2590 | const void *val, size_t size, int flags) |
| 2591 | { |
| 2592 | if (!xattr_enabled(dentry)) |
| 2593 | return -EOPNOTSUPP; |
| 2594 | if (!is_valid_xattr(name)) |
| 2595 | return -EINVAL; |
| 2596 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2597 | } |
| 2598 | |
| 2599 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2600 | { |
| 2601 | if (!xattr_enabled(dentry)) |
| 2602 | return -EOPNOTSUPP; |
| 2603 | if (!is_valid_xattr(name)) |
| 2604 | return -EINVAL; |
| 2605 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2606 | } |
| 2607 | |
| 2608 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2609 | void *buf, size_t size) |
| 2610 | { |
| 2611 | if (!xattr_enabled(dentry)) |
| 2612 | return -EOPNOTSUPP; |
| 2613 | if (!is_valid_xattr(name)) |
| 2614 | return -EINVAL; |
| 2615 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2616 | } |
| 2617 | |
| 2618 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2619 | { |
| 2620 | if (!xattr_enabled(dentry)) |
| 2621 | return -EOPNOTSUPP; |
| 2622 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2623 | } |
| 2624 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2625 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2626 | .read = cgroup_file_read, |
| 2627 | .write = cgroup_file_write, |
| 2628 | .llseek = generic_file_llseek, |
| 2629 | .open = cgroup_file_open, |
| 2630 | .release = cgroup_file_release, |
| 2631 | }; |
| 2632 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2633 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2634 | .setxattr = cgroup_setxattr, |
| 2635 | .getxattr = cgroup_getxattr, |
| 2636 | .listxattr = cgroup_listxattr, |
| 2637 | .removexattr = cgroup_removexattr, |
| 2638 | }; |
| 2639 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2640 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2641 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2642 | .mkdir = cgroup_mkdir, |
| 2643 | .rmdir = cgroup_rmdir, |
| 2644 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2645 | .setxattr = cgroup_setxattr, |
| 2646 | .getxattr = cgroup_getxattr, |
| 2647 | .listxattr = cgroup_listxattr, |
| 2648 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2649 | }; |
| 2650 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2651 | 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] | 2652 | { |
| 2653 | if (dentry->d_name.len > NAME_MAX) |
| 2654 | return ERR_PTR(-ENAMETOOLONG); |
| 2655 | d_add(dentry, NULL); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2659 | /* |
| 2660 | * Check if a file is a control file |
| 2661 | */ |
| 2662 | static inline struct cftype *__file_cft(struct file *file) |
| 2663 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2664 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2665 | return ERR_PTR(-EINVAL); |
| 2666 | return __d_cft(file->f_dentry); |
| 2667 | } |
| 2668 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2669 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2670 | struct super_block *sb) |
| 2671 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2672 | struct inode *inode; |
| 2673 | |
| 2674 | if (!dentry) |
| 2675 | return -ENOENT; |
| 2676 | if (dentry->d_inode) |
| 2677 | return -EEXIST; |
| 2678 | |
| 2679 | inode = cgroup_new_inode(mode, sb); |
| 2680 | if (!inode) |
| 2681 | return -ENOMEM; |
| 2682 | |
| 2683 | if (S_ISDIR(mode)) { |
| 2684 | inode->i_op = &cgroup_dir_inode_operations; |
| 2685 | inode->i_fop = &simple_dir_operations; |
| 2686 | |
| 2687 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2688 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2689 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2690 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2691 | /* |
| 2692 | * Control reaches here with cgroup_mutex held. |
| 2693 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2694 | * want to populate it immediately without releasing |
| 2695 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2696 | * yet, trylock will always succeed without affecting |
| 2697 | * lockdep checks. |
| 2698 | */ |
| 2699 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2700 | } else if (S_ISREG(mode)) { |
| 2701 | inode->i_size = 0; |
| 2702 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2703 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2704 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2705 | d_instantiate(dentry, inode); |
| 2706 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2707 | return 0; |
| 2708 | } |
| 2709 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2710 | /** |
| 2711 | * cgroup_file_mode - deduce file mode of a control file |
| 2712 | * @cft: the control file in question |
| 2713 | * |
| 2714 | * returns cft->mode if ->mode is not 0 |
| 2715 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2716 | * returns S_IRUGO if it has only a read handler |
| 2717 | * returns S_IWUSR if it has only a write hander |
| 2718 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2719 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2720 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2721 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2722 | |
| 2723 | if (cft->mode) |
| 2724 | return cft->mode; |
| 2725 | |
| 2726 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2727 | cft->read_map || cft->read_seq_string) |
| 2728 | mode |= S_IRUGO; |
| 2729 | |
| 2730 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2731 | cft->write_string || cft->trigger) |
| 2732 | mode |= S_IWUSR; |
| 2733 | |
| 2734 | return mode; |
| 2735 | } |
| 2736 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2737 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2738 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2739 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2740 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2741 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2742 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2743 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2744 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2745 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2746 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2747 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2748 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2749 | strcpy(name, subsys->name); |
| 2750 | strcat(name, "."); |
| 2751 | } |
| 2752 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2753 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2754 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2755 | |
| 2756 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2757 | if (!cfe) |
| 2758 | return -ENOMEM; |
| 2759 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2760 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2761 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2762 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2763 | goto out; |
| 2764 | } |
| 2765 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2766 | cfe->type = (void *)cft; |
| 2767 | cfe->dentry = dentry; |
| 2768 | dentry->d_fsdata = cfe; |
| 2769 | simple_xattrs_init(&cfe->xattrs); |
| 2770 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2771 | mode = cgroup_file_mode(cft); |
| 2772 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2773 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2774 | list_add_tail(&cfe->node, &parent->files); |
| 2775 | cfe = NULL; |
| 2776 | } |
| 2777 | dput(dentry); |
| 2778 | out: |
| 2779 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2780 | return error; |
| 2781 | } |
| 2782 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2783 | /** |
| 2784 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2785 | * @cgrp: the target cgroup |
| 2786 | * @subsys: the subsystem of files to be added |
| 2787 | * @cfts: array of cftypes to be added |
| 2788 | * @is_add: whether to add or remove |
| 2789 | * |
| 2790 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
| 2791 | * All @cfts should belong to @subsys. For removals, this function never |
| 2792 | * fails. If addition fails, this function doesn't remove files already |
| 2793 | * added. The caller is responsible for cleaning up. |
| 2794 | */ |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2795 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2796 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2797 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2798 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2799 | int ret; |
| 2800 | |
| 2801 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2802 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2803 | |
| 2804 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2805 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2806 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2807 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2808 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2809 | continue; |
| 2810 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2811 | continue; |
| 2812 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2813 | if (is_add) { |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2814 | ret = cgroup_add_file(cgrp, subsys, cft); |
| 2815 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2816 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2817 | cft->name, ret); |
| 2818 | return ret; |
| 2819 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2820 | } else { |
| 2821 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2822 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2823 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2824 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2827 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2828 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2829 | { |
| 2830 | /* |
| 2831 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2832 | * the existing cgroups under cgroup_mutex and create files. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2833 | * Instead, we use cgroup_for_each_descendant_pre() and drop RCU |
| 2834 | * read lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2835 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2836 | mutex_lock(&cgroup_mutex); |
| 2837 | } |
| 2838 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2839 | static int cgroup_cfts_commit(struct cgroup_subsys *ss, |
| 2840 | struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2841 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2842 | { |
| 2843 | LIST_HEAD(pending); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2844 | struct cgroup *cgrp, *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2845 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2846 | struct dentry *prev = NULL; |
| 2847 | struct inode *inode; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2848 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2849 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2850 | |
| 2851 | /* %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] | 2852 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2854 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2855 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2858 | /* |
| 2859 | * All cgroups which are created after we drop cgroup_mutex will |
| 2860 | * 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] | 2861 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2862 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2863 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2864 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2865 | mutex_unlock(&cgroup_mutex); |
| 2866 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2867 | /* @root always needs to be updated */ |
| 2868 | inode = root->dentry->d_inode; |
| 2869 | mutex_lock(&inode->i_mutex); |
| 2870 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2871 | ret = cgroup_addrm_files(root, ss, cfts, is_add); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2872 | mutex_unlock(&cgroup_mutex); |
| 2873 | mutex_unlock(&inode->i_mutex); |
| 2874 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2875 | if (ret) |
| 2876 | goto out_deact; |
| 2877 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2878 | /* add/rm files for all cgroups created before */ |
| 2879 | rcu_read_lock(); |
| 2880 | cgroup_for_each_descendant_pre(cgrp, root) { |
| 2881 | if (cgroup_is_dead(cgrp)) |
| 2882 | continue; |
| 2883 | |
| 2884 | inode = cgrp->dentry->d_inode; |
| 2885 | dget(cgrp->dentry); |
| 2886 | rcu_read_unlock(); |
| 2887 | |
| 2888 | dput(prev); |
| 2889 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2890 | |
| 2891 | mutex_lock(&inode->i_mutex); |
| 2892 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2893 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2894 | ret = cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2895 | mutex_unlock(&cgroup_mutex); |
| 2896 | mutex_unlock(&inode->i_mutex); |
| 2897 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2898 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2899 | if (ret) |
| 2900 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2901 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2902 | rcu_read_unlock(); |
| 2903 | dput(prev); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2904 | out_deact: |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2905 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2906 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | /** |
| 2910 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2911 | * @ss: target cgroup subsystem |
| 2912 | * @cfts: zero-length name terminated array of cftypes |
| 2913 | * |
| 2914 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2915 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2916 | * have them too. This function can be called anytime whether @ss is |
| 2917 | * attached or not. |
| 2918 | * |
| 2919 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2920 | * function currently returns 0 as long as @cfts registration is successful |
| 2921 | * even if some file creation attempts on existing cgroups fail. |
| 2922 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2923 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2924 | { |
| 2925 | struct cftype_set *set; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2926 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2927 | |
| 2928 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2929 | if (!set) |
| 2930 | return -ENOMEM; |
| 2931 | |
| 2932 | cgroup_cfts_prepare(); |
| 2933 | set->cfts = cfts; |
| 2934 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame^] | 2935 | ret = cgroup_cfts_commit(ss, cfts, true); |
| 2936 | if (ret) |
| 2937 | cgroup_rm_cftypes(ss, cfts); |
| 2938 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2939 | } |
| 2940 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2941 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2942 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2943 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2944 | * @ss: target cgroup subsystem |
| 2945 | * @cfts: zero-length name terminated array of cftypes |
| 2946 | * |
| 2947 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2948 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2949 | * won't have them either. This function can be called anytime whether @ss |
| 2950 | * is attached or not. |
| 2951 | * |
| 2952 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2953 | * registered with @ss. |
| 2954 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2955 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2956 | { |
| 2957 | struct cftype_set *set; |
| 2958 | |
| 2959 | cgroup_cfts_prepare(); |
| 2960 | |
| 2961 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2962 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2963 | list_del(&set->node); |
| 2964 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2965 | cgroup_cfts_commit(ss, cfts, false); |
| 2966 | return 0; |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | cgroup_cfts_commit(ss, NULL, false); |
| 2971 | return -ENOENT; |
| 2972 | } |
| 2973 | |
| 2974 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2975 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2976 | * @cgrp: the cgroup in question |
| 2977 | * |
| 2978 | * Return the number of tasks in the cgroup. |
| 2979 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2980 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2981 | { |
| 2982 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2983 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2984 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2985 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2986 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2987 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2988 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2989 | return count; |
| 2990 | } |
| 2991 | |
| 2992 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2993 | * Advance a list_head iterator. The iterator should be positioned at |
| 2994 | * the start of a css_set |
| 2995 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2996 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2997 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2998 | struct list_head *l = it->cset_link; |
| 2999 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3000 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3001 | |
| 3002 | /* Advance to the next non-empty css_set */ |
| 3003 | do { |
| 3004 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3005 | if (l == &cgrp->cset_links) { |
| 3006 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3007 | return; |
| 3008 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3009 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3010 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3011 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3012 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 3013 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3016 | /* |
| 3017 | * To reduce the fork() overhead for systems that are not actually |
| 3018 | * using their cgroups capability, we don't maintain the lists running |
| 3019 | * through each css_set to its tasks until we see the list actually |
| 3020 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3021 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 3022 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3023 | { |
| 3024 | struct task_struct *p, *g; |
| 3025 | write_lock(&css_set_lock); |
| 3026 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3027 | /* |
| 3028 | * We need tasklist_lock because RCU is not safe against |
| 3029 | * while_each_thread(). Besides, a forking task that has passed |
| 3030 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 3031 | * is not guaranteed to have its child immediately visible in the |
| 3032 | * tasklist if we walk through it with RCU. |
| 3033 | */ |
| 3034 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3035 | do_each_thread(g, p) { |
| 3036 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 3037 | /* |
| 3038 | * We should check if the process is exiting, otherwise |
| 3039 | * it will race with cgroup_exit() in that the list |
| 3040 | * entry won't be deleted though the process has exited. |
| 3041 | */ |
| 3042 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 3043 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3044 | task_unlock(p); |
| 3045 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3046 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3047 | write_unlock(&css_set_lock); |
| 3048 | } |
| 3049 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3050 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3051 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 3052 | * @pos: the current cgroup |
| 3053 | * |
| 3054 | * This function returns the next sibling of @pos and should be called |
| 3055 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 3056 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 3057 | * state. |
| 3058 | */ |
| 3059 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 3060 | { |
| 3061 | struct cgroup *next; |
| 3062 | |
| 3063 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3064 | |
| 3065 | /* |
| 3066 | * @pos could already have been removed. Once a cgroup is removed, |
| 3067 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3068 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3069 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3070 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3071 | * finished its grace period even if it's already removed, and thus |
| 3072 | * safe to dereference from this RCU critical section. If |
| 3073 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3074 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3075 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3076 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3077 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3078 | if (&next->sibling != &pos->parent->children) |
| 3079 | return next; |
| 3080 | return NULL; |
| 3081 | } |
| 3082 | |
| 3083 | /* |
| 3084 | * Can't dereference the next pointer. Each cgroup is given a |
| 3085 | * monotonically increasing unique serial number and always |
| 3086 | * appended to the sibling list, so the next one can be found by |
| 3087 | * walking the parent's children until we see a cgroup with higher |
| 3088 | * serial number than @pos's. |
| 3089 | * |
| 3090 | * While this path can be slow, it's taken only when either the |
| 3091 | * current cgroup is removed or iteration and removal race. |
| 3092 | */ |
| 3093 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3094 | if (next->serial_nr > pos->serial_nr) |
| 3095 | return next; |
| 3096 | return NULL; |
| 3097 | } |
| 3098 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3099 | |
| 3100 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3101 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3102 | * @pos: the current position (%NULL to initiate traversal) |
| 3103 | * @cgroup: cgroup whose descendants to walk |
| 3104 | * |
| 3105 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3106 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3107 | * |
| 3108 | * While this function requires RCU read locking, it doesn't require the |
| 3109 | * whole traversal to be contained in a single RCU critical section. This |
| 3110 | * function will return the correct next descendant as long as both @pos |
| 3111 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3112 | */ |
| 3113 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3114 | struct cgroup *cgroup) |
| 3115 | { |
| 3116 | struct cgroup *next; |
| 3117 | |
| 3118 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3119 | |
| 3120 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3121 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3122 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3123 | |
| 3124 | /* visit the first child if exists */ |
| 3125 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3126 | if (next) |
| 3127 | return next; |
| 3128 | |
| 3129 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3130 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3131 | next = cgroup_next_sibling(pos); |
| 3132 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3133 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3134 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3135 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3136 | |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3140 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3141 | /** |
| 3142 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3143 | * @pos: cgroup of interest |
| 3144 | * |
| 3145 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3146 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3147 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3148 | * |
| 3149 | * While this function requires RCU read locking, it doesn't require the |
| 3150 | * whole traversal to be contained in a single RCU critical section. This |
| 3151 | * function will return the correct rightmost descendant as long as @pos is |
| 3152 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3153 | */ |
| 3154 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3155 | { |
| 3156 | struct cgroup *last, *tmp; |
| 3157 | |
| 3158 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3159 | |
| 3160 | do { |
| 3161 | last = pos; |
| 3162 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3163 | pos = NULL; |
| 3164 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3165 | pos = tmp; |
| 3166 | } while (pos); |
| 3167 | |
| 3168 | return last; |
| 3169 | } |
| 3170 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3171 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3172 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3173 | { |
| 3174 | struct cgroup *last; |
| 3175 | |
| 3176 | do { |
| 3177 | last = pos; |
| 3178 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3179 | sibling); |
| 3180 | } while (pos); |
| 3181 | |
| 3182 | return last; |
| 3183 | } |
| 3184 | |
| 3185 | /** |
| 3186 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3187 | * @pos: the current position (%NULL to initiate traversal) |
| 3188 | * @cgroup: cgroup whose descendants to walk |
| 3189 | * |
| 3190 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3191 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3192 | * |
| 3193 | * While this function requires RCU read locking, it doesn't require the |
| 3194 | * whole traversal to be contained in a single RCU critical section. This |
| 3195 | * function will return the correct next descendant as long as both @pos |
| 3196 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3197 | */ |
| 3198 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3199 | struct cgroup *cgroup) |
| 3200 | { |
| 3201 | struct cgroup *next; |
| 3202 | |
| 3203 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3204 | |
| 3205 | /* if first iteration, visit the leftmost descendant */ |
| 3206 | if (!pos) { |
| 3207 | next = cgroup_leftmost_descendant(cgroup); |
| 3208 | return next != cgroup ? next : NULL; |
| 3209 | } |
| 3210 | |
| 3211 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3212 | next = cgroup_next_sibling(pos); |
| 3213 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3214 | return cgroup_leftmost_descendant(next); |
| 3215 | |
| 3216 | /* no sibling left, visit parent */ |
| 3217 | next = pos->parent; |
| 3218 | return next != cgroup ? next : NULL; |
| 3219 | } |
| 3220 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3221 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3222 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3223 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3224 | { |
| 3225 | /* |
| 3226 | * The first time anyone tries to iterate across a cgroup, |
| 3227 | * we need to enable the list linking each css_set to its |
| 3228 | * tasks, and fix up all existing tasks. |
| 3229 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3230 | if (!use_task_css_set_links) |
| 3231 | cgroup_enable_task_cg_lists(); |
| 3232 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3233 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3234 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3235 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3236 | } |
| 3237 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3238 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3239 | struct cgroup_iter *it) |
| 3240 | { |
| 3241 | struct task_struct *res; |
| 3242 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3243 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3244 | |
| 3245 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3246 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3247 | return NULL; |
| 3248 | res = list_entry(l, struct task_struct, cg_list); |
| 3249 | /* Advance iterator to find next entry */ |
| 3250 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3251 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3252 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3253 | /* We reached the end of this task list - move on to |
| 3254 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3255 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3256 | } else { |
| 3257 | it->task = l; |
| 3258 | } |
| 3259 | return res; |
| 3260 | } |
| 3261 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3262 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3263 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3264 | { |
| 3265 | read_unlock(&css_set_lock); |
| 3266 | } |
| 3267 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3268 | static inline int started_after_time(struct task_struct *t1, |
| 3269 | struct timespec *time, |
| 3270 | struct task_struct *t2) |
| 3271 | { |
| 3272 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3273 | if (start_diff > 0) { |
| 3274 | return 1; |
| 3275 | } else if (start_diff < 0) { |
| 3276 | return 0; |
| 3277 | } else { |
| 3278 | /* |
| 3279 | * Arbitrarily, if two processes started at the same |
| 3280 | * time, we'll say that the lower pointer value |
| 3281 | * started first. Note that t2 may have exited by now |
| 3282 | * so this may not be a valid pointer any longer, but |
| 3283 | * that's fine - it still serves to distinguish |
| 3284 | * between two tasks started (effectively) simultaneously. |
| 3285 | */ |
| 3286 | return t1 > t2; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | /* |
| 3291 | * This function is a callback from heap_insert() and is used to order |
| 3292 | * the heap. |
| 3293 | * In this case we order the heap in descending task start time. |
| 3294 | */ |
| 3295 | static inline int started_after(void *p1, void *p2) |
| 3296 | { |
| 3297 | struct task_struct *t1 = p1; |
| 3298 | struct task_struct *t2 = p2; |
| 3299 | return started_after_time(t1, &t2->start_time, t2); |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3304 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3305 | * |
| 3306 | * Arguments include pointers to callback functions test_task() and |
| 3307 | * process_task(). |
| 3308 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3309 | * and if it returns true, call process_task() for it also. |
| 3310 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3311 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3312 | * but does not lock css_set_lock for the call to process_task(). |
| 3313 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3314 | * creation. |
| 3315 | * It is guaranteed that process_task() will act on every task that |
| 3316 | * is a member of the cgroup for the duration of this call. This |
| 3317 | * function may or may not call process_task() for tasks that exit |
| 3318 | * or move to a different cgroup during the call, or are forked or |
| 3319 | * move into the cgroup during the call. |
| 3320 | * |
| 3321 | * Note that test_task() may be called with locks held, and may in some |
| 3322 | * situations be called multiple times for the same task, so it should |
| 3323 | * be cheap. |
| 3324 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3325 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3326 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3327 | * may cause this function to fail). |
| 3328 | */ |
| 3329 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3330 | { |
| 3331 | int retval, i; |
| 3332 | struct cgroup_iter it; |
| 3333 | struct task_struct *p, *dropped; |
| 3334 | /* Never dereference latest_task, since it's not refcounted */ |
| 3335 | struct task_struct *latest_task = NULL; |
| 3336 | struct ptr_heap tmp_heap; |
| 3337 | struct ptr_heap *heap; |
| 3338 | struct timespec latest_time = { 0, 0 }; |
| 3339 | |
| 3340 | if (scan->heap) { |
| 3341 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3342 | heap = scan->heap; |
| 3343 | heap->gt = &started_after; |
| 3344 | } else { |
| 3345 | /* We need to allocate our own heap memory */ |
| 3346 | heap = &tmp_heap; |
| 3347 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3348 | if (retval) |
| 3349 | /* cannot allocate the heap */ |
| 3350 | return retval; |
| 3351 | } |
| 3352 | |
| 3353 | again: |
| 3354 | /* |
| 3355 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3356 | * to determine which are of interest, and using the scanner's |
| 3357 | * "process_task" callback to process any of them that need an update. |
| 3358 | * Since we don't want to hold any locks during the task updates, |
| 3359 | * gather tasks to be processed in a heap structure. |
| 3360 | * The heap is sorted by descending task start time. |
| 3361 | * If the statically-sized heap fills up, we overflow tasks that |
| 3362 | * started later, and in future iterations only consider tasks that |
| 3363 | * started after the latest task in the previous pass. This |
| 3364 | * guarantees forward progress and that we don't miss any tasks. |
| 3365 | */ |
| 3366 | heap->size = 0; |
| 3367 | cgroup_iter_start(scan->cg, &it); |
| 3368 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3369 | /* |
| 3370 | * Only affect tasks that qualify per the caller's callback, |
| 3371 | * if he provided one |
| 3372 | */ |
| 3373 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3374 | continue; |
| 3375 | /* |
| 3376 | * Only process tasks that started after the last task |
| 3377 | * we processed |
| 3378 | */ |
| 3379 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3380 | continue; |
| 3381 | dropped = heap_insert(heap, p); |
| 3382 | if (dropped == NULL) { |
| 3383 | /* |
| 3384 | * The new task was inserted; the heap wasn't |
| 3385 | * previously full |
| 3386 | */ |
| 3387 | get_task_struct(p); |
| 3388 | } else if (dropped != p) { |
| 3389 | /* |
| 3390 | * The new task was inserted, and pushed out a |
| 3391 | * different task |
| 3392 | */ |
| 3393 | get_task_struct(p); |
| 3394 | put_task_struct(dropped); |
| 3395 | } |
| 3396 | /* |
| 3397 | * Else the new task was newer than anything already in |
| 3398 | * the heap and wasn't inserted |
| 3399 | */ |
| 3400 | } |
| 3401 | cgroup_iter_end(scan->cg, &it); |
| 3402 | |
| 3403 | if (heap->size) { |
| 3404 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3405 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3406 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3407 | latest_time = q->start_time; |
| 3408 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3409 | } |
| 3410 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3411 | scan->process_task(q, scan); |
| 3412 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3413 | } |
| 3414 | /* |
| 3415 | * If we had to process any tasks at all, scan again |
| 3416 | * in case some of them were in the middle of forking |
| 3417 | * children that didn't get processed. |
| 3418 | * Not the most efficient way to do it, but it avoids |
| 3419 | * having to take callback_mutex in the fork path |
| 3420 | */ |
| 3421 | goto again; |
| 3422 | } |
| 3423 | if (heap == &tmp_heap) |
| 3424 | heap_free(&tmp_heap); |
| 3425 | return 0; |
| 3426 | } |
| 3427 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3428 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3429 | struct cgroup_scanner *scan) |
| 3430 | { |
| 3431 | struct cgroup *new_cgroup = scan->data; |
| 3432 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3433 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3434 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3435 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /** |
| 3439 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3440 | * @to: cgroup to which the tasks will be moved |
| 3441 | * @from: cgroup in which the tasks currently reside |
| 3442 | */ |
| 3443 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3444 | { |
| 3445 | struct cgroup_scanner scan; |
| 3446 | |
| 3447 | scan.cg = from; |
| 3448 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3449 | scan.process_task = cgroup_transfer_one_task; |
| 3450 | scan.heap = NULL; |
| 3451 | scan.data = to; |
| 3452 | |
| 3453 | return cgroup_scan_tasks(&scan); |
| 3454 | } |
| 3455 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3456 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3457 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3458 | * |
| 3459 | * Reading this file can return large amounts of data if a cgroup has |
| 3460 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3461 | * but we cannot guarantee that the information we produce is correct |
| 3462 | * unless we produce it entirely atomically. |
| 3463 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3464 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3465 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3466 | /* which pidlist file are we talking about? */ |
| 3467 | enum cgroup_filetype { |
| 3468 | CGROUP_FILE_PROCS, |
| 3469 | CGROUP_FILE_TASKS, |
| 3470 | }; |
| 3471 | |
| 3472 | /* |
| 3473 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3474 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3475 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3476 | * to the cgroup. |
| 3477 | */ |
| 3478 | struct cgroup_pidlist { |
| 3479 | /* |
| 3480 | * used to find which pidlist is wanted. doesn't change as long as |
| 3481 | * this particular list stays in the list. |
| 3482 | */ |
| 3483 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3484 | /* array of xids */ |
| 3485 | pid_t *list; |
| 3486 | /* how many elements the above list has */ |
| 3487 | int length; |
| 3488 | /* how many files are using the current array */ |
| 3489 | int use_count; |
| 3490 | /* each of these stored in a list by its cgroup */ |
| 3491 | struct list_head links; |
| 3492 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3493 | struct cgroup *owner; |
| 3494 | /* protects the other fields */ |
| 3495 | struct rw_semaphore mutex; |
| 3496 | }; |
| 3497 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3498 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3499 | * The following two functions "fix" the issue where there are more pids |
| 3500 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3501 | * TODO: replace with a kernel-wide solution to this problem |
| 3502 | */ |
| 3503 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3504 | static void *pidlist_allocate(int count) |
| 3505 | { |
| 3506 | if (PIDLIST_TOO_LARGE(count)) |
| 3507 | return vmalloc(count * sizeof(pid_t)); |
| 3508 | else |
| 3509 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3510 | } |
| 3511 | static void pidlist_free(void *p) |
| 3512 | { |
| 3513 | if (is_vmalloc_addr(p)) |
| 3514 | vfree(p); |
| 3515 | else |
| 3516 | kfree(p); |
| 3517 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3518 | |
| 3519 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3520 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3521 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3522 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3523 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3524 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3525 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3526 | |
| 3527 | /* |
| 3528 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3529 | * edge cases first; no work needs to be done for either |
| 3530 | */ |
| 3531 | if (length == 0 || length == 1) |
| 3532 | return length; |
| 3533 | /* src and dest walk down the list; dest counts unique elements */ |
| 3534 | for (src = 1; src < length; src++) { |
| 3535 | /* find next unique element */ |
| 3536 | while (list[src] == list[src-1]) { |
| 3537 | src++; |
| 3538 | if (src == length) |
| 3539 | goto after; |
| 3540 | } |
| 3541 | /* dest always points to where the next unique element goes */ |
| 3542 | list[dest] = list[src]; |
| 3543 | dest++; |
| 3544 | } |
| 3545 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3546 | return dest; |
| 3547 | } |
| 3548 | |
| 3549 | static int cmppid(const void *a, const void *b) |
| 3550 | { |
| 3551 | return *(pid_t *)a - *(pid_t *)b; |
| 3552 | } |
| 3553 | |
| 3554 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3555 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3556 | * returns with the lock on that pidlist already held, and takes care |
| 3557 | * of the use count, or returns NULL with no locks held if we're out of |
| 3558 | * memory. |
| 3559 | */ |
| 3560 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3561 | enum cgroup_filetype type) |
| 3562 | { |
| 3563 | struct cgroup_pidlist *l; |
| 3564 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3565 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3566 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3567 | /* |
| 3568 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3569 | * the last ref-holder is trying to remove l from the list at the same |
| 3570 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3571 | * list we find out from under us - compare release_pid_array(). |
| 3572 | */ |
| 3573 | mutex_lock(&cgrp->pidlist_mutex); |
| 3574 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3575 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3576 | /* make sure l doesn't vanish out from under us */ |
| 3577 | down_write(&l->mutex); |
| 3578 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3579 | return l; |
| 3580 | } |
| 3581 | } |
| 3582 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3583 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3584 | if (!l) { |
| 3585 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3586 | return l; |
| 3587 | } |
| 3588 | init_rwsem(&l->mutex); |
| 3589 | down_write(&l->mutex); |
| 3590 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3591 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3592 | l->owner = cgrp; |
| 3593 | list_add(&l->links, &cgrp->pidlists); |
| 3594 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3595 | return l; |
| 3596 | } |
| 3597 | |
| 3598 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3599 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3600 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3601 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3602 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3603 | { |
| 3604 | pid_t *array; |
| 3605 | int length; |
| 3606 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3607 | struct cgroup_iter it; |
| 3608 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3609 | struct cgroup_pidlist *l; |
| 3610 | |
| 3611 | /* |
| 3612 | * If cgroup gets more users after we read count, we won't have |
| 3613 | * enough space - tough. This race is indistinguishable to the |
| 3614 | * caller from the case that the additional cgroup users didn't |
| 3615 | * show up until sometime later on. |
| 3616 | */ |
| 3617 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3618 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3619 | if (!array) |
| 3620 | return -ENOMEM; |
| 3621 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3622 | cgroup_iter_start(cgrp, &it); |
| 3623 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3624 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3625 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3626 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3627 | if (type == CGROUP_FILE_PROCS) |
| 3628 | pid = task_tgid_vnr(tsk); |
| 3629 | else |
| 3630 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3631 | if (pid > 0) /* make sure to only use valid results */ |
| 3632 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3633 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3634 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3635 | length = n; |
| 3636 | /* now sort & (if procs) strip out duplicates */ |
| 3637 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3638 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3639 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3640 | l = cgroup_pidlist_find(cgrp, type); |
| 3641 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3642 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3643 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3644 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3645 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3646 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | l->list = array; |
| 3648 | l->length = length; |
| 3649 | l->use_count++; |
| 3650 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3651 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3652 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3655 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3656 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3657 | * @stats: cgroupstats to fill information into |
| 3658 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3659 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3660 | * |
| 3661 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3662 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3663 | */ |
| 3664 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3665 | { |
| 3666 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3667 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3668 | struct cgroup_iter it; |
| 3669 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3670 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3671 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3672 | * Validate dentry by checking the superblock operations, |
| 3673 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3674 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3675 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3676 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3677 | goto err; |
| 3678 | |
| 3679 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3680 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3681 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3682 | cgroup_iter_start(cgrp, &it); |
| 3683 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3684 | switch (tsk->state) { |
| 3685 | case TASK_RUNNING: |
| 3686 | stats->nr_running++; |
| 3687 | break; |
| 3688 | case TASK_INTERRUPTIBLE: |
| 3689 | stats->nr_sleeping++; |
| 3690 | break; |
| 3691 | case TASK_UNINTERRUPTIBLE: |
| 3692 | stats->nr_uninterruptible++; |
| 3693 | break; |
| 3694 | case TASK_STOPPED: |
| 3695 | stats->nr_stopped++; |
| 3696 | break; |
| 3697 | default: |
| 3698 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3699 | stats->nr_io_wait++; |
| 3700 | break; |
| 3701 | } |
| 3702 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3703 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3704 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3705 | err: |
| 3706 | return ret; |
| 3707 | } |
| 3708 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3709 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3710 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3711 | * 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] | 3712 | * 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] | 3713 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3714 | */ |
| 3715 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3716 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3717 | { |
| 3718 | /* |
| 3719 | * Initially we receive a position value that corresponds to |
| 3720 | * one more than the last pid shown (or 0 on the first call or |
| 3721 | * after a seek to the start). Use a binary-search to find the |
| 3722 | * next pid to display, if any |
| 3723 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3724 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3725 | int index = 0, pid = *pos; |
| 3726 | int *iter; |
| 3727 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3728 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3729 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3730 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3731 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3732 | while (index < end) { |
| 3733 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3734 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3735 | index = mid; |
| 3736 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3737 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3738 | index = mid + 1; |
| 3739 | else |
| 3740 | end = mid; |
| 3741 | } |
| 3742 | } |
| 3743 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3744 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3745 | return NULL; |
| 3746 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3747 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3748 | *pos = *iter; |
| 3749 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3750 | } |
| 3751 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3752 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3753 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3754 | struct cgroup_pidlist *l = s->private; |
| 3755 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3758 | 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] | 3759 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3760 | struct cgroup_pidlist *l = s->private; |
| 3761 | pid_t *p = v; |
| 3762 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3763 | /* |
| 3764 | * Advance to the next pid in the array. If this goes off the |
| 3765 | * end, we're done |
| 3766 | */ |
| 3767 | p++; |
| 3768 | if (p >= end) { |
| 3769 | return NULL; |
| 3770 | } else { |
| 3771 | *pos = *p; |
| 3772 | return p; |
| 3773 | } |
| 3774 | } |
| 3775 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3776 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3777 | { |
| 3778 | return seq_printf(s, "%d\n", *(int *)v); |
| 3779 | } |
| 3780 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3781 | /* |
| 3782 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3783 | * independent of whether it's tasks or procs |
| 3784 | */ |
| 3785 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3786 | .start = cgroup_pidlist_start, |
| 3787 | .stop = cgroup_pidlist_stop, |
| 3788 | .next = cgroup_pidlist_next, |
| 3789 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3790 | }; |
| 3791 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3792 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3793 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3794 | /* |
| 3795 | * the case where we're the last user of this particular pidlist will |
| 3796 | * have us remove it from the cgroup's list, which entails taking the |
| 3797 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3798 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3799 | */ |
| 3800 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3801 | down_write(&l->mutex); |
| 3802 | BUG_ON(!l->use_count); |
| 3803 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3804 | /* we're the last user if refcount is 0; remove and free */ |
| 3805 | list_del(&l->links); |
| 3806 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3807 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3808 | put_pid_ns(l->key.ns); |
| 3809 | up_write(&l->mutex); |
| 3810 | kfree(l); |
| 3811 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3812 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3813 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3814 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3817 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3818 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3819 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3820 | if (!(file->f_mode & FMODE_READ)) |
| 3821 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3822 | /* |
| 3823 | * the seq_file will only be initialized if the file was opened for |
| 3824 | * reading; hence we check if it's not null only in that case. |
| 3825 | */ |
| 3826 | l = ((struct seq_file *)file->private_data)->private; |
| 3827 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3828 | return seq_release(inode, file); |
| 3829 | } |
| 3830 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3831 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3832 | .read = seq_read, |
| 3833 | .llseek = seq_lseek, |
| 3834 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3835 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3836 | }; |
| 3837 | |
| 3838 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3839 | * The following functions handle opens on a file that displays a pidlist |
| 3840 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3841 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3842 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3843 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3844 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3845 | { |
| 3846 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3847 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3848 | int retval; |
| 3849 | |
| 3850 | /* Nothing to do for write-only files */ |
| 3851 | if (!(file->f_mode & FMODE_READ)) |
| 3852 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3853 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3854 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3855 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3856 | if (retval) |
| 3857 | return retval; |
| 3858 | /* configure file information */ |
| 3859 | file->f_op = &cgroup_pidlist_operations; |
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 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3862 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3863 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3864 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3865 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3866 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3869 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3870 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3871 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3872 | } |
| 3873 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3874 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3875 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3876 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3877 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3878 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3879 | struct cftype *cft) |
| 3880 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3881 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3882 | } |
| 3883 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3884 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3885 | struct cftype *cft, |
| 3886 | u64 val) |
| 3887 | { |
| 3888 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3889 | if (val) |
| 3890 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3891 | else |
| 3892 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3893 | return 0; |
| 3894 | } |
| 3895 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3896 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3897 | * When dput() is called asynchronously, if umount has been done and |
| 3898 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3899 | * there's a small window that vfs will see the root dentry with non-zero |
| 3900 | * refcnt and trigger BUG(). |
| 3901 | * |
| 3902 | * That's why we hold a reference before dput() and drop it right after. |
| 3903 | */ |
| 3904 | static void cgroup_dput(struct cgroup *cgrp) |
| 3905 | { |
| 3906 | struct super_block *sb = cgrp->root->sb; |
| 3907 | |
| 3908 | atomic_inc(&sb->s_active); |
| 3909 | dput(cgrp->dentry); |
| 3910 | deactivate_super(sb); |
| 3911 | } |
| 3912 | |
| 3913 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3914 | * Unregister event and free resources. |
| 3915 | * |
| 3916 | * Gets called from workqueue. |
| 3917 | */ |
| 3918 | static void cgroup_event_remove(struct work_struct *work) |
| 3919 | { |
| 3920 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3921 | remove); |
| 3922 | struct cgroup *cgrp = event->cgrp; |
| 3923 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3924 | remove_wait_queue(event->wqh, &event->wait); |
| 3925 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3926 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3927 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3928 | /* Notify userspace the event is going away. */ |
| 3929 | eventfd_signal(event->eventfd, 1); |
| 3930 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3931 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3932 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3933 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | /* |
| 3937 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3938 | * |
| 3939 | * Called with wqh->lock held and interrupts disabled. |
| 3940 | */ |
| 3941 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3942 | int sync, void *key) |
| 3943 | { |
| 3944 | struct cgroup_event *event = container_of(wait, |
| 3945 | struct cgroup_event, wait); |
| 3946 | struct cgroup *cgrp = event->cgrp; |
| 3947 | unsigned long flags = (unsigned long)key; |
| 3948 | |
| 3949 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3950 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3951 | * If the event has been detached at cgroup removal, we |
| 3952 | * can simply return knowing the other side will cleanup |
| 3953 | * for us. |
| 3954 | * |
| 3955 | * We can't race against event freeing since the other |
| 3956 | * side will require wqh->lock via remove_wait_queue(), |
| 3957 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3958 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3959 | spin_lock(&cgrp->event_list_lock); |
| 3960 | if (!list_empty(&event->list)) { |
| 3961 | list_del_init(&event->list); |
| 3962 | /* |
| 3963 | * We are in atomic context, but cgroup_event_remove() |
| 3964 | * may sleep, so we have to call it in workqueue. |
| 3965 | */ |
| 3966 | schedule_work(&event->remove); |
| 3967 | } |
| 3968 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
| 3974 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3975 | wait_queue_head_t *wqh, poll_table *pt) |
| 3976 | { |
| 3977 | struct cgroup_event *event = container_of(pt, |
| 3978 | struct cgroup_event, pt); |
| 3979 | |
| 3980 | event->wqh = wqh; |
| 3981 | add_wait_queue(wqh, &event->wait); |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Parse input and register new cgroup event handler. |
| 3986 | * |
| 3987 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3988 | * Interpretation of args is defined by control file implementation. |
| 3989 | */ |
| 3990 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3991 | const char *buffer) |
| 3992 | { |
| 3993 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3994 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3995 | unsigned int efd, cfd; |
| 3996 | struct file *efile = NULL; |
| 3997 | struct file *cfile = NULL; |
| 3998 | char *endp; |
| 3999 | int ret; |
| 4000 | |
| 4001 | efd = simple_strtoul(buffer, &endp, 10); |
| 4002 | if (*endp != ' ') |
| 4003 | return -EINVAL; |
| 4004 | buffer = endp + 1; |
| 4005 | |
| 4006 | cfd = simple_strtoul(buffer, &endp, 10); |
| 4007 | if ((*endp != ' ') && (*endp != '\0')) |
| 4008 | return -EINVAL; |
| 4009 | buffer = endp + 1; |
| 4010 | |
| 4011 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 4012 | if (!event) |
| 4013 | return -ENOMEM; |
| 4014 | event->cgrp = cgrp; |
| 4015 | INIT_LIST_HEAD(&event->list); |
| 4016 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 4017 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 4018 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 4019 | |
| 4020 | efile = eventfd_fget(efd); |
| 4021 | if (IS_ERR(efile)) { |
| 4022 | ret = PTR_ERR(efile); |
| 4023 | goto fail; |
| 4024 | } |
| 4025 | |
| 4026 | event->eventfd = eventfd_ctx_fileget(efile); |
| 4027 | if (IS_ERR(event->eventfd)) { |
| 4028 | ret = PTR_ERR(event->eventfd); |
| 4029 | goto fail; |
| 4030 | } |
| 4031 | |
| 4032 | cfile = fget(cfd); |
| 4033 | if (!cfile) { |
| 4034 | ret = -EBADF; |
| 4035 | goto fail; |
| 4036 | } |
| 4037 | |
| 4038 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 4039 | /* 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] | 4040 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4041 | if (ret < 0) |
| 4042 | goto fail; |
| 4043 | |
| 4044 | event->cft = __file_cft(cfile); |
| 4045 | if (IS_ERR(event->cft)) { |
| 4046 | ret = PTR_ERR(event->cft); |
| 4047 | goto fail; |
| 4048 | } |
| 4049 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4050 | /* |
| 4051 | * The file to be monitored must be in the same cgroup as |
| 4052 | * cgroup.event_control is. |
| 4053 | */ |
| 4054 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4055 | if (cgrp_cfile != cgrp) { |
| 4056 | ret = -EINVAL; |
| 4057 | goto fail; |
| 4058 | } |
| 4059 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4060 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4061 | ret = -EINVAL; |
| 4062 | goto fail; |
| 4063 | } |
| 4064 | |
| 4065 | ret = event->cft->register_event(cgrp, event->cft, |
| 4066 | event->eventfd, buffer); |
| 4067 | if (ret) |
| 4068 | goto fail; |
| 4069 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4070 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4071 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4072 | /* |
| 4073 | * Events should be removed after rmdir of cgroup directory, but before |
| 4074 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4075 | * directory dentry to do that. |
| 4076 | */ |
| 4077 | dget(cgrp->dentry); |
| 4078 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4079 | spin_lock(&cgrp->event_list_lock); |
| 4080 | list_add(&event->list, &cgrp->event_list); |
| 4081 | spin_unlock(&cgrp->event_list_lock); |
| 4082 | |
| 4083 | fput(cfile); |
| 4084 | fput(efile); |
| 4085 | |
| 4086 | return 0; |
| 4087 | |
| 4088 | fail: |
| 4089 | if (cfile) |
| 4090 | fput(cfile); |
| 4091 | |
| 4092 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4093 | eventfd_ctx_put(event->eventfd); |
| 4094 | |
| 4095 | if (!IS_ERR_OR_NULL(efile)) |
| 4096 | fput(efile); |
| 4097 | |
| 4098 | kfree(event); |
| 4099 | |
| 4100 | return ret; |
| 4101 | } |
| 4102 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4103 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4104 | struct cftype *cft) |
| 4105 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4106 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4110 | struct cftype *cft, |
| 4111 | u64 val) |
| 4112 | { |
| 4113 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4114 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4115 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4116 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4117 | return 0; |
| 4118 | } |
| 4119 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4120 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4121 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4122 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4123 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4124 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4125 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4126 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4127 | }, |
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.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4130 | .write_string = cgroup_write_event_control, |
| 4131 | .mode = S_IWUGO, |
| 4132 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4133 | { |
| 4134 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4135 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4136 | .read_u64 = cgroup_clone_children_read, |
| 4137 | .write_u64 = cgroup_clone_children_write, |
| 4138 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4139 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4140 | .name = "cgroup.sane_behavior", |
| 4141 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4142 | .read_seq_string = cgroup_sane_behavior_show, |
| 4143 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4144 | |
| 4145 | /* |
| 4146 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4147 | * don't exist if sane_behavior. If you're depending on these, be |
| 4148 | * prepared to be burned. |
| 4149 | */ |
| 4150 | { |
| 4151 | .name = "tasks", |
| 4152 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4153 | .open = cgroup_tasks_open, |
| 4154 | .write_u64 = cgroup_tasks_write, |
| 4155 | .release = cgroup_pidlist_release, |
| 4156 | .mode = S_IRUGO | S_IWUSR, |
| 4157 | }, |
| 4158 | { |
| 4159 | .name = "notify_on_release", |
| 4160 | .flags = CFTYPE_INSANE, |
| 4161 | .read_u64 = cgroup_read_notify_on_release, |
| 4162 | .write_u64 = cgroup_write_notify_on_release, |
| 4163 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4164 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4165 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4166 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4167 | .read_seq_string = cgroup_release_agent_show, |
| 4168 | .write_string = cgroup_release_agent_write, |
| 4169 | .max_write_len = PATH_MAX, |
| 4170 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4171 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4172 | }; |
| 4173 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4174 | /** |
| 4175 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4176 | * @cgrp: target cgroup |
| 4177 | * @base_files: true if the base files should be added |
| 4178 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4179 | */ |
| 4180 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4181 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4182 | { |
| 4183 | int err; |
| 4184 | struct cgroup_subsys *ss; |
| 4185 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4186 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4187 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4188 | if (err < 0) |
| 4189 | return err; |
| 4190 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4191 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4192 | /* process cftsets of each subsystem */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4193 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4194 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4195 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4196 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4197 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4198 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4199 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4201 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4202 | /* This cgroup is ready now */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4203 | for_each_root_subsys(cgrp->root, ss) { |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4204 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4205 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 4206 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4207 | /* |
| 4208 | * Update id->css pointer and make this css visible from |
| 4209 | * CSS ID functions. This pointer will be dereferened |
| 4210 | * from RCU-read-side without locks. |
| 4211 | */ |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4212 | if (id) |
| 4213 | rcu_assign_pointer(id->css, css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4214 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4219 | static void css_dput_fn(struct work_struct *work) |
| 4220 | { |
| 4221 | struct cgroup_subsys_state *css = |
| 4222 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4223 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4224 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4225 | } |
| 4226 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4227 | static void css_release(struct percpu_ref *ref) |
| 4228 | { |
| 4229 | struct cgroup_subsys_state *css = |
| 4230 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4231 | |
| 4232 | schedule_work(&css->dput_work); |
| 4233 | } |
| 4234 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4235 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4236 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4237 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4238 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4239 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4240 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4241 | css->id = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4242 | if (cgrp == cgroup_dummy_top) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4243 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4244 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4245 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4246 | |
| 4247 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4248 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4249 | * css_put(). dput() requires process context, which css_put() may |
| 4250 | * be called without. @css->dput_work will be used to invoke |
| 4251 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4252 | */ |
| 4253 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4254 | } |
| 4255 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4256 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4257 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4258 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4259 | int ret = 0; |
| 4260 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4261 | lockdep_assert_held(&cgroup_mutex); |
| 4262 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4263 | if (ss->css_online) |
| 4264 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4265 | if (!ret) |
| 4266 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4267 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4271 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4272 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4273 | { |
| 4274 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4275 | |
| 4276 | lockdep_assert_held(&cgroup_mutex); |
| 4277 | |
| 4278 | if (!(css->flags & CSS_ONLINE)) |
| 4279 | return; |
| 4280 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4281 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4282 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4283 | |
| 4284 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4285 | } |
| 4286 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4287 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4288 | * cgroup_create - create a cgroup |
| 4289 | * @parent: cgroup that will be parent of the new cgroup |
| 4290 | * @dentry: dentry of the new cgroup |
| 4291 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4292 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4293 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4294 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4295 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4296 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4297 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4298 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4299 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4300 | struct cgroupfs_root *root = parent->root; |
| 4301 | int err = 0; |
| 4302 | struct cgroup_subsys *ss; |
| 4303 | struct super_block *sb = root->sb; |
| 4304 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4305 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4306 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4307 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4308 | return -ENOMEM; |
| 4309 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4310 | name = cgroup_alloc_name(dentry); |
| 4311 | if (!name) |
| 4312 | goto err_free_cgrp; |
| 4313 | rcu_assign_pointer(cgrp->name, name); |
| 4314 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4315 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4316 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4317 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4318 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4319 | /* |
| 4320 | * Only live parents can have children. Note that the liveliness |
| 4321 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4322 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4323 | * anyway so that locking is contained inside cgroup proper and we |
| 4324 | * don't get nasty surprises if we ever grow another caller. |
| 4325 | */ |
| 4326 | if (!cgroup_lock_live_group(parent)) { |
| 4327 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4328 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4329 | } |
| 4330 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4331 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4332 | * get deleted on unmount if there are child cgroups. This |
| 4333 | * can be done outside cgroup_mutex, since the sb can't |
| 4334 | * disappear while someone has an open control file on the |
| 4335 | * fs */ |
| 4336 | atomic_inc(&sb->s_active); |
| 4337 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4338 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4339 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4340 | dentry->d_fsdata = cgrp; |
| 4341 | cgrp->dentry = dentry; |
| 4342 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4343 | cgrp->parent = parent; |
| 4344 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4345 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4346 | if (notify_on_release(parent)) |
| 4347 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4348 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4349 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4350 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4351 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4352 | for_each_root_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4353 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4354 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4355 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4356 | if (IS_ERR(css)) { |
| 4357 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4358 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4359 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4360 | |
| 4361 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4362 | if (err) |
| 4363 | goto err_free_all; |
| 4364 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4365 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4366 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4367 | if (ss->use_id) { |
| 4368 | err = alloc_css_id(ss, parent, cgrp); |
| 4369 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4370 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4371 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4374 | /* |
| 4375 | * Create directory. cgroup_create_file() returns with the new |
| 4376 | * directory locked on success so that it can be populated without |
| 4377 | * dropping cgroup_mutex. |
| 4378 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4379 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4380 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4381 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4382 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4383 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4384 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4385 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4386 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4387 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4388 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4389 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4390 | /* each css holds a ref to the cgroup's dentry */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4391 | for_each_root_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4392 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4393 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4394 | /* hold a ref to the parent's dentry */ |
| 4395 | dget(parent->dentry); |
| 4396 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4397 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4398 | for_each_root_subsys(root, ss) { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4399 | err = online_css(ss, cgrp); |
| 4400 | if (err) |
| 4401 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4402 | |
| 4403 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4404 | parent->parent) { |
| 4405 | 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", |
| 4406 | current->comm, current->pid, ss->name); |
| 4407 | if (!strcmp(ss->name, "memory")) |
| 4408 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4409 | ss->warned_broken_hierarchy = true; |
| 4410 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4411 | } |
| 4412 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4413 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4414 | if (err) |
| 4415 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4416 | |
| 4417 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4418 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4419 | |
| 4420 | return 0; |
| 4421 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4422 | err_free_all: |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4423 | for_each_root_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4424 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4425 | |
| 4426 | if (css) { |
| 4427 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4428 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4429 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4430 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4431 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4432 | /* Release the reference count that we took on the superblock */ |
| 4433 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4434 | err_free_id: |
| 4435 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4436 | err_free_name: |
| 4437 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4438 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4439 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4440 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4441 | |
| 4442 | err_destroy: |
| 4443 | cgroup_destroy_locked(cgrp); |
| 4444 | mutex_unlock(&cgroup_mutex); |
| 4445 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4446 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4447 | } |
| 4448 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4449 | 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] | 4450 | { |
| 4451 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4452 | |
| 4453 | /* the vfs holds inode->i_mutex already */ |
| 4454 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4455 | } |
| 4456 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4457 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4458 | { |
| 4459 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4460 | return; |
| 4461 | |
| 4462 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4463 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4464 | schedule_work(&cgrp->destroy_work); |
| 4465 | } |
| 4466 | |
| 4467 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4468 | { |
| 4469 | struct cgroup_subsys_state *css = |
| 4470 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4471 | |
| 4472 | cgroup_css_killed(css->cgroup); |
| 4473 | } |
| 4474 | |
| 4475 | /** |
| 4476 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4477 | * @cgrp: cgroup to be destroyed |
| 4478 | * |
| 4479 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4480 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4481 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4482 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4483 | * the following two steps. |
| 4484 | * |
| 4485 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4486 | * userland visible parts and start killing the percpu refcnts of |
| 4487 | * css's. Set up so that the next stage will be kicked off once all |
| 4488 | * the percpu refcnts are confirmed to be killed. |
| 4489 | * |
| 4490 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4491 | * rest of destruction. Once all cgroup references are gone, the |
| 4492 | * cgroup is RCU-freed. |
| 4493 | * |
| 4494 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4495 | * the userland is concerned and a new cgroup with the same name may be |
| 4496 | * created. As cgroup doesn't care about the names internally, this |
| 4497 | * doesn't cause any problem. |
| 4498 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4499 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4500 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4501 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4502 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4503 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4504 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4505 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4506 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4507 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4508 | lockdep_assert_held(&cgroup_mutex); |
| 4509 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4510 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4511 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4512 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4513 | */ |
| 4514 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4515 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4516 | read_unlock(&css_set_lock); |
| 4517 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4518 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4519 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4520 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4521 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4522 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4523 | * css reference will be given out via css_tryget(). We can't |
| 4524 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4525 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4526 | * as killed on all CPUs on return. |
| 4527 | * |
| 4528 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4529 | * css is confirmed to be seen as killed on all CPUs. The |
| 4530 | * notification callback keeps track of the number of css's to be |
| 4531 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4532 | * destruction once the percpu refs of all css's are confirmed to |
| 4533 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4534 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4535 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4536 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4537 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4538 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4539 | /* |
| 4540 | * Killing would put the base ref, but we need to keep it |
| 4541 | * alive until after ->css_offline. |
| 4542 | */ |
| 4543 | percpu_ref_get(&css->refcnt); |
| 4544 | |
| 4545 | atomic_inc(&cgrp->css_kill_cnt); |
| 4546 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4547 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4548 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4549 | |
| 4550 | /* |
| 4551 | * Mark @cgrp dead. This prevents further task migration and child |
| 4552 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4553 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4554 | * resume iteration after dropping RCU read lock. See |
| 4555 | * cgroup_next_sibling() for details. |
| 4556 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4557 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4558 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4559 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4560 | raw_spin_lock(&release_list_lock); |
| 4561 | if (!list_empty(&cgrp->release_list)) |
| 4562 | list_del_init(&cgrp->release_list); |
| 4563 | raw_spin_unlock(&release_list_lock); |
| 4564 | |
| 4565 | /* |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4566 | * Clear and remove @cgrp directory. The removal puts the base ref |
| 4567 | * 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] | 4568 | */ |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4569 | cgroup_clear_dir(cgrp, true, cgrp->root->subsys_mask); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4570 | dget(d); |
| 4571 | cgroup_d_remove_dir(d); |
| 4572 | |
| 4573 | /* |
| 4574 | * Unregister events and notify userspace. |
| 4575 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4576 | * directory to avoid race between userspace and kernelspace. |
| 4577 | */ |
| 4578 | spin_lock(&cgrp->event_list_lock); |
| 4579 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4580 | list_del_init(&event->list); |
| 4581 | schedule_work(&event->remove); |
| 4582 | } |
| 4583 | spin_unlock(&cgrp->event_list_lock); |
| 4584 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4585 | return 0; |
| 4586 | }; |
| 4587 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4588 | /** |
| 4589 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4590 | * @work: cgroup->destroy_free_work |
| 4591 | * |
| 4592 | * This function is invoked from a work item for a cgroup which is being |
| 4593 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4594 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4595 | * is the second step of destruction described in the comment above |
| 4596 | * cgroup_destroy_locked(). |
| 4597 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4598 | static void cgroup_offline_fn(struct work_struct *work) |
| 4599 | { |
| 4600 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4601 | struct cgroup *parent = cgrp->parent; |
| 4602 | struct dentry *d = cgrp->dentry; |
| 4603 | struct cgroup_subsys *ss; |
| 4604 | |
| 4605 | mutex_lock(&cgroup_mutex); |
| 4606 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4607 | /* |
| 4608 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4609 | * initate destruction. |
| 4610 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4611 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4612 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4613 | |
| 4614 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4615 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4616 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4617 | * proceeds regardless of css refs. On the last put of each css, |
| 4618 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4619 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4620 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4621 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4622 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4623 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4624 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4625 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4626 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4627 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4628 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4629 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4630 | check_for_release(parent); |
| 4631 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4632 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4635 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4636 | { |
| 4637 | int ret; |
| 4638 | |
| 4639 | mutex_lock(&cgroup_mutex); |
| 4640 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4641 | mutex_unlock(&cgroup_mutex); |
| 4642 | |
| 4643 | return ret; |
| 4644 | } |
| 4645 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4646 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4647 | { |
| 4648 | INIT_LIST_HEAD(&ss->cftsets); |
| 4649 | |
| 4650 | /* |
| 4651 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4652 | * deregistration. |
| 4653 | */ |
| 4654 | if (ss->base_cftypes) { |
| 4655 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4656 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4657 | } |
| 4658 | } |
| 4659 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4660 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4661 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4662 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4663 | |
| 4664 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4665 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4666 | mutex_lock(&cgroup_mutex); |
| 4667 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4668 | /* init base cftset */ |
| 4669 | cgroup_init_cftsets(ss); |
| 4670 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4671 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4672 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4673 | ss->root = &cgroup_dummy_root; |
| 4674 | css = ss->css_alloc(cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4675 | /* We don't handle early failures gracefully */ |
| 4676 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4677 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4678 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4679 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4680 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4681 | * newly registered, all tasks and hence the |
| 4682 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4683 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4684 | |
| 4685 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4686 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4687 | /* At system boot, before all subsystems have been |
| 4688 | * registered, no tasks have been forked, so we don't |
| 4689 | * need to invoke fork callbacks here. */ |
| 4690 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4691 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4692 | BUG_ON(online_css(ss, cgroup_dummy_top)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4693 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4694 | mutex_unlock(&cgroup_mutex); |
| 4695 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4696 | /* this function shouldn't be used with modular subsystems, since they |
| 4697 | * need to register a subsys_id, among other things */ |
| 4698 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4702 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4703 | * @ss: the subsystem to load |
| 4704 | * |
| 4705 | * 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] | 4706 | * 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] | 4707 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4708 | * simpler cgroup_init_subsys. |
| 4709 | */ |
| 4710 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4711 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4712 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4713 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4714 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4715 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4716 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4717 | |
| 4718 | /* check name and function validity */ |
| 4719 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4720 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4721 | return -EINVAL; |
| 4722 | |
| 4723 | /* |
| 4724 | * we don't support callbacks in modular subsystems. this check is |
| 4725 | * before the ss->module check for consistency; a subsystem that could |
| 4726 | * be a module should still have no callbacks even if the user isn't |
| 4727 | * compiling it as one. |
| 4728 | */ |
| 4729 | if (ss->fork || ss->exit) |
| 4730 | return -EINVAL; |
| 4731 | |
| 4732 | /* |
| 4733 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4734 | * since cgroup_init_subsys will have already taken care of it. |
| 4735 | */ |
| 4736 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4737 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4738 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4739 | return 0; |
| 4740 | } |
| 4741 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4742 | /* init base cftset */ |
| 4743 | cgroup_init_cftsets(ss); |
| 4744 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4745 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4746 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4747 | |
| 4748 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4749 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4750 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4751 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4752 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4753 | css = ss->css_alloc(cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4754 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4755 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4756 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4757 | mutex_unlock(&cgroup_mutex); |
| 4758 | return PTR_ERR(css); |
| 4759 | } |
| 4760 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4761 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4762 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4763 | |
| 4764 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4765 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4766 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4767 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4768 | ret = cgroup_init_idr(ss, css); |
| 4769 | if (ret) |
| 4770 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4775 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4776 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4777 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4778 | * table state, so each changed css_set will need its hash recomputed. |
| 4779 | * this is all done under the css_set_lock. |
| 4780 | */ |
| 4781 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4782 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4783 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4784 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4785 | continue; |
| 4786 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4787 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4788 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4789 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4790 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4791 | key = css_set_hash(cset->subsys); |
| 4792 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4793 | } |
| 4794 | write_unlock(&css_set_lock); |
| 4795 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4796 | ret = online_css(ss, cgroup_dummy_top); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4797 | if (ret) |
| 4798 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4799 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4800 | /* success! */ |
| 4801 | mutex_unlock(&cgroup_mutex); |
| 4802 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4803 | |
| 4804 | err_unload: |
| 4805 | mutex_unlock(&cgroup_mutex); |
| 4806 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4807 | cgroup_unload_subsys(ss); |
| 4808 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4809 | } |
| 4810 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4811 | |
| 4812 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4813 | * cgroup_unload_subsys: unload a modular subsystem |
| 4814 | * @ss: the subsystem to unload |
| 4815 | * |
| 4816 | * This function should be called in a modular subsystem's exitcall. When this |
| 4817 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4818 | * the subsystem will not be attached to any hierarchy. |
| 4819 | */ |
| 4820 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4821 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4822 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4823 | |
| 4824 | BUG_ON(ss->module == NULL); |
| 4825 | |
| 4826 | /* |
| 4827 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4828 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4829 | * doesn't start being used while we're killing it off. |
| 4830 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4831 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4832 | |
| 4833 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4834 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4835 | offline_css(ss, cgroup_dummy_top); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4836 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4837 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4838 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4839 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4840 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4841 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4842 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4843 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4844 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4845 | |
| 4846 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4847 | * disentangle the css from all css_sets attached to the dummy |
| 4848 | * top. as in loading, we need to pay our respects to the hashtable |
| 4849 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4850 | */ |
| 4851 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4852 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4853 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4854 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4855 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4856 | hash_del(&cset->hlist); |
| 4857 | cset->subsys[ss->subsys_id] = NULL; |
| 4858 | key = css_set_hash(cset->subsys); |
| 4859 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4860 | } |
| 4861 | write_unlock(&css_set_lock); |
| 4862 | |
| 4863 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4864 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4865 | * need to free before marking as null because ss->css_free needs |
| 4866 | * the cgrp->subsys pointer to find their state. note that this |
| 4867 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4868 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4869 | ss->css_free(cgroup_dummy_top); |
| 4870 | cgroup_dummy_top->subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4871 | |
| 4872 | mutex_unlock(&cgroup_mutex); |
| 4873 | } |
| 4874 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4875 | |
| 4876 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4877 | * cgroup_init_early - cgroup initialization at system boot |
| 4878 | * |
| 4879 | * Initialize cgroups at system boot, and initialize any |
| 4880 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4881 | */ |
| 4882 | int __init cgroup_init_early(void) |
| 4883 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4884 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4885 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4886 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4887 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4888 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4889 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4890 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4891 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4892 | init_cgroup_root(&cgroup_dummy_root); |
| 4893 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4894 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4895 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4896 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4897 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4898 | 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] | 4899 | 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] | 4900 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4901 | /* at bootup time, we don't worry about modular subsystems */ |
| 4902 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4903 | BUG_ON(!ss->name); |
| 4904 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4905 | BUG_ON(!ss->css_alloc); |
| 4906 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4907 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4908 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4909 | ss->name, ss->subsys_id); |
| 4910 | BUG(); |
| 4911 | } |
| 4912 | |
| 4913 | if (ss->early_init) |
| 4914 | cgroup_init_subsys(ss); |
| 4915 | } |
| 4916 | return 0; |
| 4917 | } |
| 4918 | |
| 4919 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4920 | * cgroup_init - cgroup initialization |
| 4921 | * |
| 4922 | * Register cgroup filesystem and /proc file, and initialize |
| 4923 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4924 | */ |
| 4925 | int __init cgroup_init(void) |
| 4926 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4927 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4928 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4929 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4930 | |
| 4931 | err = bdi_init(&cgroup_backing_dev_info); |
| 4932 | if (err) |
| 4933 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4934 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4935 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4936 | if (!ss->early_init) |
| 4937 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4938 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4939 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4942 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4943 | mutex_lock(&cgroup_mutex); |
| 4944 | mutex_lock(&cgroup_root_mutex); |
| 4945 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4946 | /* Add init_css_set to the hash table */ |
| 4947 | key = css_set_hash(init_css_set.subsys); |
| 4948 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4949 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4950 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4951 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4952 | mutex_unlock(&cgroup_root_mutex); |
| 4953 | mutex_unlock(&cgroup_mutex); |
| 4954 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4955 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4956 | if (!cgroup_kobj) { |
| 4957 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4958 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | err = register_filesystem(&cgroup_fs_type); |
| 4962 | if (err < 0) { |
| 4963 | kobject_put(cgroup_kobj); |
| 4964 | goto out; |
| 4965 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4966 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4967 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4968 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4969 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4970 | if (err) |
| 4971 | bdi_destroy(&cgroup_backing_dev_info); |
| 4972 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4973 | return err; |
| 4974 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4975 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4976 | /* |
| 4977 | * proc_cgroup_show() |
| 4978 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4979 | * - Used for /proc/<pid>/cgroup. |
| 4980 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4981 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4982 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4983 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4984 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4985 | * cgroup to top_cgroup. |
| 4986 | */ |
| 4987 | |
| 4988 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4989 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4990 | { |
| 4991 | struct pid *pid; |
| 4992 | struct task_struct *tsk; |
| 4993 | char *buf; |
| 4994 | int retval; |
| 4995 | struct cgroupfs_root *root; |
| 4996 | |
| 4997 | retval = -ENOMEM; |
| 4998 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4999 | if (!buf) |
| 5000 | goto out; |
| 5001 | |
| 5002 | retval = -ESRCH; |
| 5003 | pid = m->private; |
| 5004 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 5005 | if (!tsk) |
| 5006 | goto out_free; |
| 5007 | |
| 5008 | retval = 0; |
| 5009 | |
| 5010 | mutex_lock(&cgroup_mutex); |
| 5011 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 5012 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5013 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5014 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5015 | int count = 0; |
| 5016 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5017 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 5018 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5019 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5020 | if (strlen(root->name)) |
| 5021 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5022 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5023 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5024 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5025 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5026 | if (retval < 0) |
| 5027 | goto out_unlock; |
| 5028 | seq_puts(m, buf); |
| 5029 | seq_putc(m, '\n'); |
| 5030 | } |
| 5031 | |
| 5032 | out_unlock: |
| 5033 | mutex_unlock(&cgroup_mutex); |
| 5034 | put_task_struct(tsk); |
| 5035 | out_free: |
| 5036 | kfree(buf); |
| 5037 | out: |
| 5038 | return retval; |
| 5039 | } |
| 5040 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5041 | /* Display information about each subsystem and each hierarchy */ |
| 5042 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5043 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5044 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5045 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5046 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5047 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5048 | /* |
| 5049 | * ideally we don't want subsystems moving around while we do this. |
| 5050 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5051 | * subsys/hierarchy state. |
| 5052 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5053 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5054 | |
| 5055 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5056 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5057 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5058 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5059 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5060 | mutex_unlock(&cgroup_mutex); |
| 5061 | return 0; |
| 5062 | } |
| 5063 | |
| 5064 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5065 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5066 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5069 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5070 | .open = cgroupstats_open, |
| 5071 | .read = seq_read, |
| 5072 | .llseek = seq_lseek, |
| 5073 | .release = single_release, |
| 5074 | }; |
| 5075 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5076 | /** |
| 5077 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5078 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5079 | * |
| 5080 | * Description: A task inherits its parent's cgroup at fork(). |
| 5081 | * |
| 5082 | * A pointer to the shared css_set was automatically copied in |
| 5083 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5084 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5085 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5086 | * have already changed current->cgroups, allowing the previously |
| 5087 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5088 | * |
| 5089 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5090 | * task, and the passed argument 'child' points to the child task. |
| 5091 | */ |
| 5092 | void cgroup_fork(struct task_struct *child) |
| 5093 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5094 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5095 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5096 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5097 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5098 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5102 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5103 | * @child: the task in question |
| 5104 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5105 | * Adds the task to the list running through its css_set if necessary and |
| 5106 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5107 | * visible on the task list in case we race with the first call to |
| 5108 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5109 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5110 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5111 | void cgroup_post_fork(struct task_struct *child) |
| 5112 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5113 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5114 | int i; |
| 5115 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5116 | /* |
| 5117 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5118 | * under the tasklist_lock and we read it here after we added the child |
| 5119 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5120 | * yet in the tasklist when we walked through it from |
| 5121 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5122 | * should be visible now due to the paired locking and barriers implied |
| 5123 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5124 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5125 | * lock on fork. |
| 5126 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5127 | if (use_task_css_set_links) { |
| 5128 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5129 | task_lock(child); |
| 5130 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5131 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5132 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5133 | write_unlock(&css_set_lock); |
| 5134 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5135 | |
| 5136 | /* |
| 5137 | * Call ss->fork(). This must happen after @child is linked on |
| 5138 | * css_set; otherwise, @child might change state between ->fork() |
| 5139 | * and addition to css_set. |
| 5140 | */ |
| 5141 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5142 | /* |
| 5143 | * fork/exit callbacks are supported only for builtin |
| 5144 | * subsystems, and the builtin section of the subsys |
| 5145 | * array is immutable, so we don't need to lock the |
| 5146 | * subsys array here. On the other hand, modular section |
| 5147 | * of the array can be freed at module unload, so we |
| 5148 | * can't touch that. |
| 5149 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5150 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5151 | if (ss->fork) |
| 5152 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5153 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5154 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5155 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5156 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5157 | * cgroup_exit - detach cgroup from exiting task |
| 5158 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5159 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5160 | * |
| 5161 | * Description: Detach cgroup from @tsk and release it. |
| 5162 | * |
| 5163 | * Note that cgroups marked notify_on_release force every task in |
| 5164 | * them to take the global cgroup_mutex mutex when exiting. |
| 5165 | * This could impact scaling on very large systems. Be reluctant to |
| 5166 | * use notify_on_release cgroups where very high task exit scaling |
| 5167 | * is required on large systems. |
| 5168 | * |
| 5169 | * the_top_cgroup_hack: |
| 5170 | * |
| 5171 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5172 | * |
| 5173 | * We call cgroup_exit() while the task is still competent to |
| 5174 | * handle notify_on_release(), then leave the task attached to the |
| 5175 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5176 | * |
| 5177 | * To do this properly, we would increment the reference count on |
| 5178 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5179 | * code we would add a second cgroup function call, to drop that |
| 5180 | * reference. This would just create an unnecessary hot spot on |
| 5181 | * the top_cgroup reference count, to no avail. |
| 5182 | * |
| 5183 | * Normally, holding a reference to a cgroup without bumping its |
| 5184 | * count is unsafe. The cgroup could go away, or someone could |
| 5185 | * attach us to a different cgroup, decrementing the count on |
| 5186 | * the first cgroup that we never incremented. But in this case, |
| 5187 | * 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] | 5188 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5189 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5190 | */ |
| 5191 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5192 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5193 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5194 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5195 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5196 | |
| 5197 | /* |
| 5198 | * Unlink from the css_set task list if necessary. |
| 5199 | * Optimistically check cg_list before taking |
| 5200 | * css_set_lock |
| 5201 | */ |
| 5202 | if (!list_empty(&tsk->cg_list)) { |
| 5203 | write_lock(&css_set_lock); |
| 5204 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5205 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5206 | write_unlock(&css_set_lock); |
| 5207 | } |
| 5208 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5209 | /* Reassign the task to the init_css_set. */ |
| 5210 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5211 | cset = task_css_set(tsk); |
| 5212 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5213 | |
| 5214 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5215 | /* |
| 5216 | * fork/exit callbacks are supported only for builtin |
| 5217 | * subsystems, see cgroup_post_fork() for details. |
| 5218 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5219 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5220 | if (ss->exit) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5221 | struct cgroup *old_cgrp = cset->subsys[i]->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5222 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5223 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5224 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5228 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5229 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5230 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5231 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5232 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5233 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5234 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5235 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5236 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5237 | /* |
| 5238 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5239 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5240 | * it now |
| 5241 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5242 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5243 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5244 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5245 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5246 | list_empty(&cgrp->release_list)) { |
| 5247 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5248 | need_schedule_work = 1; |
| 5249 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5250 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5251 | if (need_schedule_work) |
| 5252 | schedule_work(&release_agent_work); |
| 5253 | } |
| 5254 | } |
| 5255 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5256 | /* |
| 5257 | * Notify userspace when a cgroup is released, by running the |
| 5258 | * configured release agent with the name of the cgroup (path |
| 5259 | * relative to the root of cgroup file system) as the argument. |
| 5260 | * |
| 5261 | * Most likely, this user command will try to rmdir this cgroup. |
| 5262 | * |
| 5263 | * This races with the possibility that some other task will be |
| 5264 | * attached to this cgroup before it is removed, or that some other |
| 5265 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5266 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5267 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5268 | * to continue to serve a useful existence. Next time it's released, |
| 5269 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5270 | * |
| 5271 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5272 | * means only wait until the task is successfully execve()'d. The |
| 5273 | * separate release agent task is forked by call_usermodehelper(), |
| 5274 | * then control in this thread returns here, without waiting for the |
| 5275 | * release agent task. We don't bother to wait because the caller of |
| 5276 | * this routine has no use for the exit status of the release agent |
| 5277 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5278 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5279 | static void cgroup_release_agent(struct work_struct *work) |
| 5280 | { |
| 5281 | BUG_ON(work != &release_agent_work); |
| 5282 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5283 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5284 | while (!list_empty(&release_list)) { |
| 5285 | char *argv[3], *envp[3]; |
| 5286 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5287 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5288 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5289 | struct cgroup, |
| 5290 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5291 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5292 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5293 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5294 | if (!pathbuf) |
| 5295 | goto continue_free; |
| 5296 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5297 | goto continue_free; |
| 5298 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5299 | if (!agentbuf) |
| 5300 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5301 | |
| 5302 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5303 | argv[i++] = agentbuf; |
| 5304 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5305 | argv[i] = NULL; |
| 5306 | |
| 5307 | i = 0; |
| 5308 | /* minimal command environment */ |
| 5309 | envp[i++] = "HOME=/"; |
| 5310 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5311 | envp[i] = NULL; |
| 5312 | |
| 5313 | /* Drop the lock while we invoke the usermode helper, |
| 5314 | * since the exec could involve hitting disk and hence |
| 5315 | * be a slow process */ |
| 5316 | mutex_unlock(&cgroup_mutex); |
| 5317 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5318 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5319 | continue_free: |
| 5320 | kfree(pathbuf); |
| 5321 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5322 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5323 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5324 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5325 | mutex_unlock(&cgroup_mutex); |
| 5326 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5327 | |
| 5328 | static int __init cgroup_disable(char *str) |
| 5329 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5330 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5331 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5332 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5333 | |
| 5334 | while ((token = strsep(&str, ",")) != NULL) { |
| 5335 | if (!*token) |
| 5336 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5337 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5338 | /* |
| 5339 | * cgroup_disable, being at boot time, can't know about |
| 5340 | * module subsystems, so we don't worry about them. |
| 5341 | */ |
| 5342 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5343 | if (!strcmp(token, ss->name)) { |
| 5344 | ss->disabled = 1; |
| 5345 | printk(KERN_INFO "Disabling %s control group" |
| 5346 | " subsystem\n", ss->name); |
| 5347 | break; |
| 5348 | } |
| 5349 | } |
| 5350 | } |
| 5351 | return 1; |
| 5352 | } |
| 5353 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5354 | |
| 5355 | /* |
| 5356 | * Functons for CSS ID. |
| 5357 | */ |
| 5358 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5359 | /* 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] | 5360 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5361 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5362 | struct css_id *cssid; |
| 5363 | |
| 5364 | /* |
| 5365 | * This css_id() can return correct value when somone has refcnt |
| 5366 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5367 | * it's unchanged until freed. |
| 5368 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5369 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5370 | |
| 5371 | if (cssid) |
| 5372 | return cssid->id; |
| 5373 | return 0; |
| 5374 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5375 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5376 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5377 | /** |
| 5378 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5379 | * @child: the css to be tested. |
| 5380 | * @root: the css supporsed to be an ancestor of the child. |
| 5381 | * |
| 5382 | * 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] | 5383 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5384 | * But, considering usual usage, the csses should be valid objects after test. |
| 5385 | * Assuming that the caller will do some action to the child if this returns |
| 5386 | * returns true, the caller must take "child";s reference count. |
| 5387 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5388 | */ |
| 5389 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5390 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5391 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5392 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5393 | struct css_id *child_id; |
| 5394 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5395 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5396 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5397 | if (!child_id) |
| 5398 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5399 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5400 | if (!root_id) |
| 5401 | return false; |
| 5402 | if (child_id->depth < root_id->depth) |
| 5403 | return false; |
| 5404 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5405 | return false; |
| 5406 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5407 | } |
| 5408 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5409 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5410 | { |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5411 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 5412 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5413 | /* When this is called before css_id initialization, id can be NULL */ |
| 5414 | if (!id) |
| 5415 | return; |
| 5416 | |
| 5417 | BUG_ON(!ss->use_id); |
| 5418 | |
| 5419 | rcu_assign_pointer(id->css, NULL); |
| 5420 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5421 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5422 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5423 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5424 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5425 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5426 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5427 | |
| 5428 | /* |
| 5429 | * This is called by init or create(). Then, calls to this function are |
| 5430 | * always serialized (By cgroup_mutex() at create()). |
| 5431 | */ |
| 5432 | |
| 5433 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5434 | { |
| 5435 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5436 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5437 | |
| 5438 | BUG_ON(!ss->use_id); |
| 5439 | |
| 5440 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5441 | newid = kzalloc(size, GFP_KERNEL); |
| 5442 | if (!newid) |
| 5443 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5444 | |
| 5445 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5446 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5447 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5448 | 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] | 5449 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5450 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5451 | |
| 5452 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5453 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5454 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5455 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5456 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5457 | newid->depth = depth; |
| 5458 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5459 | err_out: |
| 5460 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5461 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5462 | |
| 5463 | } |
| 5464 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5465 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5466 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5467 | { |
| 5468 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5469 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5470 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5471 | idr_init(&ss->idr); |
| 5472 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5473 | newid = get_new_cssid(ss, 0); |
| 5474 | if (IS_ERR(newid)) |
| 5475 | return PTR_ERR(newid); |
| 5476 | |
| 5477 | newid->stack[0] = newid->id; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5478 | RCU_INIT_POINTER(newid->css, rootcss); |
| 5479 | RCU_INIT_POINTER(rootcss->id, newid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5484 | struct cgroup *child) |
| 5485 | { |
| 5486 | int subsys_id, i, depth = 0; |
| 5487 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5488 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5489 | |
| 5490 | subsys_id = ss->subsys_id; |
| 5491 | parent_css = parent->subsys[subsys_id]; |
| 5492 | child_css = child->subsys[subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5493 | parent_id = rcu_dereference_protected(parent_css->id, true); |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5494 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5495 | |
| 5496 | child_id = get_new_cssid(ss, depth); |
| 5497 | if (IS_ERR(child_id)) |
| 5498 | return PTR_ERR(child_id); |
| 5499 | |
| 5500 | for (i = 0; i < depth; i++) |
| 5501 | child_id->stack[i] = parent_id->stack[i]; |
| 5502 | child_id->stack[depth] = child_id->id; |
| 5503 | /* |
| 5504 | * child_id->css pointer will be set after this cgroup is available |
| 5505 | * see cgroup_populate_dir() |
| 5506 | */ |
| 5507 | rcu_assign_pointer(child_css->id, child_id); |
| 5508 | |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
| 5512 | /** |
| 5513 | * css_lookup - lookup css by id |
| 5514 | * @ss: cgroup subsys to be looked into. |
| 5515 | * @id: the id |
| 5516 | * |
| 5517 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5518 | * NULL if not. Should be called under rcu_read_lock() |
| 5519 | */ |
| 5520 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5521 | { |
| 5522 | struct css_id *cssid = NULL; |
| 5523 | |
| 5524 | BUG_ON(!ss->use_id); |
| 5525 | cssid = idr_find(&ss->idr, id); |
| 5526 | |
| 5527 | if (unlikely(!cssid)) |
| 5528 | return NULL; |
| 5529 | |
| 5530 | return rcu_dereference(cssid->css); |
| 5531 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5532 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5533 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5534 | /* |
| 5535 | * get corresponding css from file open on cgroupfs directory |
| 5536 | */ |
| 5537 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5538 | { |
| 5539 | struct cgroup *cgrp; |
| 5540 | struct inode *inode; |
| 5541 | struct cgroup_subsys_state *css; |
| 5542 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5543 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5544 | /* check in cgroup filesystem dir */ |
| 5545 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5546 | return ERR_PTR(-EBADF); |
| 5547 | |
| 5548 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5549 | return ERR_PTR(-EINVAL); |
| 5550 | |
| 5551 | /* get cgroup */ |
| 5552 | cgrp = __d_cgrp(f->f_dentry); |
| 5553 | css = cgrp->subsys[id]; |
| 5554 | return css ? css : ERR_PTR(-ENOENT); |
| 5555 | } |
| 5556 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5557 | #ifdef CONFIG_CGROUP_DEBUG |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5558 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5559 | { |
| 5560 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5561 | |
| 5562 | if (!css) |
| 5563 | return ERR_PTR(-ENOMEM); |
| 5564 | |
| 5565 | return css; |
| 5566 | } |
| 5567 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5568 | static void debug_css_free(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5569 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5570 | kfree(cgrp->subsys[debug_subsys_id]); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5571 | } |
| 5572 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5573 | static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5574 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5575 | return cgroup_task_count(cgrp); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5576 | } |
| 5577 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5578 | static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5579 | { |
| 5580 | return (u64)(unsigned long)current->cgroups; |
| 5581 | } |
| 5582 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5583 | static u64 current_css_set_refcount_read(struct cgroup *cgrp, |
| 5584 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5585 | { |
| 5586 | u64 count; |
| 5587 | |
| 5588 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5589 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5590 | rcu_read_unlock(); |
| 5591 | return count; |
| 5592 | } |
| 5593 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5594 | static int current_css_set_cg_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5595 | struct cftype *cft, |
| 5596 | struct seq_file *seq) |
| 5597 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5598 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5599 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5600 | |
| 5601 | read_lock(&css_set_lock); |
| 5602 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5603 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5604 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5605 | struct cgroup *c = link->cgrp; |
| 5606 | const char *name; |
| 5607 | |
| 5608 | if (c->dentry) |
| 5609 | name = c->dentry->d_name.name; |
| 5610 | else |
| 5611 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5612 | seq_printf(seq, "Root %d group %s\n", |
| 5613 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5614 | } |
| 5615 | rcu_read_unlock(); |
| 5616 | read_unlock(&css_set_lock); |
| 5617 | return 0; |
| 5618 | } |
| 5619 | |
| 5620 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5621 | static int cgroup_css_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5622 | struct cftype *cft, |
| 5623 | struct seq_file *seq) |
| 5624 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5625 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5626 | |
| 5627 | read_lock(&css_set_lock); |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5628 | list_for_each_entry(link, &cgrp->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5629 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5630 | struct task_struct *task; |
| 5631 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5632 | seq_printf(seq, "css_set %p\n", cset); |
| 5633 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5634 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5635 | seq_puts(seq, " ...\n"); |
| 5636 | break; |
| 5637 | } else { |
| 5638 | seq_printf(seq, " task %d\n", |
| 5639 | task_pid_vnr(task)); |
| 5640 | } |
| 5641 | } |
| 5642 | } |
| 5643 | read_unlock(&css_set_lock); |
| 5644 | return 0; |
| 5645 | } |
| 5646 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5647 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5648 | { |
| 5649 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5650 | } |
| 5651 | |
| 5652 | static struct cftype debug_files[] = { |
| 5653 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5654 | .name = "taskcount", |
| 5655 | .read_u64 = debug_taskcount_read, |
| 5656 | }, |
| 5657 | |
| 5658 | { |
| 5659 | .name = "current_css_set", |
| 5660 | .read_u64 = current_css_set_read, |
| 5661 | }, |
| 5662 | |
| 5663 | { |
| 5664 | .name = "current_css_set_refcount", |
| 5665 | .read_u64 = current_css_set_refcount_read, |
| 5666 | }, |
| 5667 | |
| 5668 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5669 | .name = "current_css_set_cg_links", |
| 5670 | .read_seq_string = current_css_set_cg_links_read, |
| 5671 | }, |
| 5672 | |
| 5673 | { |
| 5674 | .name = "cgroup_css_links", |
| 5675 | .read_seq_string = cgroup_css_links_read, |
| 5676 | }, |
| 5677 | |
| 5678 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5679 | .name = "releasable", |
| 5680 | .read_u64 = releasable_read, |
| 5681 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5682 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5683 | { } /* terminate */ |
| 5684 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5685 | |
| 5686 | struct cgroup_subsys debug_subsys = { |
| 5687 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5688 | .css_alloc = debug_css_alloc, |
| 5689 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5690 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5691 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5692 | }; |
| 5693 | #endif /* CONFIG_CGROUP_DEBUG */ |