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> |
| 33 | #include <linux/fs.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 34 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 35 | #include <linux/kernel.h> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/mutex.h> |
| 39 | #include <linux/mount.h> |
| 40 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 44 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/seq_file.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/magic.h> |
| 48 | #include <linux/spinlock.h> |
| 49 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 50 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 51 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 52 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 53 | #include <linux/delayacct.h> |
| 54 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 55 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 56 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 57 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 58 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 59 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 60 | #include <linux/eventfd.h> |
| 61 | #include <linux/poll.h> |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 62 | #include <linux/flex_array.h> /* used in cgroup_attach_proc */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 63 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 64 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 65 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 66 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 67 | /* css deactivation bias, makes css->refcnt negative to deny new trygets */ |
| 68 | #define CSS_DEACT_BIAS INT_MIN |
| 69 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 70 | /* |
| 71 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 72 | * hierarchy must be performed while holding it. |
| 73 | * |
| 74 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 75 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 76 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 77 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 78 | * break the following locking order cycle. |
| 79 | * |
| 80 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 81 | * B. namespace_sem -> cgroup_mutex |
| 82 | * |
| 83 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 84 | * breaks it. |
| 85 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 87 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 88 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 89 | /* |
| 90 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 91 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 92 | * registered after that. The mutable section of this array is protected by |
| 93 | * cgroup_mutex. |
| 94 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 95 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 96 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 97 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 98 | #include <linux/cgroup_subsys.h> |
| 99 | }; |
| 100 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 101 | #define MAX_CGROUP_ROOT_NAMELEN 64 |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
| 104 | * A cgroupfs_root represents the root of a cgroup hierarchy, |
| 105 | * and may be associated with a superblock to form an active |
| 106 | * hierarchy |
| 107 | */ |
| 108 | struct cgroupfs_root { |
| 109 | struct super_block *sb; |
| 110 | |
| 111 | /* |
| 112 | * The bitmask of subsystems intended to be attached to this |
| 113 | * hierarchy |
| 114 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 115 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 116 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 117 | /* Unique id for this hierarchy. */ |
| 118 | int hierarchy_id; |
| 119 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 120 | /* The bitmask of subsystems currently attached to this hierarchy */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 121 | unsigned long actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | |
| 123 | /* A list running through the attached subsystems */ |
| 124 | struct list_head subsys_list; |
| 125 | |
| 126 | /* The root cgroup for this hierarchy */ |
| 127 | struct cgroup top_cgroup; |
| 128 | |
| 129 | /* Tracks how many cgroups are currently defined in hierarchy.*/ |
| 130 | int number_of_cgroups; |
| 131 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 132 | /* A list running through the active hierarchies */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 133 | struct list_head root_list; |
| 134 | |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 135 | /* All cgroups on this root, cgroup_mutex protected */ |
| 136 | struct list_head allcg_list; |
| 137 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 138 | /* Hierarchy-specific flags */ |
| 139 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 140 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 141 | /* IDs for cgroups in this hierarchy */ |
| 142 | struct ida cgroup_ida; |
| 143 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 144 | /* The path to use for release notifications. */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 145 | char release_agent_path[PATH_MAX]; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 146 | |
| 147 | /* The name for this hierarchy - may be empty */ |
| 148 | char name[MAX_CGROUP_ROOT_NAMELEN]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 151 | /* |
| 152 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 153 | * subsystems that are otherwise unattached - it never has more than a |
| 154 | * single cgroup, and all tasks are part of that cgroup. |
| 155 | */ |
| 156 | static struct cgroupfs_root rootnode; |
| 157 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 158 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 159 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 160 | */ |
| 161 | struct cfent { |
| 162 | struct list_head node; |
| 163 | struct dentry *dentry; |
| 164 | struct cftype *type; |
| 165 | }; |
| 166 | |
| 167 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 168 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 169 | * cgroup_subsys->use_id != 0. |
| 170 | */ |
| 171 | #define CSS_ID_MAX (65535) |
| 172 | struct css_id { |
| 173 | /* |
| 174 | * The css to which this ID points. This pointer is set to valid value |
| 175 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 176 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 177 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 178 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 179 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 180 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 181 | /* |
| 182 | * ID of this css. |
| 183 | */ |
| 184 | unsigned short id; |
| 185 | /* |
| 186 | * Depth in hierarchy which this ID belongs to. |
| 187 | */ |
| 188 | unsigned short depth; |
| 189 | /* |
| 190 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 191 | */ |
| 192 | struct rcu_head rcu_head; |
| 193 | /* |
| 194 | * Hierarchy of CSS ID belongs to. |
| 195 | */ |
| 196 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 197 | }; |
| 198 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 199 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 200 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 201 | */ |
| 202 | struct cgroup_event { |
| 203 | /* |
| 204 | * Cgroup which the event belongs to. |
| 205 | */ |
| 206 | struct cgroup *cgrp; |
| 207 | /* |
| 208 | * Control file which the event associated. |
| 209 | */ |
| 210 | struct cftype *cft; |
| 211 | /* |
| 212 | * eventfd to signal userspace about the event. |
| 213 | */ |
| 214 | struct eventfd_ctx *eventfd; |
| 215 | /* |
| 216 | * Each of these stored in a list by the cgroup. |
| 217 | */ |
| 218 | struct list_head list; |
| 219 | /* |
| 220 | * All fields below needed to unregister event when |
| 221 | * userspace closes eventfd. |
| 222 | */ |
| 223 | poll_table pt; |
| 224 | wait_queue_head_t *wqh; |
| 225 | wait_queue_t wait; |
| 226 | struct work_struct remove; |
| 227 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 228 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 229 | /* The list of hierarchy roots */ |
| 230 | |
| 231 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 232 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 233 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 234 | static DEFINE_IDA(hierarchy_ida); |
| 235 | static int next_hierarchy_id; |
| 236 | static DEFINE_SPINLOCK(hierarchy_id_lock); |
| 237 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 238 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 239 | #define dummytop (&rootnode.top_cgroup) |
| 240 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 241 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 242 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 243 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 244 | * check for fork/exit handlers to call. This avoids us having to do |
| 245 | * extra work in the fork/exit path if none of the subsystems need to |
| 246 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 247 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 248 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 249 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 250 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 251 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 252 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 253 | |
Paul E. McKenney | d11c563 | 2010-02-22 17:04:50 -0800 | [diff] [blame] | 254 | #ifdef CONFIG_PROVE_LOCKING |
| 255 | int cgroup_lock_is_held(void) |
| 256 | { |
| 257 | return lockdep_is_held(&cgroup_mutex); |
| 258 | } |
| 259 | #else /* #ifdef CONFIG_PROVE_LOCKING */ |
| 260 | int cgroup_lock_is_held(void) |
| 261 | { |
| 262 | return mutex_is_locked(&cgroup_mutex); |
| 263 | } |
| 264 | #endif /* #else #ifdef CONFIG_PROVE_LOCKING */ |
| 265 | |
| 266 | EXPORT_SYMBOL_GPL(cgroup_lock_is_held); |
| 267 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 268 | static int css_unbias_refcnt(int refcnt) |
| 269 | { |
| 270 | return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS; |
| 271 | } |
| 272 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 273 | /* the current nr of refs, always >= 0 whether @css is deactivated or not */ |
| 274 | static int css_refcnt(struct cgroup_subsys_state *css) |
| 275 | { |
| 276 | int v = atomic_read(&css->refcnt); |
| 277 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 278 | return css_unbias_refcnt(v); |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 281 | /* convenient tests for these bits */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 282 | inline int cgroup_is_removed(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 283 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 284 | return test_bit(CGRP_REMOVED, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* bits in struct cgroupfs_root flags field */ |
| 288 | enum { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 289 | ROOT_NOPREFIX, /* mounted subsystems have no named prefix */ |
| 290 | ROOT_XATTR, /* supports extended attributes */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 293 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 294 | { |
| 295 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 296 | (1 << CGRP_RELEASABLE) | |
| 297 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 298 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 301 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 302 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 303 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 306 | /* |
| 307 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 308 | * an active hierarchy |
| 309 | */ |
| 310 | #define for_each_subsys(_root, _ss) \ |
| 311 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 312 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 313 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 314 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 315 | list_for_each_entry(_root, &roots, root_list) |
| 316 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 317 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 318 | { |
| 319 | return dentry->d_fsdata; |
| 320 | } |
| 321 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 322 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 323 | { |
| 324 | return dentry->d_fsdata; |
| 325 | } |
| 326 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 327 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 328 | { |
| 329 | return __d_cfe(dentry)->type; |
| 330 | } |
| 331 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 332 | /* the list of cgroups eligible for automatic release. Protected by |
| 333 | * release_list_lock */ |
| 334 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 335 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 336 | static void cgroup_release_agent(struct work_struct *work); |
| 337 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 338 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 339 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 340 | /* Link structure for associating css_set objects with cgroups */ |
| 341 | struct cg_cgroup_link { |
| 342 | /* |
| 343 | * List running through cg_cgroup_links associated with a |
| 344 | * cgroup, anchored on cgroup->css_sets |
| 345 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 346 | struct list_head cgrp_link_list; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 347 | struct cgroup *cgrp; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 348 | /* |
| 349 | * List running through cg_cgroup_links pointing at a |
| 350 | * single css_set object, anchored on css_set->cg_links |
| 351 | */ |
| 352 | struct list_head cg_link_list; |
| 353 | struct css_set *cg; |
| 354 | }; |
| 355 | |
| 356 | /* The default css_set - used by init and its children prior to any |
| 357 | * hierarchies being mounted. It contains a pointer to the root state |
| 358 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 359 | * reference-counted, to improve performance when child cgroups |
| 360 | * haven't been created. |
| 361 | */ |
| 362 | |
| 363 | static struct css_set init_css_set; |
| 364 | static struct cg_cgroup_link init_css_set_link; |
| 365 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 366 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 367 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 368 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 369 | /* css_set_lock protects the list of css_set objects, and the |
| 370 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 371 | * due to cgroup_iter_start() */ |
| 372 | static DEFINE_RWLOCK(css_set_lock); |
| 373 | static int css_set_count; |
| 374 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 375 | /* |
| 376 | * hash table for cgroup groups. This improves the performance to find |
| 377 | * an existing css_set. This hash doesn't (currently) take into |
| 378 | * account cgroups in empty hierarchies. |
| 379 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 380 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 381 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 382 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 383 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 384 | { |
| 385 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 386 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 387 | |
| 388 | for (i = 0; i < CGROUP_SUBSYS_COUNT; 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 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 401 | static void __put_css_set(struct css_set *cg, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 402 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 403 | struct cg_cgroup_link *link; |
| 404 | struct cg_cgroup_link *saved_link; |
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 | */ |
| 410 | if (atomic_add_unless(&cg->refcount, -1, 1)) |
| 411 | return; |
| 412 | write_lock(&css_set_lock); |
| 413 | if (!atomic_dec_and_test(&cg->refcount)) { |
| 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 */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 419 | hash_del(&cg->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 420 | css_set_count--; |
| 421 | |
| 422 | list_for_each_entry_safe(link, saved_link, &cg->cg_links, |
| 423 | cg_link_list) { |
| 424 | struct cgroup *cgrp = link->cgrp; |
| 425 | list_del(&link->cg_link_list); |
| 426 | list_del(&link->cgrp_link_list); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * We may not be holding cgroup_mutex, and if cgrp->count is |
| 430 | * dropped to 0 the cgroup can be destroyed at any time, hence |
| 431 | * rcu_read_lock is used to keep it alive. |
| 432 | */ |
| 433 | rcu_read_lock(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 434 | if (atomic_dec_and_test(&cgrp->count) && |
| 435 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 436 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 437 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 438 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 439 | } |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 440 | rcu_read_unlock(); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 441 | |
| 442 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 443 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 444 | |
| 445 | write_unlock(&css_set_lock); |
Lai Jiangshan | 30088ad | 2011-03-15 17:53:46 +0800 | [diff] [blame] | 446 | kfree_rcu(cg, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* |
| 450 | * refcounted get/put for css_set objects |
| 451 | */ |
| 452 | static inline void get_css_set(struct css_set *cg) |
| 453 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 454 | atomic_inc(&cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static inline void put_css_set(struct css_set *cg) |
| 458 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 459 | __put_css_set(cg, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 462 | static inline void put_css_set_taskexit(struct css_set *cg) |
| 463 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 464 | __put_css_set(cg, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 467 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 468 | * compare_css_sets - helper function for find_existing_css_set(). |
| 469 | * @cg: candidate css_set being tested |
| 470 | * @old_cg: existing css_set for a task |
| 471 | * @new_cgrp: cgroup that's being entered by the task |
| 472 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 473 | * |
| 474 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 475 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 476 | */ |
| 477 | static bool compare_css_sets(struct css_set *cg, |
| 478 | struct css_set *old_cg, |
| 479 | struct cgroup *new_cgrp, |
| 480 | struct cgroup_subsys_state *template[]) |
| 481 | { |
| 482 | struct list_head *l1, *l2; |
| 483 | |
| 484 | if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { |
| 485 | /* Not all subsystems matched */ |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Compare cgroup pointers in order to distinguish between |
| 491 | * different cgroups in heirarchies with no subsystems. We |
| 492 | * could get by with just this check alone (and skip the |
| 493 | * memcmp above) but on most setups the memcmp check will |
| 494 | * avoid the need for this more expensive check on almost all |
| 495 | * candidates. |
| 496 | */ |
| 497 | |
| 498 | l1 = &cg->cg_links; |
| 499 | l2 = &old_cg->cg_links; |
| 500 | while (1) { |
| 501 | struct cg_cgroup_link *cgl1, *cgl2; |
| 502 | struct cgroup *cg1, *cg2; |
| 503 | |
| 504 | l1 = l1->next; |
| 505 | l2 = l2->next; |
| 506 | /* See if we reached the end - both lists are equal length. */ |
| 507 | if (l1 == &cg->cg_links) { |
| 508 | BUG_ON(l2 != &old_cg->cg_links); |
| 509 | break; |
| 510 | } else { |
| 511 | BUG_ON(l2 == &old_cg->cg_links); |
| 512 | } |
| 513 | /* Locate the cgroups associated with these links. */ |
| 514 | cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); |
| 515 | cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); |
| 516 | cg1 = cgl1->cgrp; |
| 517 | cg2 = cgl2->cgrp; |
| 518 | /* Hierarchies should be linked in the same order. */ |
| 519 | BUG_ON(cg1->root != cg2->root); |
| 520 | |
| 521 | /* |
| 522 | * If this hierarchy is the hierarchy of the cgroup |
| 523 | * that's changing, then we need to check that this |
| 524 | * css_set points to the new cgroup; if it's any other |
| 525 | * hierarchy, then this css_set should point to the |
| 526 | * same cgroup as the old css_set. |
| 527 | */ |
| 528 | if (cg1->root == new_cgrp->root) { |
| 529 | if (cg1 != new_cgrp) |
| 530 | return false; |
| 531 | } else { |
| 532 | if (cg1 != cg2) |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 540 | * find_existing_css_set() is a helper for |
| 541 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 542 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | * |
| 544 | * oldcg: the cgroup group that we're using before the cgroup |
| 545 | * transition |
| 546 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 547 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 548 | * |
| 549 | * template: location in which to build the desired set of subsystem |
| 550 | * state objects for the new cgroup group |
| 551 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 552 | static struct css_set *find_existing_css_set( |
| 553 | struct css_set *oldcg, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 554 | struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 555 | struct cgroup_subsys_state *template[]) |
| 556 | { |
| 557 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 558 | struct cgroupfs_root *root = cgrp->root; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 559 | struct css_set *cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 560 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 562 | /* |
| 563 | * Build the set of subsystem state objects that we want to see in the |
| 564 | * new css_set. while subsystems can change globally, the entries here |
| 565 | * won't change, so no need for locking. |
| 566 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 567 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 568 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 569 | /* Subsystem is in this hierarchy. So we want |
| 570 | * the subsystem state from the new |
| 571 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 572 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 573 | } else { |
| 574 | /* Subsystem is not in this hierarchy, so we |
| 575 | * don't want to change the subsystem state */ |
| 576 | template[i] = oldcg->subsys[i]; |
| 577 | } |
| 578 | } |
| 579 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 580 | key = css_set_hash(template); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 581 | hash_for_each_possible(css_set_table, cg, hlist, key) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 582 | if (!compare_css_sets(cg, oldcg, cgrp, template)) |
| 583 | continue; |
| 584 | |
| 585 | /* This css_set matches what we need */ |
| 586 | return cg; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 587 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* No existing cgroup group matched */ |
| 590 | return NULL; |
| 591 | } |
| 592 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 593 | static void free_cg_links(struct list_head *tmp) |
| 594 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 595 | struct cg_cgroup_link *link; |
| 596 | struct cg_cgroup_link *saved_link; |
| 597 | |
| 598 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 599 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 600 | kfree(link); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 605 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 606 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 607 | * success or a negative error |
| 608 | */ |
| 609 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 610 | { |
| 611 | struct cg_cgroup_link *link; |
| 612 | int i; |
| 613 | INIT_LIST_HEAD(tmp); |
| 614 | for (i = 0; i < count; i++) { |
| 615 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 616 | if (!link) { |
| 617 | free_cg_links(tmp); |
| 618 | return -ENOMEM; |
| 619 | } |
| 620 | list_add(&link->cgrp_link_list, tmp); |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 625 | /** |
| 626 | * link_css_set - a helper function to link a css_set to a cgroup |
| 627 | * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() |
| 628 | * @cg: the css_set to be linked |
| 629 | * @cgrp: the destination cgroup |
| 630 | */ |
| 631 | static void link_css_set(struct list_head *tmp_cg_links, |
| 632 | struct css_set *cg, struct cgroup *cgrp) |
| 633 | { |
| 634 | struct cg_cgroup_link *link; |
| 635 | |
| 636 | BUG_ON(list_empty(tmp_cg_links)); |
| 637 | link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, |
| 638 | cgrp_link_list); |
| 639 | link->cg = cg; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 640 | link->cgrp = cgrp; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 641 | atomic_inc(&cgrp->count); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 642 | list_move(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 643 | /* |
| 644 | * Always add links to the tail of the list so that the list |
| 645 | * is sorted by order of hierarchy creation |
| 646 | */ |
| 647 | list_add_tail(&link->cg_link_list, &cg->cg_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 650 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 651 | * find_css_set() takes an existing cgroup group and a |
| 652 | * cgroup object, and returns a css_set object that's |
| 653 | * equivalent to the old group, but with the given cgroup |
| 654 | * substituted into the appropriate hierarchy. Must be called with |
| 655 | * cgroup_mutex held |
| 656 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 657 | static struct css_set *find_css_set( |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 658 | struct css_set *oldcg, struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 | { |
| 660 | struct css_set *res; |
| 661 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 662 | |
| 663 | struct list_head tmp_cg_links; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 664 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 665 | struct cg_cgroup_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 666 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 667 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 668 | /* First see if we already have a cgroup group that matches |
| 669 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 670 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 671 | res = find_existing_css_set(oldcg, cgrp, template); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | if (res) |
| 673 | get_css_set(res); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 674 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | |
| 676 | if (res) |
| 677 | return res; |
| 678 | |
| 679 | res = kmalloc(sizeof(*res), GFP_KERNEL); |
| 680 | if (!res) |
| 681 | return NULL; |
| 682 | |
| 683 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 684 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
| 685 | kfree(res); |
| 686 | return NULL; |
| 687 | } |
| 688 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 689 | atomic_set(&res->refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 690 | INIT_LIST_HEAD(&res->cg_links); |
| 691 | INIT_LIST_HEAD(&res->tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 692 | INIT_HLIST_NODE(&res->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 693 | |
| 694 | /* Copy the set of subsystem state objects generated in |
| 695 | * find_existing_css_set() */ |
| 696 | memcpy(res->subsys, template, sizeof(res->subsys)); |
| 697 | |
| 698 | write_lock(&css_set_lock); |
| 699 | /* Add reference counts and links from the new css_set. */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 700 | list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { |
| 701 | struct cgroup *c = link->cgrp; |
| 702 | if (c->root == cgrp->root) |
| 703 | c = cgrp; |
| 704 | link_css_set(&tmp_cg_links, res, c); |
| 705 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 706 | |
| 707 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 708 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 709 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 710 | |
| 711 | /* Add this cgroup group to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 712 | key = css_set_hash(res->subsys); |
| 713 | hash_add(css_set_table, &res->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 714 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 715 | write_unlock(&css_set_lock); |
| 716 | |
| 717 | return res; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 720 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 721 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 722 | * called with cgroup_mutex held. |
| 723 | */ |
| 724 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 725 | struct cgroupfs_root *root) |
| 726 | { |
| 727 | struct css_set *css; |
| 728 | struct cgroup *res = NULL; |
| 729 | |
| 730 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 731 | read_lock(&css_set_lock); |
| 732 | /* |
| 733 | * No need to lock the task - since we hold cgroup_mutex the |
| 734 | * task can't change groups, so the only thing that can happen |
| 735 | * is that it exits and its css is set back to init_css_set. |
| 736 | */ |
| 737 | css = task->cgroups; |
| 738 | if (css == &init_css_set) { |
| 739 | res = &root->top_cgroup; |
| 740 | } else { |
| 741 | struct cg_cgroup_link *link; |
| 742 | list_for_each_entry(link, &css->cg_links, cg_link_list) { |
| 743 | struct cgroup *c = link->cgrp; |
| 744 | if (c->root == root) { |
| 745 | res = c; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | read_unlock(&css_set_lock); |
| 751 | BUG_ON(!res); |
| 752 | return res; |
| 753 | } |
| 754 | |
| 755 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 756 | * There is one global cgroup mutex. We also require taking |
| 757 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 758 | * See "The task_lock() exception", at the end of this comment. |
| 759 | * |
| 760 | * A task must hold cgroup_mutex to modify cgroups. |
| 761 | * |
| 762 | * Any task can increment and decrement the count field without lock. |
| 763 | * So in general, code holding cgroup_mutex can't rely on the count |
| 764 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 765 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 766 | * means that no tasks are currently attached, therefore there is no |
| 767 | * way a task attached to that cgroup can fork (the other way to |
| 768 | * increment the count). So code holding cgroup_mutex can safely |
| 769 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 770 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 771 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 772 | * needs that mutex. |
| 773 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 774 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 775 | * (usually) take cgroup_mutex. These are the two most performance |
| 776 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 777 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 778 | * 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] | 779 | * to the release agent with the name of the cgroup (path relative to |
| 780 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 781 | * |
| 782 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 783 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 784 | * tasks in the system use _some_ cgroup, and since there is always at |
| 785 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 786 | * always has either children cgroups and/or using tasks. So we don't |
| 787 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 788 | * |
| 789 | * The task_lock() exception |
| 790 | * |
| 791 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 792 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 793 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 794 | * several performance critical places that need to reference |
| 795 | * task->cgroup without the expense of grabbing a system global |
| 796 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 797 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 798 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 799 | * the task_struct routinely used for such matters. |
| 800 | * |
| 801 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 802 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | */ |
| 804 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 805 | /** |
| 806 | * cgroup_lock - lock out any changes to cgroup structures |
| 807 | * |
| 808 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 809 | void cgroup_lock(void) |
| 810 | { |
| 811 | mutex_lock(&cgroup_mutex); |
| 812 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 813 | EXPORT_SYMBOL_GPL(cgroup_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 814 | |
| 815 | /** |
| 816 | * cgroup_unlock - release lock on cgroup changes |
| 817 | * |
| 818 | * Undo the lock taken in a previous cgroup_lock() call. |
| 819 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 820 | void cgroup_unlock(void) |
| 821 | { |
| 822 | mutex_unlock(&cgroup_mutex); |
| 823 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 824 | EXPORT_SYMBOL_GPL(cgroup_unlock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * A couple of forward declarations required, due to cyclic reference loop: |
| 828 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 829 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 830 | * -> cgroup_mkdir. |
| 831 | */ |
| 832 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 833 | 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] | 834 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 835 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 836 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 837 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 838 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 839 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 840 | |
| 841 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 842 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 843 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 844 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 845 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 846 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 847 | struct cgroup *parent, struct cgroup *child); |
| 848 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 849 | 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] | 850 | { |
| 851 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 852 | |
| 853 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 854 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 855 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 856 | inode->i_uid = current_fsuid(); |
| 857 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 858 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 859 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 860 | } |
| 861 | return inode; |
| 862 | } |
| 863 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 864 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 865 | { |
| 866 | struct cgroup_name *name; |
| 867 | |
| 868 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 869 | if (!name) |
| 870 | return NULL; |
| 871 | strcpy(name->name, dentry->d_name.name); |
| 872 | return name; |
| 873 | } |
| 874 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 875 | static void cgroup_free_fn(struct work_struct *work) |
| 876 | { |
| 877 | struct cgroup *cgrp = container_of(work, struct cgroup, free_work); |
| 878 | struct cgroup_subsys *ss; |
| 879 | |
| 880 | mutex_lock(&cgroup_mutex); |
| 881 | /* |
| 882 | * Release the subsystem state objects. |
| 883 | */ |
| 884 | for_each_subsys(cgrp->root, ss) |
| 885 | ss->css_free(cgrp); |
| 886 | |
| 887 | cgrp->root->number_of_cgroups--; |
| 888 | mutex_unlock(&cgroup_mutex); |
| 889 | |
| 890 | /* |
| 891 | * Drop the active superblock reference that we took when we |
| 892 | * created the cgroup |
| 893 | */ |
| 894 | deactivate_super(cgrp->root->sb); |
| 895 | |
| 896 | /* |
| 897 | * if we're getting rid of the cgroup, refcount should ensure |
| 898 | * that there are no pidlists left. |
| 899 | */ |
| 900 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 901 | |
| 902 | simple_xattrs_free(&cgrp->xattrs); |
| 903 | |
| 904 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 905 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 906 | kfree(cgrp); |
| 907 | } |
| 908 | |
| 909 | static void cgroup_free_rcu(struct rcu_head *head) |
| 910 | { |
| 911 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 912 | |
| 913 | schedule_work(&cgrp->free_work); |
| 914 | } |
| 915 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 916 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 917 | { |
| 918 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 919 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 920 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 921 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 922 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 923 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 924 | } else { |
| 925 | struct cfent *cfe = __d_cfe(dentry); |
| 926 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 927 | struct cftype *cft = cfe->type; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 928 | |
| 929 | WARN_ONCE(!list_empty(&cfe->node) && |
| 930 | cgrp != &cgrp->root->top_cgroup, |
| 931 | "cfe still linked for %s\n", cfe->type->name); |
| 932 | kfree(cfe); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 933 | simple_xattrs_free(&cft->xattrs); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | } |
| 935 | iput(inode); |
| 936 | } |
| 937 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 938 | static int cgroup_delete(const struct dentry *d) |
| 939 | { |
| 940 | return 1; |
| 941 | } |
| 942 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 943 | static void remove_dir(struct dentry *d) |
| 944 | { |
| 945 | struct dentry *parent = dget(d->d_parent); |
| 946 | |
| 947 | d_delete(d); |
| 948 | simple_rmdir(parent->d_inode, d); |
| 949 | dput(parent); |
| 950 | } |
| 951 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 952 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 953 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 954 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 955 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 956 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 957 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 958 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 959 | /* |
| 960 | * If we're doing cleanup due to failure of cgroup_create(), |
| 961 | * the corresponding @cfe may not exist. |
| 962 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 963 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 964 | struct dentry *d = cfe->dentry; |
| 965 | |
| 966 | if (cft && cfe->type != cft) |
| 967 | continue; |
| 968 | |
| 969 | dget(d); |
| 970 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 971 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 972 | list_del_init(&cfe->node); |
| 973 | dput(d); |
| 974 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 975 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 979 | /** |
| 980 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 981 | * @dir: directory containing the files |
| 982 | * @base_files: true if the base files should be removed |
| 983 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 984 | */ |
| 985 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 986 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 987 | { |
| 988 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 989 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 990 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 991 | for_each_subsys(cgrp->root, ss) { |
| 992 | struct cftype_set *set; |
| 993 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 994 | continue; |
| 995 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 996 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 997 | } |
| 998 | if (base_files) { |
| 999 | while (!list_empty(&cgrp->files)) |
| 1000 | cgroup_rm_file(cgrp, NULL); |
| 1001 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * NOTE : the dentry must have been dget()'ed |
| 1006 | */ |
| 1007 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 1008 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1009 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1010 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1011 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1012 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1014 | parent = dentry->d_parent; |
| 1015 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 1016 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1018 | spin_unlock(&dentry->d_lock); |
| 1019 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1020 | remove_dir(dentry); |
| 1021 | } |
| 1022 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1023 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1024 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1025 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1026 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1027 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1029 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1031 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1032 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | int i; |
| 1034 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1035 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1036 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1037 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1038 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 1039 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1040 | /* Check that any added subsystems are currently free */ |
| 1041 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1042 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1043 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1044 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1046 | /* |
| 1047 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1048 | * parse_cgroupfs_options should catch that case and refcounts |
| 1049 | * ensure that subsystems won't disappear once selected. |
| 1050 | */ |
| 1051 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1052 | if (ss->root != &rootnode) { |
| 1053 | /* Subsystem isn't free */ |
| 1054 | return -EBUSY; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /* Currently we don't handle adding/removing subsystems when |
| 1059 | * any child cgroups exist. This is theoretically supportable |
| 1060 | * but involves complex error handling, so it's being left until |
| 1061 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1062 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1063 | return -EBUSY; |
| 1064 | |
| 1065 | /* Process each subsystem */ |
| 1066 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1067 | struct cgroup_subsys *ss = subsys[i]; |
| 1068 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1069 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1071 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1072 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | BUG_ON(!dummytop->subsys[i]); |
| 1074 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1075 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1076 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1077 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1078 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1080 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1081 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1082 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1084 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1085 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1086 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1088 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1090 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1091 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1092 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1093 | /* subsystem is now free - drop reference on module */ |
| 1094 | module_put(ss->module); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1095 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1096 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1097 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1098 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1099 | /* |
| 1100 | * a refcount was taken, but we already had one, so |
| 1101 | * drop the extra reference. |
| 1102 | */ |
| 1103 | module_put(ss->module); |
| 1104 | #ifdef CONFIG_MODULE_UNLOAD |
| 1105 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1106 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1107 | } else { |
| 1108 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1109 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1112 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1117 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1118 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1119 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1120 | struct cgroup_subsys *ss; |
| 1121 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1122 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1123 | for_each_subsys(root, ss) |
| 1124 | seq_printf(seq, ",%s", ss->name); |
| 1125 | if (test_bit(ROOT_NOPREFIX, &root->flags)) |
| 1126 | seq_puts(seq, ",noprefix"); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1127 | if (test_bit(ROOT_XATTR, &root->flags)) |
| 1128 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1129 | if (strlen(root->release_agent_path)) |
| 1130 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1131 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1132 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1133 | if (strlen(root->name)) |
| 1134 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1135 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1140 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1141 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1142 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1143 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1144 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1145 | /* User explicitly requested empty subsystem */ |
| 1146 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1147 | |
| 1148 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1149 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1152 | /* |
| 1153 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1154 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1155 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1156 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1157 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1158 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1159 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1160 | char *token, *o = data; |
| 1161 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1162 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1163 | int i; |
| 1164 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1165 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1166 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1167 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1168 | #ifdef CONFIG_CPUSETS |
| 1169 | mask = ~(1UL << cpuset_subsys_id); |
| 1170 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1171 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1172 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1173 | |
| 1174 | while ((token = strsep(&o, ",")) != NULL) { |
| 1175 | if (!*token) |
| 1176 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1177 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1178 | /* Explicitly have no subsystems */ |
| 1179 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1180 | continue; |
| 1181 | } |
| 1182 | if (!strcmp(token, "all")) { |
| 1183 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1184 | if (one_ss) |
| 1185 | return -EINVAL; |
| 1186 | all_ss = true; |
| 1187 | continue; |
| 1188 | } |
| 1189 | if (!strcmp(token, "noprefix")) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1190 | set_bit(ROOT_NOPREFIX, &opts->flags); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1191 | continue; |
| 1192 | } |
| 1193 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1194 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1195 | continue; |
| 1196 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1197 | if (!strcmp(token, "xattr")) { |
| 1198 | set_bit(ROOT_XATTR, &opts->flags); |
| 1199 | continue; |
| 1200 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1201 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1202 | /* Specifying two release agents is forbidden */ |
| 1203 | if (opts->release_agent) |
| 1204 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1205 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1206 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1207 | if (!opts->release_agent) |
| 1208 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1209 | continue; |
| 1210 | } |
| 1211 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1212 | const char *name = token + 5; |
| 1213 | /* Can't specify an empty name */ |
| 1214 | if (!strlen(name)) |
| 1215 | return -EINVAL; |
| 1216 | /* Must match [\w.-]+ */ |
| 1217 | for (i = 0; i < strlen(name); i++) { |
| 1218 | char c = name[i]; |
| 1219 | if (isalnum(c)) |
| 1220 | continue; |
| 1221 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1222 | continue; |
| 1223 | return -EINVAL; |
| 1224 | } |
| 1225 | /* Specifying two names is forbidden */ |
| 1226 | if (opts->name) |
| 1227 | return -EINVAL; |
| 1228 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1229 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1230 | GFP_KERNEL); |
| 1231 | if (!opts->name) |
| 1232 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1233 | |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1238 | struct cgroup_subsys *ss = subsys[i]; |
| 1239 | if (ss == NULL) |
| 1240 | continue; |
| 1241 | if (strcmp(token, ss->name)) |
| 1242 | continue; |
| 1243 | if (ss->disabled) |
| 1244 | continue; |
| 1245 | |
| 1246 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1247 | if (all_ss) |
| 1248 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1249 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1250 | one_ss = true; |
| 1251 | |
| 1252 | break; |
| 1253 | } |
| 1254 | if (i == CGROUP_SUBSYS_COUNT) |
| 1255 | return -ENOENT; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1260 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1261 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1262 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1263 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1264 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1265 | struct cgroup_subsys *ss = subsys[i]; |
| 1266 | if (ss == NULL) |
| 1267 | continue; |
| 1268 | if (ss->disabled) |
| 1269 | continue; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1270 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1274 | /* Consistency checks */ |
| 1275 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1276 | /* |
| 1277 | * Option noprefix was introduced just for backward compatibility |
| 1278 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1279 | * the cpuset subsystem. |
| 1280 | */ |
| 1281 | if (test_bit(ROOT_NOPREFIX, &opts->flags) && |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1282 | (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1283 | return -EINVAL; |
| 1284 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1287 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1288 | return -EINVAL; |
| 1289 | |
| 1290 | /* |
| 1291 | * We either have to specify by name or by subsystems. (So all |
| 1292 | * empty hierarchies must have a name). |
| 1293 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1294 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1295 | return -EINVAL; |
| 1296 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1297 | /* |
| 1298 | * Grab references on all the modules we'll need, so the subsystems |
| 1299 | * don't dance around before rebind_subsystems attaches them. This may |
| 1300 | * take duplicate reference counts on a subsystem that's already used, |
| 1301 | * but rebind_subsystems handles this case. |
| 1302 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1303 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1304 | unsigned long bit = 1UL << i; |
| 1305 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1306 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1307 | continue; |
| 1308 | if (!try_module_get(subsys[i]->module)) { |
| 1309 | module_pin_failed = true; |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | if (module_pin_failed) { |
| 1314 | /* |
| 1315 | * oops, one of the modules was going away. this means that we |
| 1316 | * raced with a module_delete call, and to the user this is |
| 1317 | * essentially a "subsystem doesn't exist" case. |
| 1318 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1319 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1320 | /* drop refcounts only on the ones we took */ |
| 1321 | unsigned long bit = 1UL << i; |
| 1322 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1323 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1324 | continue; |
| 1325 | module_put(subsys[i]->module); |
| 1326 | } |
| 1327 | return -ENOENT; |
| 1328 | } |
| 1329 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1333 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1334 | { |
| 1335 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1336 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1337 | unsigned long bit = 1UL << i; |
| 1338 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1339 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1340 | continue; |
| 1341 | module_put(subsys[i]->module); |
| 1342 | } |
| 1343 | } |
| 1344 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1345 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1346 | { |
| 1347 | int ret = 0; |
| 1348 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1349 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1350 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1351 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1352 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1353 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1354 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1355 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1356 | |
| 1357 | /* See what subsystems are wanted */ |
| 1358 | ret = parse_cgroupfs_options(data, &opts); |
| 1359 | if (ret) |
| 1360 | goto out_unlock; |
| 1361 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1362 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1363 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1364 | task_tgid_nr(current), current->comm); |
| 1365 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1366 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1367 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1368 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1369 | /* Don't allow flags or name to change at remount */ |
| 1370 | if (opts.flags != root->flags || |
| 1371 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1372 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1373 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1374 | goto out_unlock; |
| 1375 | } |
| 1376 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1377 | /* |
| 1378 | * Clear out the files of subsystems that should be removed, do |
| 1379 | * this before rebind_subsystems, since rebind_subsystems may |
| 1380 | * change this hierarchy's subsys_list. |
| 1381 | */ |
| 1382 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1383 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1384 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1385 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1386 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1387 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1388 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1389 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1390 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1392 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1393 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1394 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1395 | if (opts.release_agent) |
| 1396 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1398 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1399 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1400 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1401 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1402 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1403 | return ret; |
| 1404 | } |
| 1405 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1406 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1407 | .statfs = simple_statfs, |
| 1408 | .drop_inode = generic_delete_inode, |
| 1409 | .show_options = cgroup_show_options, |
| 1410 | .remount_fs = cgroup_remount, |
| 1411 | }; |
| 1412 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1413 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1414 | { |
| 1415 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1416 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1417 | INIT_LIST_HEAD(&cgrp->files); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1418 | INIT_LIST_HEAD(&cgrp->css_sets); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1419 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1420 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1421 | INIT_LIST_HEAD(&cgrp->pidlists); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1422 | INIT_WORK(&cgrp->free_work, cgroup_free_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1423 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1424 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1425 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1426 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1427 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1428 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1429 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1430 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1431 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1432 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1433 | INIT_LIST_HEAD(&root->subsys_list); |
| 1434 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1435 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1436 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1437 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1438 | cgrp->name = &root_cgroup_name; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1439 | cgrp->top_cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1440 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1441 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1444 | static bool init_root_id(struct cgroupfs_root *root) |
| 1445 | { |
| 1446 | int ret = 0; |
| 1447 | |
| 1448 | do { |
| 1449 | if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) |
| 1450 | return false; |
| 1451 | spin_lock(&hierarchy_id_lock); |
| 1452 | /* Try to allocate the next unused ID */ |
| 1453 | ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, |
| 1454 | &root->hierarchy_id); |
| 1455 | if (ret == -ENOSPC) |
| 1456 | /* Try again starting from 0 */ |
| 1457 | ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); |
| 1458 | if (!ret) { |
| 1459 | next_hierarchy_id = root->hierarchy_id + 1; |
| 1460 | } else if (ret != -EAGAIN) { |
| 1461 | /* Can only get here if the 31-bit IDR is full ... */ |
| 1462 | BUG_ON(ret); |
| 1463 | } |
| 1464 | spin_unlock(&hierarchy_id_lock); |
| 1465 | } while (ret); |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1469 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1470 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1471 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1472 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1473 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1474 | /* If we asked for a name then it must match */ |
| 1475 | if (opts->name && strcmp(opts->name, root->name)) |
| 1476 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1477 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1478 | /* |
| 1479 | * If we asked for subsystems (or explicitly for no |
| 1480 | * subsystems) then they must match |
| 1481 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1482 | if ((opts->subsys_mask || opts->none) |
| 1483 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1484 | return 0; |
| 1485 | |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1489 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1490 | { |
| 1491 | struct cgroupfs_root *root; |
| 1492 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1493 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1494 | return NULL; |
| 1495 | |
| 1496 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1497 | if (!root) |
| 1498 | return ERR_PTR(-ENOMEM); |
| 1499 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1500 | if (!init_root_id(root)) { |
| 1501 | kfree(root); |
| 1502 | return ERR_PTR(-ENOMEM); |
| 1503 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1504 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1505 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1506 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1507 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1508 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1509 | if (opts->release_agent) |
| 1510 | strcpy(root->release_agent_path, opts->release_agent); |
| 1511 | if (opts->name) |
| 1512 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1513 | if (opts->cpuset_clone_children) |
| 1514 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1515 | return root; |
| 1516 | } |
| 1517 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1518 | static void cgroup_drop_root(struct cgroupfs_root *root) |
| 1519 | { |
| 1520 | if (!root) |
| 1521 | return; |
| 1522 | |
| 1523 | BUG_ON(!root->hierarchy_id); |
| 1524 | spin_lock(&hierarchy_id_lock); |
| 1525 | ida_remove(&hierarchy_ida, root->hierarchy_id); |
| 1526 | spin_unlock(&hierarchy_id_lock); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1527 | ida_destroy(&root->cgroup_ida); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1528 | kfree(root); |
| 1529 | } |
| 1530 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1531 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1532 | { |
| 1533 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1534 | struct cgroup_sb_opts *opts = data; |
| 1535 | |
| 1536 | /* If we don't have a new root, we can't set up a new sb */ |
| 1537 | if (!opts->new_root) |
| 1538 | return -EINVAL; |
| 1539 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1540 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1541 | |
| 1542 | ret = set_anon_super(sb, NULL); |
| 1543 | if (ret) |
| 1544 | return ret; |
| 1545 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1546 | sb->s_fs_info = opts->new_root; |
| 1547 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | |
| 1549 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1550 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1551 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1552 | sb->s_op = &cgroup_ops; |
| 1553 | |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1558 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1559 | static const struct dentry_operations cgroup_dops = { |
| 1560 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1561 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1562 | }; |
| 1563 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 | struct inode *inode = |
| 1565 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1566 | |
| 1567 | if (!inode) |
| 1568 | return -ENOMEM; |
| 1569 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1570 | inode->i_fop = &simple_dir_operations; |
| 1571 | inode->i_op = &cgroup_dir_inode_operations; |
| 1572 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1573 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1574 | sb->s_root = d_make_root(inode); |
| 1575 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1577 | /* for everything else we want ->d_op set */ |
| 1578 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1579 | return 0; |
| 1580 | } |
| 1581 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1582 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1583 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1584 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1585 | { |
| 1586 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1587 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | int ret = 0; |
| 1589 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1590 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1591 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1592 | |
| 1593 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1594 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1595 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1596 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | if (ret) |
| 1598 | goto out_err; |
| 1599 | |
| 1600 | /* |
| 1601 | * Allocate a new cgroup root. We may not need it if we're |
| 1602 | * reusing an existing hierarchy. |
| 1603 | */ |
| 1604 | new_root = cgroup_root_from_opts(&opts); |
| 1605 | if (IS_ERR(new_root)) { |
| 1606 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1607 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1608 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1609 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1611 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1612 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1613 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1614 | ret = PTR_ERR(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1615 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1616 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1619 | root = sb->s_fs_info; |
| 1620 | BUG_ON(!root); |
| 1621 | if (root == opts.new_root) { |
| 1622 | /* We used the new root structure, so this is a new hierarchy */ |
| 1623 | struct list_head tmp_cg_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1624 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1625 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1626 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1627 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1628 | struct css_set *cg; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1629 | |
| 1630 | BUG_ON(sb->s_root != NULL); |
| 1631 | |
| 1632 | ret = cgroup_get_rootdir(sb); |
| 1633 | if (ret) |
| 1634 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1635 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1636 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1637 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1638 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1639 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1640 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1641 | /* Check for name clashes with existing mounts */ |
| 1642 | ret = -EBUSY; |
| 1643 | if (strlen(root->name)) |
| 1644 | for_each_active_root(existing_root) |
| 1645 | if (!strcmp(existing_root->name, root->name)) |
| 1646 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1647 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1648 | /* |
| 1649 | * We're accessing css_set_count without locking |
| 1650 | * css_set_lock here, but that's OK - it can only be |
| 1651 | * increased by someone holding cgroup_lock, and |
| 1652 | * that's us. The worst that can happen is that we |
| 1653 | * have some link structures left over |
| 1654 | */ |
| 1655 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1656 | if (ret) |
| 1657 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1658 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1659 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1660 | if (ret == -EBUSY) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1661 | free_cg_links(&tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1662 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1663 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1664 | /* |
| 1665 | * There must be no failure case after here, since rebinding |
| 1666 | * takes care of subsystems' refcounts, which are explicitly |
| 1667 | * dropped in the failure exit path. |
| 1668 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1669 | |
| 1670 | /* EBUSY should be the only error here */ |
| 1671 | BUG_ON(ret); |
| 1672 | |
| 1673 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1674 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1676 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1677 | root->top_cgroup.dentry = sb->s_root; |
| 1678 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1679 | /* Link the top cgroup in this hierarchy into all |
| 1680 | * the css_set objects */ |
| 1681 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1682 | hash_for_each(css_set_table, i, cg, hlist) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1683 | link_css_set(&tmp_cg_links, cg, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1684 | write_unlock(&css_set_lock); |
| 1685 | |
| 1686 | free_cg_links(&tmp_cg_links); |
| 1687 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1688 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1689 | BUG_ON(root->number_of_cgroups != 1); |
| 1690 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1691 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1692 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1693 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1694 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1695 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1696 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1697 | } else { |
| 1698 | /* |
| 1699 | * We re-used an existing hierarchy - the new root (if |
| 1700 | * any) is not needed |
| 1701 | */ |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1702 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1703 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1704 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1707 | kfree(opts.release_agent); |
| 1708 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1709 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1711 | unlock_drop: |
| 1712 | mutex_unlock(&cgroup_root_mutex); |
| 1713 | mutex_unlock(&cgroup_mutex); |
| 1714 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1715 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1716 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1717 | drop_modules: |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1718 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1719 | out_err: |
| 1720 | kfree(opts.release_agent); |
| 1721 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1722 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1726 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1727 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1728 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1729 | struct cg_cgroup_link *link; |
| 1730 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1731 | |
| 1732 | BUG_ON(!root); |
| 1733 | |
| 1734 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1735 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1736 | |
| 1737 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1738 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1739 | |
| 1740 | /* Rebind all subsystems back to the default hierarchy */ |
| 1741 | ret = rebind_subsystems(root, 0); |
| 1742 | /* Shouldn't be able to fail ... */ |
| 1743 | BUG_ON(ret); |
| 1744 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1745 | /* |
| 1746 | * Release all the links from css_sets to this hierarchy's |
| 1747 | * root cgroup |
| 1748 | */ |
| 1749 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1750 | |
| 1751 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1752 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1753 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1754 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1755 | kfree(link); |
| 1756 | } |
| 1757 | write_unlock(&css_set_lock); |
| 1758 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1759 | if (!list_empty(&root->root_list)) { |
| 1760 | list_del(&root->root_list); |
| 1761 | root_count--; |
| 1762 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1763 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1764 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1765 | mutex_unlock(&cgroup_mutex); |
| 1766 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1767 | simple_xattrs_free(&cgrp->xattrs); |
| 1768 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1769 | kill_litter_super(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1770 | cgroup_drop_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static struct file_system_type cgroup_fs_type = { |
| 1774 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1775 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1776 | .kill_sb = cgroup_kill_sb, |
| 1777 | }; |
| 1778 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1779 | static struct kobject *cgroup_kobj; |
| 1780 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1781 | /** |
| 1782 | * cgroup_path - generate the path of a cgroup |
| 1783 | * @cgrp: the cgroup in question |
| 1784 | * @buf: the buffer to write the path into |
| 1785 | * @buflen: the length of the buffer |
| 1786 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1787 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1788 | * |
| 1789 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1790 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1791 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1792 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1793 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1794 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1795 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1796 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1797 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1798 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1799 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1800 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1801 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1802 | rcu_read_lock(); |
| 1803 | while (cgrp) { |
| 1804 | const char *name = cgroup_name(cgrp); |
| 1805 | int len; |
| 1806 | |
| 1807 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | goto out; |
| 1810 | memcpy(start, name, len); |
| 1811 | |
| 1812 | if (!cgrp->parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1813 | break; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1814 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1815 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1816 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1817 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1818 | |
| 1819 | cgrp = cgrp->parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1820 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1821 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1822 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1823 | out: |
| 1824 | rcu_read_unlock(); |
| 1825 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1826 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1827 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1828 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1829 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1830 | * Control Group taskset |
| 1831 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1832 | struct task_and_cgroup { |
| 1833 | struct task_struct *task; |
| 1834 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1835 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1836 | }; |
| 1837 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1838 | struct cgroup_taskset { |
| 1839 | struct task_and_cgroup single; |
| 1840 | struct flex_array *tc_array; |
| 1841 | int tc_array_len; |
| 1842 | int idx; |
| 1843 | struct cgroup *cur_cgrp; |
| 1844 | }; |
| 1845 | |
| 1846 | /** |
| 1847 | * cgroup_taskset_first - reset taskset and return the first task |
| 1848 | * @tset: taskset of interest |
| 1849 | * |
| 1850 | * @tset iteration is initialized and the first task is returned. |
| 1851 | */ |
| 1852 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1853 | { |
| 1854 | if (tset->tc_array) { |
| 1855 | tset->idx = 0; |
| 1856 | return cgroup_taskset_next(tset); |
| 1857 | } else { |
| 1858 | tset->cur_cgrp = tset->single.cgrp; |
| 1859 | return tset->single.task; |
| 1860 | } |
| 1861 | } |
| 1862 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1863 | |
| 1864 | /** |
| 1865 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1866 | * @tset: taskset of interest |
| 1867 | * |
| 1868 | * Return the next task in @tset. Iteration must have been initialized |
| 1869 | * with cgroup_taskset_first(). |
| 1870 | */ |
| 1871 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1872 | { |
| 1873 | struct task_and_cgroup *tc; |
| 1874 | |
| 1875 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1876 | return NULL; |
| 1877 | |
| 1878 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1879 | tset->cur_cgrp = tc->cgrp; |
| 1880 | return tc->task; |
| 1881 | } |
| 1882 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1883 | |
| 1884 | /** |
| 1885 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1886 | * @tset: taskset of interest |
| 1887 | * |
| 1888 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1889 | * function must be preceded by either cgroup_taskset_first() or |
| 1890 | * cgroup_taskset_next(). |
| 1891 | */ |
| 1892 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1893 | { |
| 1894 | return tset->cur_cgrp; |
| 1895 | } |
| 1896 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1897 | |
| 1898 | /** |
| 1899 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1900 | * @tset: taskset of interest |
| 1901 | */ |
| 1902 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1903 | { |
| 1904 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1905 | } |
| 1906 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1907 | |
| 1908 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1909 | /* |
| 1910 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1911 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1912 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | */ |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1914 | static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp, |
| 1915 | struct task_struct *tsk, struct css_set *newcg) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1916 | { |
| 1917 | struct css_set *oldcg; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1918 | |
| 1919 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1920 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1921 | * setting such that we can't race against cgroup_exit() changing the |
| 1922 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1923 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1924 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1925 | oldcg = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1926 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1927 | task_lock(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1928 | rcu_assign_pointer(tsk->cgroups, newcg); |
| 1929 | task_unlock(tsk); |
| 1930 | |
| 1931 | /* Update the css_set linked lists if we're using them */ |
| 1932 | write_lock(&css_set_lock); |
| 1933 | if (!list_empty(&tsk->cg_list)) |
| 1934 | list_move(&tsk->cg_list, &newcg->tasks); |
| 1935 | write_unlock(&css_set_lock); |
| 1936 | |
| 1937 | /* |
| 1938 | * We just gained a reference on oldcg by taking it from the task. As |
| 1939 | * trading it for newcg is protected by cgroup_mutex, we're safe to drop |
| 1940 | * it here; it will be freed under RCU. |
| 1941 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | set_bit(CGRP_RELEASABLE, &oldcgrp->flags); |
Daisuke Nishimura | 1f5320d | 2012-10-04 16:37:16 +0900 | [diff] [blame] | 1943 | put_css_set(oldcg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1946 | /** |
| 1947 | * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp' |
| 1948 | * @cgrp: the cgroup the task is attaching to |
| 1949 | * @tsk: the task to be attached |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1950 | * |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1951 | * Call with cgroup_mutex and threadgroup locked. May take task_lock of |
| 1952 | * @tsk during call. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1953 | */ |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 1954 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1955 | { |
Tejun Heo | 8f12191 | 2012-03-29 22:03:33 -0700 | [diff] [blame] | 1956 | int retval = 0; |
Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1957 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1958 | struct cgroup *oldcgrp; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1959 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1960 | struct cgroup_taskset tset = { }; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1961 | struct css_set *newcg; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1962 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1963 | /* @tsk either already exited or can't exit until the end */ |
| 1964 | if (tsk->flags & PF_EXITING) |
| 1965 | return -ESRCH; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* Nothing to do if the task is already in that cgroup */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 1968 | oldcgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1969 | if (cgrp == oldcgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1970 | return 0; |
| 1971 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1972 | tset.single.task = tsk; |
| 1973 | tset.single.cgrp = oldcgrp; |
| 1974 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1975 | for_each_subsys(root, ss) { |
| 1976 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1977 | retval = ss->can_attach(cgrp, &tset); |
Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1978 | if (retval) { |
| 1979 | /* |
| 1980 | * Remember on which subsystem the can_attach() |
| 1981 | * failed, so that we only call cancel_attach() |
| 1982 | * against the subsystems whose can_attach() |
| 1983 | * succeeded. (See below) |
| 1984 | */ |
| 1985 | failed_ss = ss; |
| 1986 | goto out; |
| 1987 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1991 | newcg = find_css_set(tsk->cgroups, cgrp); |
| 1992 | if (!newcg) { |
| 1993 | retval = -ENOMEM; |
Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 1994 | goto out; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1998 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 1999 | for_each_subsys(root, ss) { |
Paul Jackson | e18f631 | 2008-02-07 00:13:44 -0800 | [diff] [blame] | 2000 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2001 | ss->attach(cgrp, &tset); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2002 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2003 | |
Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 2004 | out: |
| 2005 | if (retval) { |
| 2006 | for_each_subsys(root, ss) { |
| 2007 | if (ss == failed_ss) |
| 2008 | /* |
| 2009 | * This subsystem was the one that failed the |
| 2010 | * can_attach() check earlier, so we don't need |
| 2011 | * to call cancel_attach() against it or any |
| 2012 | * remaining subsystems. |
| 2013 | */ |
| 2014 | break; |
| 2015 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2016 | ss->cancel_attach(cgrp, &tset); |
Daisuke Nishimura | 2468c72 | 2010-03-10 15:22:03 -0800 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2022 | /** |
Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2023 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2024 | * @from: attach to all cgroups of a given task |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2025 | * @tsk: the task to be attached |
| 2026 | */ |
Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2027 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2028 | { |
| 2029 | struct cgroupfs_root *root; |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2030 | int retval = 0; |
| 2031 | |
| 2032 | cgroup_lock(); |
| 2033 | for_each_active_root(root) { |
Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2034 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2035 | |
| 2036 | retval = cgroup_attach_task(from_cg, tsk); |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2037 | if (retval) |
| 2038 | break; |
| 2039 | } |
| 2040 | cgroup_unlock(); |
| 2041 | |
| 2042 | return retval; |
| 2043 | } |
Michael S. Tsirkin | 31583bb | 2010-09-09 16:37:37 -0700 | [diff] [blame] | 2044 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
Sridhar Samudrala | d7926ee | 2010-05-30 22:24:39 +0200 | [diff] [blame] | 2045 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2046 | /** |
| 2047 | * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup |
| 2048 | * @cgrp: the cgroup to attach to |
| 2049 | * @leader: the threadgroup leader task_struct of the group to be attached |
| 2050 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2051 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
| 2052 | * task_lock of each thread in leader's threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2053 | */ |
Kirill A. Shutemov | 1c6c3fa | 2011-12-27 07:46:25 +0200 | [diff] [blame] | 2054 | static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2055 | { |
| 2056 | int retval, i, group_size; |
| 2057 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2058 | /* guaranteed to be initialized later, but the compiler needs this */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2059 | struct cgroupfs_root *root = cgrp->root; |
| 2060 | /* threadgroup list cursor and array */ |
| 2061 | struct task_struct *tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2062 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2063 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2064 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | |
| 2066 | /* |
| 2067 | * step 0: in order to do expensive, possibly blocking operations for |
| 2068 | * every thread, we cannot iterate the thread group list, since it needs |
| 2069 | * 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] | 2070 | * group - group_rwsem prevents new threads from appearing, and if |
| 2071 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2072 | */ |
| 2073 | group_size = get_nr_threads(leader); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2074 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2075 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2076 | if (!group) |
| 2077 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2078 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
| 2079 | retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL); |
| 2080 | if (retval) |
| 2081 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2083 | tsk = leader; |
| 2084 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2085 | /* |
| 2086 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2087 | * already PF_EXITING could be freed from underneath us unless we |
| 2088 | * take an rcu_read_lock. |
| 2089 | */ |
| 2090 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2091 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2092 | struct task_and_cgroup ent; |
| 2093 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2094 | /* @tsk either already exited or can't exit until the end */ |
| 2095 | if (tsk->flags & PF_EXITING) |
| 2096 | continue; |
| 2097 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2098 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2099 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2100 | ent.task = tsk; |
| 2101 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2102 | /* nothing to do if this task is already in the cgroup */ |
| 2103 | if (ent.cgrp == cgrp) |
| 2104 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2105 | /* |
| 2106 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2107 | * earlier, but it's good form to communicate our expectations. |
| 2108 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2109 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2110 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | i++; |
| 2112 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2113 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2114 | /* remember the number of threads in the array for later. */ |
| 2115 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2116 | tset.tc_array = group; |
| 2117 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2118 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2119 | /* methods shouldn't be called if no task is actually migrating */ |
| 2120 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2121 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2122 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2123 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2124 | /* |
| 2125 | * step 1: check that we can legitimately attach to the cgroup. |
| 2126 | */ |
| 2127 | for_each_subsys(root, ss) { |
| 2128 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2129 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2130 | if (retval) { |
| 2131 | failed_ss = ss; |
| 2132 | goto out_cancel_attach; |
| 2133 | } |
| 2134 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2139 | * we use find_css_set, which allocates a new one if necessary. |
| 2140 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2142 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2143 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2144 | if (!tc->cg) { |
| 2145 | retval = -ENOMEM; |
| 2146 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2151 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2152 | * proceed to move all tasks to the new cgroup. There are no |
| 2153 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2154 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2156 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2157 | cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2158 | } |
| 2159 | /* nothing is sensitive to fork() after this point. */ |
| 2160 | |
| 2161 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2162 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2163 | */ |
| 2164 | for_each_subsys(root, ss) { |
| 2165 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2166 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * step 5: success! and cleanup |
| 2171 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2172 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2173 | out_put_css_set_refs: |
| 2174 | if (retval) { |
| 2175 | for (i = 0; i < group_size; i++) { |
| 2176 | tc = flex_array_get(group, i); |
| 2177 | if (!tc->cg) |
| 2178 | break; |
| 2179 | put_css_set(tc->cg); |
| 2180 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2181 | } |
| 2182 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2183 | if (retval) { |
| 2184 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2185 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2186 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2187 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2188 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2189 | } |
| 2190 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2191 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2192 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2193 | return retval; |
| 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | * 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] | 2198 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2199 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2200 | */ |
| 2201 | 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] | 2202 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2203 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2204 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2205 | int ret; |
| 2206 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2207 | if (!cgroup_lock_live_group(cgrp)) |
| 2208 | return -ENODEV; |
| 2209 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2210 | retry_find_task: |
| 2211 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2212 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2213 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2214 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2215 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2216 | ret= -ESRCH; |
| 2217 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2218 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2219 | /* |
| 2220 | * even if we're attaching all tasks in the thread group, we |
| 2221 | * only need to check permissions on one of them. |
| 2222 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2223 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2224 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2225 | !uid_eq(cred->euid, tcred->uid) && |
| 2226 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2227 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2228 | ret = -EACCES; |
| 2229 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2230 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2231 | } else |
| 2232 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2233 | |
| 2234 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2235 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2236 | |
| 2237 | /* |
| 2238 | * Workqueue threads may acquire PF_THREAD_BOUND and become |
| 2239 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2240 | * with no rt_runtime allocated. Just say no. |
| 2241 | */ |
| 2242 | if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) { |
| 2243 | ret = -EINVAL; |
| 2244 | rcu_read_unlock(); |
| 2245 | goto out_unlock_cgroup; |
| 2246 | } |
| 2247 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2248 | get_task_struct(tsk); |
| 2249 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2250 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2251 | threadgroup_lock(tsk); |
| 2252 | if (threadgroup) { |
| 2253 | if (!thread_group_leader(tsk)) { |
| 2254 | /* |
| 2255 | * a race with de_thread from another thread's exec() |
| 2256 | * may strip us of our leadership, if this happens, |
| 2257 | * there is no choice but to throw this task away and |
| 2258 | * try again; this is |
| 2259 | * "double-double-toil-and-trouble-check locking". |
| 2260 | */ |
| 2261 | threadgroup_unlock(tsk); |
| 2262 | put_task_struct(tsk); |
| 2263 | goto retry_find_task; |
| 2264 | } |
| 2265 | ret = cgroup_attach_proc(cgrp, tsk); |
| 2266 | } else |
| 2267 | ret = cgroup_attach_task(cgrp, tsk); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2268 | threadgroup_unlock(tsk); |
| 2269 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2270 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2271 | out_unlock_cgroup: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2272 | cgroup_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2273 | return ret; |
| 2274 | } |
| 2275 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2276 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2277 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2278 | return attach_task_by_pid(cgrp, pid, false); |
| 2279 | } |
| 2280 | |
| 2281 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2282 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2283 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2286 | /** |
| 2287 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 2288 | * @cgrp: the cgroup to be checked for liveness |
| 2289 | * |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2290 | * On success, returns true; the lock should be later released with |
| 2291 | * cgroup_unlock(). On failure returns false with no lock held. |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2292 | */ |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2293 | bool cgroup_lock_live_group(struct cgroup *cgrp) |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2294 | { |
| 2295 | mutex_lock(&cgroup_mutex); |
| 2296 | if (cgroup_is_removed(cgrp)) { |
| 2297 | mutex_unlock(&cgroup_mutex); |
| 2298 | return false; |
| 2299 | } |
| 2300 | return true; |
| 2301 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 2302 | EXPORT_SYMBOL_GPL(cgroup_lock_live_group); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2303 | |
| 2304 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2305 | const char *buffer) |
| 2306 | { |
| 2307 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2308 | if (strlen(buffer) >= PATH_MAX) |
| 2309 | return -EINVAL; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2310 | if (!cgroup_lock_live_group(cgrp)) |
| 2311 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2312 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2313 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2314 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2315 | cgroup_unlock(); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2320 | struct seq_file *seq) |
| 2321 | { |
| 2322 | if (!cgroup_lock_live_group(cgrp)) |
| 2323 | return -ENODEV; |
| 2324 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2325 | seq_putc(seq, '\n'); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2326 | cgroup_unlock(); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2327 | return 0; |
| 2328 | } |
| 2329 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2330 | /* A buffer size big enough for numbers or short strings */ |
| 2331 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2332 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2333 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2334 | struct file *file, |
| 2335 | const char __user *userbuf, |
| 2336 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2337 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2338 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2339 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2340 | char *end; |
| 2341 | |
| 2342 | if (!nbytes) |
| 2343 | return -EINVAL; |
| 2344 | if (nbytes >= sizeof(buffer)) |
| 2345 | return -E2BIG; |
| 2346 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2347 | return -EFAULT; |
| 2348 | |
| 2349 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2350 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2351 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2352 | if (*end) |
| 2353 | return -EINVAL; |
| 2354 | retval = cft->write_u64(cgrp, cft, val); |
| 2355 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2356 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2357 | if (*end) |
| 2358 | return -EINVAL; |
| 2359 | retval = cft->write_s64(cgrp, cft, val); |
| 2360 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2361 | if (!retval) |
| 2362 | retval = nbytes; |
| 2363 | return retval; |
| 2364 | } |
| 2365 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2366 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2367 | struct file *file, |
| 2368 | const char __user *userbuf, |
| 2369 | size_t nbytes, loff_t *unused_ppos) |
| 2370 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2371 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2372 | int retval = 0; |
| 2373 | size_t max_bytes = cft->max_write_len; |
| 2374 | char *buffer = local_buffer; |
| 2375 | |
| 2376 | if (!max_bytes) |
| 2377 | max_bytes = sizeof(local_buffer) - 1; |
| 2378 | if (nbytes >= max_bytes) |
| 2379 | return -E2BIG; |
| 2380 | /* Allocate a dynamic buffer if we need one */ |
| 2381 | if (nbytes >= sizeof(local_buffer)) { |
| 2382 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2383 | if (buffer == NULL) |
| 2384 | return -ENOMEM; |
| 2385 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2386 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2387 | retval = -EFAULT; |
| 2388 | goto out; |
| 2389 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2390 | |
| 2391 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2392 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2393 | if (!retval) |
| 2394 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2395 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2396 | if (buffer != local_buffer) |
| 2397 | kfree(buffer); |
| 2398 | return retval; |
| 2399 | } |
| 2400 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2401 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2402 | size_t nbytes, loff_t *ppos) |
| 2403 | { |
| 2404 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2405 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2406 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2407 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2408 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2409 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2410 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2411 | if (cft->write_u64 || cft->write_s64) |
| 2412 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2413 | if (cft->write_string) |
| 2414 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2415 | if (cft->trigger) { |
| 2416 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2417 | return ret ? ret : nbytes; |
| 2418 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2419 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2422 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2423 | struct file *file, |
| 2424 | char __user *buf, size_t nbytes, |
| 2425 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2426 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2427 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2428 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2429 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2430 | |
| 2431 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2432 | } |
| 2433 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2434 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2435 | struct file *file, |
| 2436 | char __user *buf, size_t nbytes, |
| 2437 | loff_t *ppos) |
| 2438 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2439 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2440 | s64 val = cft->read_s64(cgrp, cft); |
| 2441 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2442 | |
| 2443 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2444 | } |
| 2445 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2446 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2447 | size_t nbytes, loff_t *ppos) |
| 2448 | { |
| 2449 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2450 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2451 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2452 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2453 | return -ENODEV; |
| 2454 | |
| 2455 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2456 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2457 | if (cft->read_u64) |
| 2458 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2459 | if (cft->read_s64) |
| 2460 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2461 | return -EINVAL; |
| 2462 | } |
| 2463 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2464 | /* |
| 2465 | * seqfile ops/methods for returning structured data. Currently just |
| 2466 | * supports string->u64 maps, but can be extended in future. |
| 2467 | */ |
| 2468 | |
| 2469 | struct cgroup_seqfile_state { |
| 2470 | struct cftype *cft; |
| 2471 | struct cgroup *cgroup; |
| 2472 | }; |
| 2473 | |
| 2474 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2475 | { |
| 2476 | struct seq_file *sf = cb->state; |
| 2477 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2478 | } |
| 2479 | |
| 2480 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2481 | { |
| 2482 | struct cgroup_seqfile_state *state = m->private; |
| 2483 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2484 | if (cft->read_map) { |
| 2485 | struct cgroup_map_cb cb = { |
| 2486 | .fill = cgroup_map_add, |
| 2487 | .state = m, |
| 2488 | }; |
| 2489 | return cft->read_map(state->cgroup, cft, &cb); |
| 2490 | } |
| 2491 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2494 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2495 | { |
| 2496 | struct seq_file *seq = file->private_data; |
| 2497 | kfree(seq->private); |
| 2498 | return single_release(inode, file); |
| 2499 | } |
| 2500 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2501 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2502 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2503 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2504 | .llseek = seq_lseek, |
| 2505 | .release = cgroup_seqfile_release, |
| 2506 | }; |
| 2507 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2508 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2509 | { |
| 2510 | int err; |
| 2511 | struct cftype *cft; |
| 2512 | |
| 2513 | err = generic_file_open(inode, file); |
| 2514 | if (err) |
| 2515 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2516 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2517 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2518 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2519 | struct cgroup_seqfile_state *state = |
| 2520 | kzalloc(sizeof(*state), GFP_USER); |
| 2521 | if (!state) |
| 2522 | return -ENOMEM; |
| 2523 | state->cft = cft; |
| 2524 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2525 | file->f_op = &cgroup_seqfile_operations; |
| 2526 | err = single_open(file, cgroup_seqfile_show, state); |
| 2527 | if (err < 0) |
| 2528 | kfree(state); |
| 2529 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2530 | err = cft->open(inode, file); |
| 2531 | else |
| 2532 | err = 0; |
| 2533 | |
| 2534 | return err; |
| 2535 | } |
| 2536 | |
| 2537 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2538 | { |
| 2539 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2540 | if (cft->release) |
| 2541 | return cft->release(inode, file); |
| 2542 | return 0; |
| 2543 | } |
| 2544 | |
| 2545 | /* |
| 2546 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2547 | */ |
| 2548 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2549 | struct inode *new_dir, struct dentry *new_dentry) |
| 2550 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2551 | int ret; |
| 2552 | struct cgroup_name *name, *old_name; |
| 2553 | struct cgroup *cgrp; |
| 2554 | |
| 2555 | /* |
| 2556 | * It's convinient to use parent dir's i_mutex to protected |
| 2557 | * cgrp->name. |
| 2558 | */ |
| 2559 | lockdep_assert_held(&old_dir->i_mutex); |
| 2560 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2561 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2562 | return -ENOTDIR; |
| 2563 | if (new_dentry->d_inode) |
| 2564 | return -EEXIST; |
| 2565 | if (old_dir != new_dir) |
| 2566 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2567 | |
| 2568 | cgrp = __d_cgrp(old_dentry); |
| 2569 | |
| 2570 | name = cgroup_alloc_name(new_dentry); |
| 2571 | if (!name) |
| 2572 | return -ENOMEM; |
| 2573 | |
| 2574 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2575 | if (ret) { |
| 2576 | kfree(name); |
| 2577 | return ret; |
| 2578 | } |
| 2579 | |
| 2580 | old_name = cgrp->name; |
| 2581 | rcu_assign_pointer(cgrp->name, name); |
| 2582 | |
| 2583 | kfree_rcu(old_name, rcu_head); |
| 2584 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2587 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2588 | { |
| 2589 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2590 | return &__d_cgrp(dentry)->xattrs; |
| 2591 | else |
| 2592 | return &__d_cft(dentry)->xattrs; |
| 2593 | } |
| 2594 | |
| 2595 | static inline int xattr_enabled(struct dentry *dentry) |
| 2596 | { |
| 2597 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
| 2598 | return test_bit(ROOT_XATTR, &root->flags); |
| 2599 | } |
| 2600 | |
| 2601 | static bool is_valid_xattr(const char *name) |
| 2602 | { |
| 2603 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2604 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2605 | return true; |
| 2606 | return false; |
| 2607 | } |
| 2608 | |
| 2609 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2610 | const void *val, size_t size, int flags) |
| 2611 | { |
| 2612 | if (!xattr_enabled(dentry)) |
| 2613 | return -EOPNOTSUPP; |
| 2614 | if (!is_valid_xattr(name)) |
| 2615 | return -EINVAL; |
| 2616 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2617 | } |
| 2618 | |
| 2619 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2620 | { |
| 2621 | if (!xattr_enabled(dentry)) |
| 2622 | return -EOPNOTSUPP; |
| 2623 | if (!is_valid_xattr(name)) |
| 2624 | return -EINVAL; |
| 2625 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2626 | } |
| 2627 | |
| 2628 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2629 | void *buf, size_t size) |
| 2630 | { |
| 2631 | if (!xattr_enabled(dentry)) |
| 2632 | return -EOPNOTSUPP; |
| 2633 | if (!is_valid_xattr(name)) |
| 2634 | return -EINVAL; |
| 2635 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2636 | } |
| 2637 | |
| 2638 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2639 | { |
| 2640 | if (!xattr_enabled(dentry)) |
| 2641 | return -EOPNOTSUPP; |
| 2642 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2643 | } |
| 2644 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2645 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2646 | .read = cgroup_file_read, |
| 2647 | .write = cgroup_file_write, |
| 2648 | .llseek = generic_file_llseek, |
| 2649 | .open = cgroup_file_open, |
| 2650 | .release = cgroup_file_release, |
| 2651 | }; |
| 2652 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2653 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2654 | .setxattr = cgroup_setxattr, |
| 2655 | .getxattr = cgroup_getxattr, |
| 2656 | .listxattr = cgroup_listxattr, |
| 2657 | .removexattr = cgroup_removexattr, |
| 2658 | }; |
| 2659 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2660 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2661 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2662 | .mkdir = cgroup_mkdir, |
| 2663 | .rmdir = cgroup_rmdir, |
| 2664 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2665 | .setxattr = cgroup_setxattr, |
| 2666 | .getxattr = cgroup_getxattr, |
| 2667 | .listxattr = cgroup_listxattr, |
| 2668 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2669 | }; |
| 2670 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2671 | 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] | 2672 | { |
| 2673 | if (dentry->d_name.len > NAME_MAX) |
| 2674 | return ERR_PTR(-ENAMETOOLONG); |
| 2675 | d_add(dentry, NULL); |
| 2676 | return NULL; |
| 2677 | } |
| 2678 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2679 | /* |
| 2680 | * Check if a file is a control file |
| 2681 | */ |
| 2682 | static inline struct cftype *__file_cft(struct file *file) |
| 2683 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2684 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2685 | return ERR_PTR(-EINVAL); |
| 2686 | return __d_cft(file->f_dentry); |
| 2687 | } |
| 2688 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2689 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2690 | struct super_block *sb) |
| 2691 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2692 | struct inode *inode; |
| 2693 | |
| 2694 | if (!dentry) |
| 2695 | return -ENOENT; |
| 2696 | if (dentry->d_inode) |
| 2697 | return -EEXIST; |
| 2698 | |
| 2699 | inode = cgroup_new_inode(mode, sb); |
| 2700 | if (!inode) |
| 2701 | return -ENOMEM; |
| 2702 | |
| 2703 | if (S_ISDIR(mode)) { |
| 2704 | inode->i_op = &cgroup_dir_inode_operations; |
| 2705 | inode->i_fop = &simple_dir_operations; |
| 2706 | |
| 2707 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2708 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2709 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2710 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2711 | /* |
| 2712 | * Control reaches here with cgroup_mutex held. |
| 2713 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2714 | * want to populate it immediately without releasing |
| 2715 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2716 | * yet, trylock will always succeed without affecting |
| 2717 | * lockdep checks. |
| 2718 | */ |
| 2719 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2720 | } else if (S_ISREG(mode)) { |
| 2721 | inode->i_size = 0; |
| 2722 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2723 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2724 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2725 | d_instantiate(dentry, inode); |
| 2726 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2727 | return 0; |
| 2728 | } |
| 2729 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2730 | /** |
| 2731 | * cgroup_file_mode - deduce file mode of a control file |
| 2732 | * @cft: the control file in question |
| 2733 | * |
| 2734 | * returns cft->mode if ->mode is not 0 |
| 2735 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2736 | * returns S_IRUGO if it has only a read handler |
| 2737 | * returns S_IWUSR if it has only a write hander |
| 2738 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2739 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2740 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2741 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2742 | |
| 2743 | if (cft->mode) |
| 2744 | return cft->mode; |
| 2745 | |
| 2746 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2747 | cft->read_map || cft->read_seq_string) |
| 2748 | mode |= S_IRUGO; |
| 2749 | |
| 2750 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2751 | cft->write_string || cft->trigger) |
| 2752 | mode |= S_IWUSR; |
| 2753 | |
| 2754 | return mode; |
| 2755 | } |
| 2756 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2757 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2758 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2759 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2760 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2761 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2762 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2763 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2764 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2765 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2766 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2767 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2768 | simple_xattrs_init(&cft->xattrs); |
| 2769 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2770 | if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2771 | strcpy(name, subsys->name); |
| 2772 | strcat(name, "."); |
| 2773 | } |
| 2774 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2775 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2776 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2777 | |
| 2778 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2779 | if (!cfe) |
| 2780 | return -ENOMEM; |
| 2781 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2782 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2783 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2784 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2785 | goto out; |
| 2786 | } |
| 2787 | |
| 2788 | mode = cgroup_file_mode(cft); |
| 2789 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2790 | if (!error) { |
| 2791 | cfe->type = (void *)cft; |
| 2792 | cfe->dentry = dentry; |
| 2793 | dentry->d_fsdata = cfe; |
| 2794 | list_add_tail(&cfe->node, &parent->files); |
| 2795 | cfe = NULL; |
| 2796 | } |
| 2797 | dput(dentry); |
| 2798 | out: |
| 2799 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2800 | return error; |
| 2801 | } |
| 2802 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2803 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2804 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2805 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2806 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2807 | int err, ret = 0; |
| 2808 | |
| 2809 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2810 | /* does cft->flags tell us to skip this file on @cgrp? */ |
| 2811 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2812 | continue; |
| 2813 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2814 | continue; |
| 2815 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2816 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2817 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2818 | if (err) |
| 2819 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2820 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2821 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2822 | } else { |
| 2823 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2824 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2825 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2826 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2829 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2830 | |
| 2831 | static void cgroup_cfts_prepare(void) |
| 2832 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2833 | { |
| 2834 | /* |
| 2835 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2836 | * the existing cgroups under cgroup_mutex and create files. |
| 2837 | * Instead, we increment reference on all cgroups and build list of |
| 2838 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2839 | * exclusive access to the field. |
| 2840 | */ |
| 2841 | mutex_lock(&cgroup_cft_mutex); |
| 2842 | mutex_lock(&cgroup_mutex); |
| 2843 | } |
| 2844 | |
| 2845 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2846 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2847 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2848 | { |
| 2849 | LIST_HEAD(pending); |
| 2850 | struct cgroup *cgrp, *n; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2851 | |
| 2852 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
| 2853 | if (cfts && ss->root != &rootnode) { |
| 2854 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2855 | dget(cgrp->dentry); |
| 2856 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | mutex_unlock(&cgroup_mutex); |
| 2861 | |
| 2862 | /* |
| 2863 | * All new cgroups will see @cfts update on @ss->cftsets. Add/rm |
| 2864 | * files for all cgroups which were created before. |
| 2865 | */ |
| 2866 | list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { |
| 2867 | struct inode *inode = cgrp->dentry->d_inode; |
| 2868 | |
| 2869 | mutex_lock(&inode->i_mutex); |
| 2870 | mutex_lock(&cgroup_mutex); |
| 2871 | if (!cgroup_is_removed(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2872 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2873 | mutex_unlock(&cgroup_mutex); |
| 2874 | mutex_unlock(&inode->i_mutex); |
| 2875 | |
| 2876 | list_del_init(&cgrp->cft_q_node); |
| 2877 | dput(cgrp->dentry); |
| 2878 | } |
| 2879 | |
| 2880 | mutex_unlock(&cgroup_cft_mutex); |
| 2881 | } |
| 2882 | |
| 2883 | /** |
| 2884 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2885 | * @ss: target cgroup subsystem |
| 2886 | * @cfts: zero-length name terminated array of cftypes |
| 2887 | * |
| 2888 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2889 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2890 | * have them too. This function can be called anytime whether @ss is |
| 2891 | * attached or not. |
| 2892 | * |
| 2893 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2894 | * function currently returns 0 as long as @cfts registration is successful |
| 2895 | * even if some file creation attempts on existing cgroups fail. |
| 2896 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2897 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2898 | { |
| 2899 | struct cftype_set *set; |
| 2900 | |
| 2901 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2902 | if (!set) |
| 2903 | return -ENOMEM; |
| 2904 | |
| 2905 | cgroup_cfts_prepare(); |
| 2906 | set->cfts = cfts; |
| 2907 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2908 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2909 | |
| 2910 | return 0; |
| 2911 | } |
| 2912 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2913 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2914 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2915 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2916 | * @ss: target cgroup subsystem |
| 2917 | * @cfts: zero-length name terminated array of cftypes |
| 2918 | * |
| 2919 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2920 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2921 | * won't have them either. This function can be called anytime whether @ss |
| 2922 | * is attached or not. |
| 2923 | * |
| 2924 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2925 | * registered with @ss. |
| 2926 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2927 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2928 | { |
| 2929 | struct cftype_set *set; |
| 2930 | |
| 2931 | cgroup_cfts_prepare(); |
| 2932 | |
| 2933 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2934 | if (set->cfts == cfts) { |
| 2935 | list_del_init(&set->node); |
| 2936 | cgroup_cfts_commit(ss, cfts, false); |
| 2937 | return 0; |
| 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | cgroup_cfts_commit(ss, NULL, false); |
| 2942 | return -ENOENT; |
| 2943 | } |
| 2944 | |
| 2945 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2946 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2947 | * @cgrp: the cgroup in question |
| 2948 | * |
| 2949 | * Return the number of tasks in the cgroup. |
| 2950 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2951 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2952 | { |
| 2953 | int count = 0; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2954 | struct cg_cgroup_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2955 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2956 | read_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2957 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2958 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2959 | } |
| 2960 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2961 | return count; |
| 2962 | } |
| 2963 | |
| 2964 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2965 | * Advance a list_head iterator. The iterator should be positioned at |
| 2966 | * the start of a css_set |
| 2967 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2968 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2969 | struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2970 | { |
| 2971 | struct list_head *l = it->cg_link; |
| 2972 | struct cg_cgroup_link *link; |
| 2973 | struct css_set *cg; |
| 2974 | |
| 2975 | /* Advance to the next non-empty css_set */ |
| 2976 | do { |
| 2977 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2978 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2979 | it->cg_link = NULL; |
| 2980 | return; |
| 2981 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2982 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2983 | cg = link->cg; |
| 2984 | } while (list_empty(&cg->tasks)); |
| 2985 | it->cg_link = l; |
| 2986 | it->task = cg->tasks.next; |
| 2987 | } |
| 2988 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2989 | /* |
| 2990 | * To reduce the fork() overhead for systems that are not actually |
| 2991 | * using their cgroups capability, we don't maintain the lists running |
| 2992 | * through each css_set to its tasks until we see the list actually |
| 2993 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2994 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2995 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2996 | { |
| 2997 | struct task_struct *p, *g; |
| 2998 | write_lock(&css_set_lock); |
| 2999 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3000 | /* |
| 3001 | * We need tasklist_lock because RCU is not safe against |
| 3002 | * while_each_thread(). Besides, a forking task that has passed |
| 3003 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 3004 | * is not guaranteed to have its child immediately visible in the |
| 3005 | * tasklist if we walk through it with RCU. |
| 3006 | */ |
| 3007 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3008 | do_each_thread(g, p) { |
| 3009 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 3010 | /* |
| 3011 | * We should check if the process is exiting, otherwise |
| 3012 | * it will race with cgroup_exit() in that the list |
| 3013 | * entry won't be deleted though the process has exited. |
| 3014 | */ |
| 3015 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3016 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 3017 | task_unlock(p); |
| 3018 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3019 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3020 | write_unlock(&css_set_lock); |
| 3021 | } |
| 3022 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3023 | /** |
| 3024 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3025 | * @pos: the current position (%NULL to initiate traversal) |
| 3026 | * @cgroup: cgroup whose descendants to walk |
| 3027 | * |
| 3028 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3029 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
| 3030 | */ |
| 3031 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3032 | struct cgroup *cgroup) |
| 3033 | { |
| 3034 | struct cgroup *next; |
| 3035 | |
| 3036 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3037 | |
| 3038 | /* if first iteration, pretend we just visited @cgroup */ |
| 3039 | if (!pos) { |
| 3040 | if (list_empty(&cgroup->children)) |
| 3041 | return NULL; |
| 3042 | pos = cgroup; |
| 3043 | } |
| 3044 | |
| 3045 | /* visit the first child if exists */ |
| 3046 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3047 | if (next) |
| 3048 | return next; |
| 3049 | |
| 3050 | /* no child, visit my or the closest ancestor's next sibling */ |
| 3051 | do { |
| 3052 | next = list_entry_rcu(pos->sibling.next, struct cgroup, |
| 3053 | sibling); |
| 3054 | if (&next->sibling != &pos->parent->children) |
| 3055 | return next; |
| 3056 | |
| 3057 | pos = pos->parent; |
| 3058 | } while (pos != cgroup); |
| 3059 | |
| 3060 | return NULL; |
| 3061 | } |
| 3062 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3063 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3064 | /** |
| 3065 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3066 | * @pos: cgroup of interest |
| 3067 | * |
| 3068 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3069 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3070 | * subtree of @pos. |
| 3071 | */ |
| 3072 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3073 | { |
| 3074 | struct cgroup *last, *tmp; |
| 3075 | |
| 3076 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3077 | |
| 3078 | do { |
| 3079 | last = pos; |
| 3080 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3081 | pos = NULL; |
| 3082 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3083 | pos = tmp; |
| 3084 | } while (pos); |
| 3085 | |
| 3086 | return last; |
| 3087 | } |
| 3088 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3089 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3090 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3091 | { |
| 3092 | struct cgroup *last; |
| 3093 | |
| 3094 | do { |
| 3095 | last = pos; |
| 3096 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3097 | sibling); |
| 3098 | } while (pos); |
| 3099 | |
| 3100 | return last; |
| 3101 | } |
| 3102 | |
| 3103 | /** |
| 3104 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3105 | * @pos: the current position (%NULL to initiate traversal) |
| 3106 | * @cgroup: cgroup whose descendants to walk |
| 3107 | * |
| 3108 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3109 | * descendant to visit for post-order traversal of @cgroup's descendants. |
| 3110 | */ |
| 3111 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3112 | struct cgroup *cgroup) |
| 3113 | { |
| 3114 | struct cgroup *next; |
| 3115 | |
| 3116 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3117 | |
| 3118 | /* if first iteration, visit the leftmost descendant */ |
| 3119 | if (!pos) { |
| 3120 | next = cgroup_leftmost_descendant(cgroup); |
| 3121 | return next != cgroup ? next : NULL; |
| 3122 | } |
| 3123 | |
| 3124 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 3125 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3126 | if (&next->sibling != &pos->parent->children) |
| 3127 | return cgroup_leftmost_descendant(next); |
| 3128 | |
| 3129 | /* no sibling left, visit parent */ |
| 3130 | next = pos->parent; |
| 3131 | return next != cgroup ? next : NULL; |
| 3132 | } |
| 3133 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3134 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3135 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3136 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3137 | { |
| 3138 | /* |
| 3139 | * The first time anyone tries to iterate across a cgroup, |
| 3140 | * we need to enable the list linking each css_set to its |
| 3141 | * tasks, and fix up all existing tasks. |
| 3142 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3143 | if (!use_task_css_set_links) |
| 3144 | cgroup_enable_task_cg_lists(); |
| 3145 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3146 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3147 | it->cg_link = &cgrp->css_sets; |
| 3148 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3151 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3152 | struct cgroup_iter *it) |
| 3153 | { |
| 3154 | struct task_struct *res; |
| 3155 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3156 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3157 | |
| 3158 | /* If the iterator cg is NULL, we have no tasks */ |
| 3159 | if (!it->cg_link) |
| 3160 | return NULL; |
| 3161 | res = list_entry(l, struct task_struct, cg_list); |
| 3162 | /* Advance iterator to find next entry */ |
| 3163 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3164 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 3165 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3166 | /* We reached the end of this task list - move on to |
| 3167 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3168 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3169 | } else { |
| 3170 | it->task = l; |
| 3171 | } |
| 3172 | return res; |
| 3173 | } |
| 3174 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3175 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3176 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3177 | { |
| 3178 | read_unlock(&css_set_lock); |
| 3179 | } |
| 3180 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3181 | static inline int started_after_time(struct task_struct *t1, |
| 3182 | struct timespec *time, |
| 3183 | struct task_struct *t2) |
| 3184 | { |
| 3185 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3186 | if (start_diff > 0) { |
| 3187 | return 1; |
| 3188 | } else if (start_diff < 0) { |
| 3189 | return 0; |
| 3190 | } else { |
| 3191 | /* |
| 3192 | * Arbitrarily, if two processes started at the same |
| 3193 | * time, we'll say that the lower pointer value |
| 3194 | * started first. Note that t2 may have exited by now |
| 3195 | * so this may not be a valid pointer any longer, but |
| 3196 | * that's fine - it still serves to distinguish |
| 3197 | * between two tasks started (effectively) simultaneously. |
| 3198 | */ |
| 3199 | return t1 > t2; |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | /* |
| 3204 | * This function is a callback from heap_insert() and is used to order |
| 3205 | * the heap. |
| 3206 | * In this case we order the heap in descending task start time. |
| 3207 | */ |
| 3208 | static inline int started_after(void *p1, void *p2) |
| 3209 | { |
| 3210 | struct task_struct *t1 = p1; |
| 3211 | struct task_struct *t2 = p2; |
| 3212 | return started_after_time(t1, &t2->start_time, t2); |
| 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3217 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3218 | * |
| 3219 | * Arguments include pointers to callback functions test_task() and |
| 3220 | * process_task(). |
| 3221 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3222 | * and if it returns true, call process_task() for it also. |
| 3223 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3224 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3225 | * but does not lock css_set_lock for the call to process_task(). |
| 3226 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3227 | * creation. |
| 3228 | * It is guaranteed that process_task() will act on every task that |
| 3229 | * is a member of the cgroup for the duration of this call. This |
| 3230 | * function may or may not call process_task() for tasks that exit |
| 3231 | * or move to a different cgroup during the call, or are forked or |
| 3232 | * move into the cgroup during the call. |
| 3233 | * |
| 3234 | * Note that test_task() may be called with locks held, and may in some |
| 3235 | * situations be called multiple times for the same task, so it should |
| 3236 | * be cheap. |
| 3237 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3238 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3239 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3240 | * may cause this function to fail). |
| 3241 | */ |
| 3242 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3243 | { |
| 3244 | int retval, i; |
| 3245 | struct cgroup_iter it; |
| 3246 | struct task_struct *p, *dropped; |
| 3247 | /* Never dereference latest_task, since it's not refcounted */ |
| 3248 | struct task_struct *latest_task = NULL; |
| 3249 | struct ptr_heap tmp_heap; |
| 3250 | struct ptr_heap *heap; |
| 3251 | struct timespec latest_time = { 0, 0 }; |
| 3252 | |
| 3253 | if (scan->heap) { |
| 3254 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3255 | heap = scan->heap; |
| 3256 | heap->gt = &started_after; |
| 3257 | } else { |
| 3258 | /* We need to allocate our own heap memory */ |
| 3259 | heap = &tmp_heap; |
| 3260 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3261 | if (retval) |
| 3262 | /* cannot allocate the heap */ |
| 3263 | return retval; |
| 3264 | } |
| 3265 | |
| 3266 | again: |
| 3267 | /* |
| 3268 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3269 | * to determine which are of interest, and using the scanner's |
| 3270 | * "process_task" callback to process any of them that need an update. |
| 3271 | * Since we don't want to hold any locks during the task updates, |
| 3272 | * gather tasks to be processed in a heap structure. |
| 3273 | * The heap is sorted by descending task start time. |
| 3274 | * If the statically-sized heap fills up, we overflow tasks that |
| 3275 | * started later, and in future iterations only consider tasks that |
| 3276 | * started after the latest task in the previous pass. This |
| 3277 | * guarantees forward progress and that we don't miss any tasks. |
| 3278 | */ |
| 3279 | heap->size = 0; |
| 3280 | cgroup_iter_start(scan->cg, &it); |
| 3281 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3282 | /* |
| 3283 | * Only affect tasks that qualify per the caller's callback, |
| 3284 | * if he provided one |
| 3285 | */ |
| 3286 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3287 | continue; |
| 3288 | /* |
| 3289 | * Only process tasks that started after the last task |
| 3290 | * we processed |
| 3291 | */ |
| 3292 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3293 | continue; |
| 3294 | dropped = heap_insert(heap, p); |
| 3295 | if (dropped == NULL) { |
| 3296 | /* |
| 3297 | * The new task was inserted; the heap wasn't |
| 3298 | * previously full |
| 3299 | */ |
| 3300 | get_task_struct(p); |
| 3301 | } else if (dropped != p) { |
| 3302 | /* |
| 3303 | * The new task was inserted, and pushed out a |
| 3304 | * different task |
| 3305 | */ |
| 3306 | get_task_struct(p); |
| 3307 | put_task_struct(dropped); |
| 3308 | } |
| 3309 | /* |
| 3310 | * Else the new task was newer than anything already in |
| 3311 | * the heap and wasn't inserted |
| 3312 | */ |
| 3313 | } |
| 3314 | cgroup_iter_end(scan->cg, &it); |
| 3315 | |
| 3316 | if (heap->size) { |
| 3317 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3318 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3319 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3320 | latest_time = q->start_time; |
| 3321 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3322 | } |
| 3323 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3324 | scan->process_task(q, scan); |
| 3325 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3326 | } |
| 3327 | /* |
| 3328 | * If we had to process any tasks at all, scan again |
| 3329 | * in case some of them were in the middle of forking |
| 3330 | * children that didn't get processed. |
| 3331 | * Not the most efficient way to do it, but it avoids |
| 3332 | * having to take callback_mutex in the fork path |
| 3333 | */ |
| 3334 | goto again; |
| 3335 | } |
| 3336 | if (heap == &tmp_heap) |
| 3337 | heap_free(&tmp_heap); |
| 3338 | return 0; |
| 3339 | } |
| 3340 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3341 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3342 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3343 | * |
| 3344 | * Reading this file can return large amounts of data if a cgroup has |
| 3345 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3346 | * but we cannot guarantee that the information we produce is correct |
| 3347 | * unless we produce it entirely atomically. |
| 3348 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3349 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3350 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3351 | /* which pidlist file are we talking about? */ |
| 3352 | enum cgroup_filetype { |
| 3353 | CGROUP_FILE_PROCS, |
| 3354 | CGROUP_FILE_TASKS, |
| 3355 | }; |
| 3356 | |
| 3357 | /* |
| 3358 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3359 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3360 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3361 | * to the cgroup. |
| 3362 | */ |
| 3363 | struct cgroup_pidlist { |
| 3364 | /* |
| 3365 | * used to find which pidlist is wanted. doesn't change as long as |
| 3366 | * this particular list stays in the list. |
| 3367 | */ |
| 3368 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3369 | /* array of xids */ |
| 3370 | pid_t *list; |
| 3371 | /* how many elements the above list has */ |
| 3372 | int length; |
| 3373 | /* how many files are using the current array */ |
| 3374 | int use_count; |
| 3375 | /* each of these stored in a list by its cgroup */ |
| 3376 | struct list_head links; |
| 3377 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3378 | struct cgroup *owner; |
| 3379 | /* protects the other fields */ |
| 3380 | struct rw_semaphore mutex; |
| 3381 | }; |
| 3382 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3383 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3384 | * The following two functions "fix" the issue where there are more pids |
| 3385 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3386 | * TODO: replace with a kernel-wide solution to this problem |
| 3387 | */ |
| 3388 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3389 | static void *pidlist_allocate(int count) |
| 3390 | { |
| 3391 | if (PIDLIST_TOO_LARGE(count)) |
| 3392 | return vmalloc(count * sizeof(pid_t)); |
| 3393 | else |
| 3394 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3395 | } |
| 3396 | static void pidlist_free(void *p) |
| 3397 | { |
| 3398 | if (is_vmalloc_addr(p)) |
| 3399 | vfree(p); |
| 3400 | else |
| 3401 | kfree(p); |
| 3402 | } |
| 3403 | static void *pidlist_resize(void *p, int newcount) |
| 3404 | { |
| 3405 | void *newlist; |
| 3406 | /* note: if new alloc fails, old p will still be valid either way */ |
| 3407 | if (is_vmalloc_addr(p)) { |
| 3408 | newlist = vmalloc(newcount * sizeof(pid_t)); |
| 3409 | if (!newlist) |
| 3410 | return NULL; |
| 3411 | memcpy(newlist, p, newcount * sizeof(pid_t)); |
| 3412 | vfree(p); |
| 3413 | } else { |
| 3414 | newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL); |
| 3415 | } |
| 3416 | return newlist; |
| 3417 | } |
| 3418 | |
| 3419 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3420 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
| 3421 | * If the new stripped list is sufficiently smaller and there's enough memory |
| 3422 | * to allocate a new buffer, will let go of the unneeded memory. Returns the |
| 3423 | * number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3424 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3425 | /* is the size difference enough that we should re-allocate the array? */ |
| 3426 | #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new)) |
| 3427 | static int pidlist_uniq(pid_t **p, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3428 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3429 | int src, dest = 1; |
| 3430 | pid_t *list = *p; |
| 3431 | pid_t *newlist; |
| 3432 | |
| 3433 | /* |
| 3434 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3435 | * edge cases first; no work needs to be done for either |
| 3436 | */ |
| 3437 | if (length == 0 || length == 1) |
| 3438 | return length; |
| 3439 | /* src and dest walk down the list; dest counts unique elements */ |
| 3440 | for (src = 1; src < length; src++) { |
| 3441 | /* find next unique element */ |
| 3442 | while (list[src] == list[src-1]) { |
| 3443 | src++; |
| 3444 | if (src == length) |
| 3445 | goto after; |
| 3446 | } |
| 3447 | /* dest always points to where the next unique element goes */ |
| 3448 | list[dest] = list[src]; |
| 3449 | dest++; |
| 3450 | } |
| 3451 | after: |
| 3452 | /* |
| 3453 | * if the length difference is large enough, we want to allocate a |
| 3454 | * smaller buffer to save memory. if this fails due to out of memory, |
| 3455 | * we'll just stay with what we've got. |
| 3456 | */ |
| 3457 | if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3458 | newlist = pidlist_resize(list, dest); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3459 | if (newlist) |
| 3460 | *p = newlist; |
| 3461 | } |
| 3462 | return dest; |
| 3463 | } |
| 3464 | |
| 3465 | static int cmppid(const void *a, const void *b) |
| 3466 | { |
| 3467 | return *(pid_t *)a - *(pid_t *)b; |
| 3468 | } |
| 3469 | |
| 3470 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3471 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3472 | * returns with the lock on that pidlist already held, and takes care |
| 3473 | * of the use count, or returns NULL with no locks held if we're out of |
| 3474 | * memory. |
| 3475 | */ |
| 3476 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3477 | enum cgroup_filetype type) |
| 3478 | { |
| 3479 | struct cgroup_pidlist *l; |
| 3480 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3481 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3482 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3483 | /* |
| 3484 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3485 | * the last ref-holder is trying to remove l from the list at the same |
| 3486 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3487 | * list we find out from under us - compare release_pid_array(). |
| 3488 | */ |
| 3489 | mutex_lock(&cgrp->pidlist_mutex); |
| 3490 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3491 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3492 | /* make sure l doesn't vanish out from under us */ |
| 3493 | down_write(&l->mutex); |
| 3494 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3495 | return l; |
| 3496 | } |
| 3497 | } |
| 3498 | /* entry not found; create a new one */ |
| 3499 | l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 3500 | if (!l) { |
| 3501 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3502 | return l; |
| 3503 | } |
| 3504 | init_rwsem(&l->mutex); |
| 3505 | down_write(&l->mutex); |
| 3506 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3507 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3508 | l->use_count = 0; /* don't increment here */ |
| 3509 | l->list = NULL; |
| 3510 | l->owner = cgrp; |
| 3511 | list_add(&l->links, &cgrp->pidlists); |
| 3512 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3513 | return l; |
| 3514 | } |
| 3515 | |
| 3516 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3517 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3518 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3519 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3520 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3521 | { |
| 3522 | pid_t *array; |
| 3523 | int length; |
| 3524 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3525 | struct cgroup_iter it; |
| 3526 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3527 | struct cgroup_pidlist *l; |
| 3528 | |
| 3529 | /* |
| 3530 | * If cgroup gets more users after we read count, we won't have |
| 3531 | * enough space - tough. This race is indistinguishable to the |
| 3532 | * caller from the case that the additional cgroup users didn't |
| 3533 | * show up until sometime later on. |
| 3534 | */ |
| 3535 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3536 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3537 | if (!array) |
| 3538 | return -ENOMEM; |
| 3539 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3540 | cgroup_iter_start(cgrp, &it); |
| 3541 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3542 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3543 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3544 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3545 | if (type == CGROUP_FILE_PROCS) |
| 3546 | pid = task_tgid_vnr(tsk); |
| 3547 | else |
| 3548 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3549 | if (pid > 0) /* make sure to only use valid results */ |
| 3550 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3551 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3552 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3553 | length = n; |
| 3554 | /* now sort & (if procs) strip out duplicates */ |
| 3555 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3556 | if (type == CGROUP_FILE_PROCS) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3557 | length = pidlist_uniq(&array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3558 | l = cgroup_pidlist_find(cgrp, type); |
| 3559 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3560 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3561 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3562 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3563 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3564 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3565 | l->list = array; |
| 3566 | l->length = length; |
| 3567 | l->use_count++; |
| 3568 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3569 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3573 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3574 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3575 | * @stats: cgroupstats to fill information into |
| 3576 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3577 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3578 | * |
| 3579 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3580 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3581 | */ |
| 3582 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3583 | { |
| 3584 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3585 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3586 | struct cgroup_iter it; |
| 3587 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3588 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3589 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3590 | * Validate dentry by checking the superblock operations, |
| 3591 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3592 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3593 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3594 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3595 | goto err; |
| 3596 | |
| 3597 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3598 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3599 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3600 | cgroup_iter_start(cgrp, &it); |
| 3601 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3602 | switch (tsk->state) { |
| 3603 | case TASK_RUNNING: |
| 3604 | stats->nr_running++; |
| 3605 | break; |
| 3606 | case TASK_INTERRUPTIBLE: |
| 3607 | stats->nr_sleeping++; |
| 3608 | break; |
| 3609 | case TASK_UNINTERRUPTIBLE: |
| 3610 | stats->nr_uninterruptible++; |
| 3611 | break; |
| 3612 | case TASK_STOPPED: |
| 3613 | stats->nr_stopped++; |
| 3614 | break; |
| 3615 | default: |
| 3616 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3617 | stats->nr_io_wait++; |
| 3618 | break; |
| 3619 | } |
| 3620 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3621 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3622 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3623 | err: |
| 3624 | return ret; |
| 3625 | } |
| 3626 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3627 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3628 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3629 | * 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] | 3630 | * 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] | 3631 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3632 | */ |
| 3633 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3634 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3635 | { |
| 3636 | /* |
| 3637 | * Initially we receive a position value that corresponds to |
| 3638 | * one more than the last pid shown (or 0 on the first call or |
| 3639 | * after a seek to the start). Use a binary-search to find the |
| 3640 | * next pid to display, if any |
| 3641 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3642 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3643 | int index = 0, pid = *pos; |
| 3644 | int *iter; |
| 3645 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3646 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3647 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3648 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3649 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3650 | while (index < end) { |
| 3651 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3652 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3653 | index = mid; |
| 3654 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3655 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3656 | index = mid + 1; |
| 3657 | else |
| 3658 | end = mid; |
| 3659 | } |
| 3660 | } |
| 3661 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | return NULL; |
| 3664 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3666 | *pos = *iter; |
| 3667 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3670 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3671 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3672 | struct cgroup_pidlist *l = s->private; |
| 3673 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3674 | } |
| 3675 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3676 | 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] | 3677 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3678 | struct cgroup_pidlist *l = s->private; |
| 3679 | pid_t *p = v; |
| 3680 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3681 | /* |
| 3682 | * Advance to the next pid in the array. If this goes off the |
| 3683 | * end, we're done |
| 3684 | */ |
| 3685 | p++; |
| 3686 | if (p >= end) { |
| 3687 | return NULL; |
| 3688 | } else { |
| 3689 | *pos = *p; |
| 3690 | return p; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3694 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3695 | { |
| 3696 | return seq_printf(s, "%d\n", *(int *)v); |
| 3697 | } |
| 3698 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3699 | /* |
| 3700 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3701 | * independent of whether it's tasks or procs |
| 3702 | */ |
| 3703 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3704 | .start = cgroup_pidlist_start, |
| 3705 | .stop = cgroup_pidlist_stop, |
| 3706 | .next = cgroup_pidlist_next, |
| 3707 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3708 | }; |
| 3709 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3710 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3711 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3712 | /* |
| 3713 | * the case where we're the last user of this particular pidlist will |
| 3714 | * have us remove it from the cgroup's list, which entails taking the |
| 3715 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3716 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3717 | */ |
| 3718 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3719 | down_write(&l->mutex); |
| 3720 | BUG_ON(!l->use_count); |
| 3721 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3722 | /* we're the last user if refcount is 0; remove and free */ |
| 3723 | list_del(&l->links); |
| 3724 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3725 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3726 | put_pid_ns(l->key.ns); |
| 3727 | up_write(&l->mutex); |
| 3728 | kfree(l); |
| 3729 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3730 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3731 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3732 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3735 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3736 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3737 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3738 | if (!(file->f_mode & FMODE_READ)) |
| 3739 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3740 | /* |
| 3741 | * the seq_file will only be initialized if the file was opened for |
| 3742 | * reading; hence we check if it's not null only in that case. |
| 3743 | */ |
| 3744 | l = ((struct seq_file *)file->private_data)->private; |
| 3745 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3746 | return seq_release(inode, file); |
| 3747 | } |
| 3748 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3749 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3750 | .read = seq_read, |
| 3751 | .llseek = seq_lseek, |
| 3752 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3753 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3754 | }; |
| 3755 | |
| 3756 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3757 | * The following functions handle opens on a file that displays a pidlist |
| 3758 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3759 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3760 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3761 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3762 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3763 | { |
| 3764 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3765 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3766 | int retval; |
| 3767 | |
| 3768 | /* Nothing to do for write-only files */ |
| 3769 | if (!(file->f_mode & FMODE_READ)) |
| 3770 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3771 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3772 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3773 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3774 | if (retval) |
| 3775 | return retval; |
| 3776 | /* configure file information */ |
| 3777 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3778 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3779 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3780 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3781 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3782 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3783 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3784 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3785 | return 0; |
| 3786 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3787 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3788 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3789 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3790 | } |
| 3791 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3792 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3793 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3795 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3796 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3797 | struct cftype *cft) |
| 3798 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3799 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3802 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3803 | struct cftype *cft, |
| 3804 | u64 val) |
| 3805 | { |
| 3806 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3807 | if (val) |
| 3808 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3809 | else |
| 3810 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3814 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3815 | * Unregister event and free resources. |
| 3816 | * |
| 3817 | * Gets called from workqueue. |
| 3818 | */ |
| 3819 | static void cgroup_event_remove(struct work_struct *work) |
| 3820 | { |
| 3821 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3822 | remove); |
| 3823 | struct cgroup *cgrp = event->cgrp; |
| 3824 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3825 | remove_wait_queue(event->wqh, &event->wait); |
| 3826 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3827 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3828 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3829 | /* Notify userspace the event is going away. */ |
| 3830 | eventfd_signal(event->eventfd, 1); |
| 3831 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3832 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3833 | kfree(event); |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3834 | dput(cgrp->dentry); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* |
| 3838 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3839 | * |
| 3840 | * Called with wqh->lock held and interrupts disabled. |
| 3841 | */ |
| 3842 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3843 | int sync, void *key) |
| 3844 | { |
| 3845 | struct cgroup_event *event = container_of(wait, |
| 3846 | struct cgroup_event, wait); |
| 3847 | struct cgroup *cgrp = event->cgrp; |
| 3848 | unsigned long flags = (unsigned long)key; |
| 3849 | |
| 3850 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3851 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3852 | * If the event has been detached at cgroup removal, we |
| 3853 | * can simply return knowing the other side will cleanup |
| 3854 | * for us. |
| 3855 | * |
| 3856 | * We can't race against event freeing since the other |
| 3857 | * side will require wqh->lock via remove_wait_queue(), |
| 3858 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3859 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3860 | spin_lock(&cgrp->event_list_lock); |
| 3861 | if (!list_empty(&event->list)) { |
| 3862 | list_del_init(&event->list); |
| 3863 | /* |
| 3864 | * We are in atomic context, but cgroup_event_remove() |
| 3865 | * may sleep, so we have to call it in workqueue. |
| 3866 | */ |
| 3867 | schedule_work(&event->remove); |
| 3868 | } |
| 3869 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | return 0; |
| 3873 | } |
| 3874 | |
| 3875 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3876 | wait_queue_head_t *wqh, poll_table *pt) |
| 3877 | { |
| 3878 | struct cgroup_event *event = container_of(pt, |
| 3879 | struct cgroup_event, pt); |
| 3880 | |
| 3881 | event->wqh = wqh; |
| 3882 | add_wait_queue(wqh, &event->wait); |
| 3883 | } |
| 3884 | |
| 3885 | /* |
| 3886 | * Parse input and register new cgroup event handler. |
| 3887 | * |
| 3888 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3889 | * Interpretation of args is defined by control file implementation. |
| 3890 | */ |
| 3891 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3892 | const char *buffer) |
| 3893 | { |
| 3894 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3895 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3896 | unsigned int efd, cfd; |
| 3897 | struct file *efile = NULL; |
| 3898 | struct file *cfile = NULL; |
| 3899 | char *endp; |
| 3900 | int ret; |
| 3901 | |
| 3902 | efd = simple_strtoul(buffer, &endp, 10); |
| 3903 | if (*endp != ' ') |
| 3904 | return -EINVAL; |
| 3905 | buffer = endp + 1; |
| 3906 | |
| 3907 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3908 | if ((*endp != ' ') && (*endp != '\0')) |
| 3909 | return -EINVAL; |
| 3910 | buffer = endp + 1; |
| 3911 | |
| 3912 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3913 | if (!event) |
| 3914 | return -ENOMEM; |
| 3915 | event->cgrp = cgrp; |
| 3916 | INIT_LIST_HEAD(&event->list); |
| 3917 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3918 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3919 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3920 | |
| 3921 | efile = eventfd_fget(efd); |
| 3922 | if (IS_ERR(efile)) { |
| 3923 | ret = PTR_ERR(efile); |
| 3924 | goto fail; |
| 3925 | } |
| 3926 | |
| 3927 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3928 | if (IS_ERR(event->eventfd)) { |
| 3929 | ret = PTR_ERR(event->eventfd); |
| 3930 | goto fail; |
| 3931 | } |
| 3932 | |
| 3933 | cfile = fget(cfd); |
| 3934 | if (!cfile) { |
| 3935 | ret = -EBADF; |
| 3936 | goto fail; |
| 3937 | } |
| 3938 | |
| 3939 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3940 | /* 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] | 3941 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3942 | if (ret < 0) |
| 3943 | goto fail; |
| 3944 | |
| 3945 | event->cft = __file_cft(cfile); |
| 3946 | if (IS_ERR(event->cft)) { |
| 3947 | ret = PTR_ERR(event->cft); |
| 3948 | goto fail; |
| 3949 | } |
| 3950 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3951 | /* |
| 3952 | * The file to be monitored must be in the same cgroup as |
| 3953 | * cgroup.event_control is. |
| 3954 | */ |
| 3955 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3956 | if (cgrp_cfile != cgrp) { |
| 3957 | ret = -EINVAL; |
| 3958 | goto fail; |
| 3959 | } |
| 3960 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3961 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3962 | ret = -EINVAL; |
| 3963 | goto fail; |
| 3964 | } |
| 3965 | |
| 3966 | ret = event->cft->register_event(cgrp, event->cft, |
| 3967 | event->eventfd, buffer); |
| 3968 | if (ret) |
| 3969 | goto fail; |
| 3970 | |
| 3971 | if (efile->f_op->poll(efile, &event->pt) & POLLHUP) { |
| 3972 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3973 | ret = 0; |
| 3974 | goto fail; |
| 3975 | } |
| 3976 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3977 | /* |
| 3978 | * Events should be removed after rmdir of cgroup directory, but before |
| 3979 | * destroying subsystem state objects. Let's take reference to cgroup |
| 3980 | * directory dentry to do that. |
| 3981 | */ |
| 3982 | dget(cgrp->dentry); |
| 3983 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3984 | spin_lock(&cgrp->event_list_lock); |
| 3985 | list_add(&event->list, &cgrp->event_list); |
| 3986 | spin_unlock(&cgrp->event_list_lock); |
| 3987 | |
| 3988 | fput(cfile); |
| 3989 | fput(efile); |
| 3990 | |
| 3991 | return 0; |
| 3992 | |
| 3993 | fail: |
| 3994 | if (cfile) |
| 3995 | fput(cfile); |
| 3996 | |
| 3997 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 3998 | eventfd_ctx_put(event->eventfd); |
| 3999 | |
| 4000 | if (!IS_ERR_OR_NULL(efile)) |
| 4001 | fput(efile); |
| 4002 | |
| 4003 | kfree(event); |
| 4004 | |
| 4005 | return ret; |
| 4006 | } |
| 4007 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4008 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4009 | struct cftype *cft) |
| 4010 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4011 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4015 | struct cftype *cft, |
| 4016 | u64 val) |
| 4017 | { |
| 4018 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4019 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4020 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4021 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4022 | return 0; |
| 4023 | } |
| 4024 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4025 | /* |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4026 | * for the common functions, 'private' gives the type of file |
| 4027 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4028 | /* for hysterical raisins, we can't put this on the older files */ |
| 4029 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4030 | static struct cftype files[] = { |
| 4031 | { |
| 4032 | .name = "tasks", |
| 4033 | .open = cgroup_tasks_open, |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 4034 | .write_u64 = cgroup_tasks_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4035 | .release = cgroup_pidlist_release, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 4036 | .mode = S_IRUGO | S_IWUSR, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4037 | }, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4038 | { |
| 4039 | .name = CGROUP_FILE_GENERIC_PREFIX "procs", |
| 4040 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4041 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4042 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4043 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4044 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4045 | { |
| 4046 | .name = "notify_on_release", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 4047 | .read_u64 = cgroup_read_notify_on_release, |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 4048 | .write_u64 = cgroup_write_notify_on_release, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4049 | }, |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4050 | { |
| 4051 | .name = CGROUP_FILE_GENERIC_PREFIX "event_control", |
| 4052 | .write_string = cgroup_write_event_control, |
| 4053 | .mode = S_IWUGO, |
| 4054 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4055 | { |
| 4056 | .name = "cgroup.clone_children", |
| 4057 | .read_u64 = cgroup_clone_children_read, |
| 4058 | .write_u64 = cgroup_clone_children_write, |
| 4059 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4060 | { |
| 4061 | .name = "release_agent", |
| 4062 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4063 | .read_seq_string = cgroup_release_agent_show, |
| 4064 | .write_string = cgroup_release_agent_write, |
| 4065 | .max_write_len = PATH_MAX, |
| 4066 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4067 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4068 | }; |
| 4069 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4070 | /** |
| 4071 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4072 | * @cgrp: target cgroup |
| 4073 | * @base_files: true if the base files should be added |
| 4074 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4075 | */ |
| 4076 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4077 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4078 | { |
| 4079 | int err; |
| 4080 | struct cgroup_subsys *ss; |
| 4081 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4082 | if (base_files) { |
| 4083 | err = cgroup_addrm_files(cgrp, NULL, files, true); |
| 4084 | if (err < 0) |
| 4085 | return err; |
| 4086 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4087 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4088 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4089 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4090 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4091 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4092 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4093 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4094 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4095 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4096 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4097 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4098 | /* This cgroup is ready now */ |
| 4099 | for_each_subsys(cgrp->root, ss) { |
| 4100 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4101 | /* |
| 4102 | * Update id->css pointer and make this css visible from |
| 4103 | * CSS ID functions. This pointer will be dereferened |
| 4104 | * from RCU-read-side without locks. |
| 4105 | */ |
| 4106 | if (css->id) |
| 4107 | rcu_assign_pointer(css->id->css, css); |
| 4108 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4109 | |
| 4110 | return 0; |
| 4111 | } |
| 4112 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4113 | static void css_dput_fn(struct work_struct *work) |
| 4114 | { |
| 4115 | struct cgroup_subsys_state *css = |
| 4116 | container_of(work, struct cgroup_subsys_state, dput_work); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4117 | struct dentry *dentry = css->cgroup->dentry; |
| 4118 | struct super_block *sb = dentry->d_sb; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4119 | |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4120 | atomic_inc(&sb->s_active); |
| 4121 | dput(dentry); |
| 4122 | deactivate_super(sb); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4125 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4126 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4127 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4128 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4129 | css->cgroup = cgrp; |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 4130 | atomic_set(&css->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4131 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4132 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4133 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4134 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4135 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4136 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4137 | |
| 4138 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4139 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4140 | * css_put(). dput() requires process context, which css_put() may |
| 4141 | * be called without. @css->dput_work will be used to invoke |
| 4142 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4143 | */ |
| 4144 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4147 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4148 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4149 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4150 | int ret = 0; |
| 4151 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4152 | lockdep_assert_held(&cgroup_mutex); |
| 4153 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4154 | if (ss->css_online) |
| 4155 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4156 | if (!ret) |
| 4157 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4158 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4162 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4163 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4164 | { |
| 4165 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4166 | |
| 4167 | lockdep_assert_held(&cgroup_mutex); |
| 4168 | |
| 4169 | if (!(css->flags & CSS_ONLINE)) |
| 4170 | return; |
| 4171 | |
| 4172 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4173 | * css_offline() should be called with cgroup_mutex unlocked. See |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4174 | * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for |
| 4175 | * details. This temporary unlocking should go away once |
| 4176 | * cgroup_mutex is unexported from controllers. |
| 4177 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4178 | if (ss->css_offline) { |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4179 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4180 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4181 | mutex_lock(&cgroup_mutex); |
| 4182 | } |
| 4183 | |
| 4184 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4185 | } |
| 4186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4187 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4188 | * cgroup_create - create a cgroup |
| 4189 | * @parent: cgroup that will be parent of the new cgroup |
| 4190 | * @dentry: dentry of the new cgroup |
| 4191 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4192 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4193 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4194 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4195 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4196 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4197 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4198 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4199 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | struct cgroupfs_root *root = parent->root; |
| 4201 | int err = 0; |
| 4202 | struct cgroup_subsys *ss; |
| 4203 | struct super_block *sb = root->sb; |
| 4204 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4205 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4206 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4207 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4208 | return -ENOMEM; |
| 4209 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4210 | name = cgroup_alloc_name(dentry); |
| 4211 | if (!name) |
| 4212 | goto err_free_cgrp; |
| 4213 | rcu_assign_pointer(cgrp->name, name); |
| 4214 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4215 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4216 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4217 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4218 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4219 | /* |
| 4220 | * Only live parents can have children. Note that the liveliness |
| 4221 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4222 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4223 | * anyway so that locking is contained inside cgroup proper and we |
| 4224 | * don't get nasty surprises if we ever grow another caller. |
| 4225 | */ |
| 4226 | if (!cgroup_lock_live_group(parent)) { |
| 4227 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4228 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4229 | } |
| 4230 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4231 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4232 | * get deleted on unmount if there are child cgroups. This |
| 4233 | * can be done outside cgroup_mutex, since the sb can't |
| 4234 | * disappear while someone has an open control file on the |
| 4235 | * fs */ |
| 4236 | atomic_inc(&sb->s_active); |
| 4237 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4238 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4239 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4240 | dentry->d_fsdata = cgrp; |
| 4241 | cgrp->dentry = dentry; |
| 4242 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4243 | cgrp->parent = parent; |
| 4244 | cgrp->root = parent->root; |
| 4245 | cgrp->top_cgroup = parent->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4246 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4247 | if (notify_on_release(parent)) |
| 4248 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4249 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4250 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4251 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4252 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4253 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4254 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4255 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4256 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4257 | if (IS_ERR(css)) { |
| 4258 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4259 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4260 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4261 | init_cgroup_css(css, ss, cgrp); |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4262 | if (ss->use_id) { |
| 4263 | err = alloc_css_id(ss, parent, cgrp); |
| 4264 | if (err) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4265 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4266 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4267 | } |
| 4268 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4269 | /* |
| 4270 | * Create directory. cgroup_create_file() returns with the new |
| 4271 | * directory locked on success so that it can be populated without |
| 4272 | * dropping cgroup_mutex. |
| 4273 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4274 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4275 | if (err < 0) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4276 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4277 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4278 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4279 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4280 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4281 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4282 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4283 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4284 | /* each css holds a ref to the cgroup's dentry */ |
| 4285 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4286 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4287 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4288 | /* creation succeeded, notify subsystems */ |
| 4289 | for_each_subsys(root, ss) { |
| 4290 | err = online_css(ss, cgrp); |
| 4291 | if (err) |
| 4292 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4293 | |
| 4294 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4295 | parent->parent) { |
| 4296 | 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", |
| 4297 | current->comm, current->pid, ss->name); |
| 4298 | if (!strcmp(ss->name, "memory")) |
| 4299 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4300 | ss->warned_broken_hierarchy = true; |
| 4301 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4302 | } |
| 4303 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4304 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4305 | if (err) |
| 4306 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4307 | |
| 4308 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4309 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4310 | |
| 4311 | return 0; |
| 4312 | |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4313 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4314 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4315 | if (cgrp->subsys[ss->subsys_id]) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4316 | ss->css_free(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4317 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4318 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4319 | /* Release the reference count that we took on the superblock */ |
| 4320 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4321 | err_free_id: |
| 4322 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4323 | err_free_name: |
| 4324 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4325 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4326 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4327 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4328 | |
| 4329 | err_destroy: |
| 4330 | cgroup_destroy_locked(cgrp); |
| 4331 | mutex_unlock(&cgroup_mutex); |
| 4332 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4333 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4336 | 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] | 4337 | { |
| 4338 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4339 | |
| 4340 | /* the vfs holds inode->i_mutex already */ |
| 4341 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4342 | } |
| 4343 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4344 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4345 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4346 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4347 | struct dentry *d = cgrp->dentry; |
| 4348 | struct cgroup *parent = cgrp->parent; |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4349 | DEFINE_WAIT(wait); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4350 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4351 | struct cgroup_subsys *ss; |
Greg Thelen | 205a872 | 2012-11-28 13:50:44 -0800 | [diff] [blame] | 4352 | LIST_HEAD(tmp_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4353 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4354 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4355 | lockdep_assert_held(&cgroup_mutex); |
| 4356 | |
| 4357 | if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4358 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4359 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4360 | /* |
| 4361 | * Block new css_tryget() by deactivating refcnt and mark @cgrp |
| 4362 | * removed. This makes future css_tryget() and child creation |
| 4363 | * attempts fail thus maintaining the removal conditions verified |
| 4364 | * above. |
| 4365 | */ |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4366 | for_each_subsys(cgrp->root, ss) { |
| 4367 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4368 | |
| 4369 | WARN_ON(atomic_read(&css->refcnt) < 0); |
| 4370 | atomic_add(CSS_DEACT_BIAS, &css->refcnt); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4371 | } |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4372 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 4373 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4374 | /* tell subsystems to initate destruction */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4375 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4376 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4377 | |
| 4378 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4379 | * Put all the base refs. Each css holds an extra reference to the |
| 4380 | * cgroup's dentry and cgroup removal proceeds regardless of css |
| 4381 | * refs. On the last put of each css, whenever that may be, the |
| 4382 | * extra dentry ref is put so that dentry destruction happens only |
| 4383 | * after all css's are released. |
| 4384 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4385 | for_each_subsys(cgrp->root, ss) |
| 4386 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4387 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4388 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4389 | if (!list_empty(&cgrp->release_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4390 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4391 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4392 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4393 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4394 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4395 | list_del_init(&cgrp->allcg_node); |
| 4396 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4397 | dget(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4398 | cgroup_d_remove_dir(d); |
| 4399 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4400 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4401 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4402 | check_for_release(parent); |
| 4403 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4404 | /* |
| 4405 | * Unregister events and notify userspace. |
| 4406 | * Notify userspace about cgroup removing only after rmdir of cgroup |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4407 | * directory to avoid race between userspace and kernelspace. |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4408 | */ |
| 4409 | spin_lock(&cgrp->event_list_lock); |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4410 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 4411 | list_del_init(&event->list); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4412 | schedule_work(&event->remove); |
| 4413 | } |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4414 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4415 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4416 | return 0; |
| 4417 | } |
| 4418 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4419 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4420 | { |
| 4421 | int ret; |
| 4422 | |
| 4423 | mutex_lock(&cgroup_mutex); |
| 4424 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4425 | mutex_unlock(&cgroup_mutex); |
| 4426 | |
| 4427 | return ret; |
| 4428 | } |
| 4429 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4430 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4431 | { |
| 4432 | INIT_LIST_HEAD(&ss->cftsets); |
| 4433 | |
| 4434 | /* |
| 4435 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4436 | * deregistration. |
| 4437 | */ |
| 4438 | if (ss->base_cftypes) { |
| 4439 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4440 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4441 | } |
| 4442 | } |
| 4443 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4444 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4445 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4446 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4447 | |
| 4448 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4449 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4450 | mutex_lock(&cgroup_mutex); |
| 4451 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4452 | /* init base cftset */ |
| 4453 | cgroup_init_cftsets(ss); |
| 4454 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4455 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4456 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4457 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4458 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4459 | /* We don't handle early failures gracefully */ |
| 4460 | BUG_ON(IS_ERR(css)); |
| 4461 | init_cgroup_css(css, ss, dummytop); |
| 4462 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4463 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4464 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4465 | * newly registered, all tasks and hence the |
| 4466 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4467 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4468 | |
| 4469 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4470 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4471 | /* At system boot, before all subsystems have been |
| 4472 | * registered, no tasks have been forked, so we don't |
| 4473 | * need to invoke fork callbacks here. */ |
| 4474 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4475 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4476 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4477 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4478 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4479 | mutex_unlock(&cgroup_mutex); |
| 4480 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4481 | /* this function shouldn't be used with modular subsystems, since they |
| 4482 | * need to register a subsys_id, among other things */ |
| 4483 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4487 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4488 | * @ss: the subsystem to load |
| 4489 | * |
| 4490 | * 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] | 4491 | * 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] | 4492 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4493 | * simpler cgroup_init_subsys. |
| 4494 | */ |
| 4495 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4496 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4497 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4498 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4499 | struct hlist_node *tmp; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4500 | struct css_set *cg; |
| 4501 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4502 | |
| 4503 | /* check name and function validity */ |
| 4504 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4505 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4506 | return -EINVAL; |
| 4507 | |
| 4508 | /* |
| 4509 | * we don't support callbacks in modular subsystems. this check is |
| 4510 | * before the ss->module check for consistency; a subsystem that could |
| 4511 | * be a module should still have no callbacks even if the user isn't |
| 4512 | * compiling it as one. |
| 4513 | */ |
| 4514 | if (ss->fork || ss->exit) |
| 4515 | return -EINVAL; |
| 4516 | |
| 4517 | /* |
| 4518 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4519 | * since cgroup_init_subsys will have already taken care of it. |
| 4520 | */ |
| 4521 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4522 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4523 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4524 | return 0; |
| 4525 | } |
| 4526 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4527 | /* init base cftset */ |
| 4528 | cgroup_init_cftsets(ss); |
| 4529 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4530 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4531 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4532 | |
| 4533 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4534 | * no ss->css_alloc seems to need anything important in the ss |
| 4535 | * struct, so this can happen first (i.e. before the rootnode |
| 4536 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4537 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4538 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4539 | if (IS_ERR(css)) { |
| 4540 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4541 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4542 | mutex_unlock(&cgroup_mutex); |
| 4543 | return PTR_ERR(css); |
| 4544 | } |
| 4545 | |
| 4546 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4547 | ss->root = &rootnode; |
| 4548 | |
| 4549 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4550 | init_cgroup_css(css, ss, dummytop); |
| 4551 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4552 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4553 | ret = cgroup_init_idr(ss, css); |
| 4554 | if (ret) |
| 4555 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* |
| 4559 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4560 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4561 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4562 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4563 | * table state, so each changed css_set will need its hash recomputed. |
| 4564 | * this is all done under the css_set_lock. |
| 4565 | */ |
| 4566 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4567 | hash_for_each_safe(css_set_table, i, tmp, cg, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4568 | /* skip entries that we already rehashed */ |
| 4569 | if (cg->subsys[ss->subsys_id]) |
| 4570 | continue; |
| 4571 | /* remove existing entry */ |
| 4572 | hash_del(&cg->hlist); |
| 4573 | /* set new value */ |
| 4574 | cg->subsys[ss->subsys_id] = css; |
| 4575 | /* recompute hash and restore entry */ |
| 4576 | key = css_set_hash(cg->subsys); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4577 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4578 | } |
| 4579 | write_unlock(&css_set_lock); |
| 4580 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4581 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4582 | ret = online_css(ss, dummytop); |
| 4583 | if (ret) |
| 4584 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4585 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4586 | /* success! */ |
| 4587 | mutex_unlock(&cgroup_mutex); |
| 4588 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4589 | |
| 4590 | err_unload: |
| 4591 | mutex_unlock(&cgroup_mutex); |
| 4592 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4593 | cgroup_unload_subsys(ss); |
| 4594 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4595 | } |
| 4596 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4597 | |
| 4598 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4599 | * cgroup_unload_subsys: unload a modular subsystem |
| 4600 | * @ss: the subsystem to unload |
| 4601 | * |
| 4602 | * This function should be called in a modular subsystem's exitcall. When this |
| 4603 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4604 | * the subsystem will not be attached to any hierarchy. |
| 4605 | */ |
| 4606 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4607 | { |
| 4608 | struct cg_cgroup_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4609 | |
| 4610 | BUG_ON(ss->module == NULL); |
| 4611 | |
| 4612 | /* |
| 4613 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4614 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4615 | * doesn't start being used while we're killing it off. |
| 4616 | */ |
| 4617 | BUG_ON(ss->root != &rootnode); |
| 4618 | |
| 4619 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4620 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4621 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4622 | ss->active = 0; |
| 4623 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4624 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4625 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4626 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4627 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4628 | subsys[ss->subsys_id] = NULL; |
| 4629 | |
| 4630 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4631 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4632 | |
| 4633 | /* |
| 4634 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4635 | * in loading, we need to pay our respects to the hashtable gods. |
| 4636 | */ |
| 4637 | write_lock(&css_set_lock); |
| 4638 | list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { |
| 4639 | struct css_set *cg = link->cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4640 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4641 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4642 | hash_del(&cg->hlist); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4643 | cg->subsys[ss->subsys_id] = NULL; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4644 | key = css_set_hash(cg->subsys); |
| 4645 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4646 | } |
| 4647 | write_unlock(&css_set_lock); |
| 4648 | |
| 4649 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4650 | * remove subsystem's css from the dummytop and free it - need to |
| 4651 | * free before marking as null because ss->css_free needs the |
| 4652 | * cgrp->subsys pointer to find their state. note that this also |
| 4653 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4654 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4655 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4656 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4657 | |
| 4658 | mutex_unlock(&cgroup_mutex); |
| 4659 | } |
| 4660 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4661 | |
| 4662 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4663 | * cgroup_init_early - cgroup initialization at system boot |
| 4664 | * |
| 4665 | * Initialize cgroups at system boot, and initialize any |
| 4666 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4667 | */ |
| 4668 | int __init cgroup_init_early(void) |
| 4669 | { |
| 4670 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4671 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4672 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 4673 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4674 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4675 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4676 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4677 | root_count = 1; |
| 4678 | init_task.cgroups = &init_css_set; |
| 4679 | |
| 4680 | init_css_set_link.cg = &init_css_set; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4681 | init_css_set_link.cgrp = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4682 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4683 | &rootnode.top_cgroup.css_sets); |
| 4684 | list_add(&init_css_set_link.cg_link_list, |
| 4685 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4686 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4687 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4688 | struct cgroup_subsys *ss = subsys[i]; |
| 4689 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4690 | /* at bootup time, we don't worry about modular subsystems */ |
| 4691 | if (!ss || ss->module) |
| 4692 | continue; |
| 4693 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4694 | BUG_ON(!ss->name); |
| 4695 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4696 | BUG_ON(!ss->css_alloc); |
| 4697 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4698 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4699 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4700 | ss->name, ss->subsys_id); |
| 4701 | BUG(); |
| 4702 | } |
| 4703 | |
| 4704 | if (ss->early_init) |
| 4705 | cgroup_init_subsys(ss); |
| 4706 | } |
| 4707 | return 0; |
| 4708 | } |
| 4709 | |
| 4710 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4711 | * cgroup_init - cgroup initialization |
| 4712 | * |
| 4713 | * Register cgroup filesystem and /proc file, and initialize |
| 4714 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4715 | */ |
| 4716 | int __init cgroup_init(void) |
| 4717 | { |
| 4718 | int err; |
| 4719 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4720 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4721 | |
| 4722 | err = bdi_init(&cgroup_backing_dev_info); |
| 4723 | if (err) |
| 4724 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4725 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4726 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4727 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4728 | |
| 4729 | /* at bootup time, we don't worry about modular subsystems */ |
| 4730 | if (!ss || ss->module) |
| 4731 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4732 | if (!ss->early_init) |
| 4733 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4734 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4735 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4738 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4739 | key = css_set_hash(init_css_set.subsys); |
| 4740 | hash_add(css_set_table, &init_css_set.hlist, key); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4741 | BUG_ON(!init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4742 | |
| 4743 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4744 | if (!cgroup_kobj) { |
| 4745 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4746 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | err = register_filesystem(&cgroup_fs_type); |
| 4750 | if (err < 0) { |
| 4751 | kobject_put(cgroup_kobj); |
| 4752 | goto out; |
| 4753 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4754 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4755 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4756 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4757 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4758 | if (err) |
| 4759 | bdi_destroy(&cgroup_backing_dev_info); |
| 4760 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4761 | return err; |
| 4762 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4763 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4764 | /* |
| 4765 | * proc_cgroup_show() |
| 4766 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4767 | * - Used for /proc/<pid>/cgroup. |
| 4768 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4769 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4770 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4771 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4772 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4773 | * cgroup to top_cgroup. |
| 4774 | */ |
| 4775 | |
| 4776 | /* TODO: Use a proper seq_file iterator */ |
| 4777 | static int proc_cgroup_show(struct seq_file *m, void *v) |
| 4778 | { |
| 4779 | struct pid *pid; |
| 4780 | struct task_struct *tsk; |
| 4781 | char *buf; |
| 4782 | int retval; |
| 4783 | struct cgroupfs_root *root; |
| 4784 | |
| 4785 | retval = -ENOMEM; |
| 4786 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4787 | if (!buf) |
| 4788 | goto out; |
| 4789 | |
| 4790 | retval = -ESRCH; |
| 4791 | pid = m->private; |
| 4792 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4793 | if (!tsk) |
| 4794 | goto out_free; |
| 4795 | |
| 4796 | retval = 0; |
| 4797 | |
| 4798 | mutex_lock(&cgroup_mutex); |
| 4799 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4800 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4801 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4802 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4803 | int count = 0; |
| 4804 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4805 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4806 | for_each_subsys(root, ss) |
| 4807 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4808 | if (strlen(root->name)) |
| 4809 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4810 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4811 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4812 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4813 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4814 | if (retval < 0) |
| 4815 | goto out_unlock; |
| 4816 | seq_puts(m, buf); |
| 4817 | seq_putc(m, '\n'); |
| 4818 | } |
| 4819 | |
| 4820 | out_unlock: |
| 4821 | mutex_unlock(&cgroup_mutex); |
| 4822 | put_task_struct(tsk); |
| 4823 | out_free: |
| 4824 | kfree(buf); |
| 4825 | out: |
| 4826 | return retval; |
| 4827 | } |
| 4828 | |
| 4829 | static int cgroup_open(struct inode *inode, struct file *file) |
| 4830 | { |
| 4831 | struct pid *pid = PROC_I(inode)->pid; |
| 4832 | return single_open(file, proc_cgroup_show, pid); |
| 4833 | } |
| 4834 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4835 | const struct file_operations proc_cgroup_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4836 | .open = cgroup_open, |
| 4837 | .read = seq_read, |
| 4838 | .llseek = seq_lseek, |
| 4839 | .release = single_release, |
| 4840 | }; |
| 4841 | |
| 4842 | /* Display information about each subsystem and each hierarchy */ |
| 4843 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4844 | { |
| 4845 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4846 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4847 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4848 | /* |
| 4849 | * ideally we don't want subsystems moving around while we do this. |
| 4850 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4851 | * subsys/hierarchy state. |
| 4852 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4853 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4854 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4855 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4856 | if (ss == NULL) |
| 4857 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4858 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4859 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4860 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4861 | } |
| 4862 | mutex_unlock(&cgroup_mutex); |
| 4863 | return 0; |
| 4864 | } |
| 4865 | |
| 4866 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4867 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4868 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4869 | } |
| 4870 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4871 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4872 | .open = cgroupstats_open, |
| 4873 | .read = seq_read, |
| 4874 | .llseek = seq_lseek, |
| 4875 | .release = single_release, |
| 4876 | }; |
| 4877 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4878 | /** |
| 4879 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4880 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4881 | * |
| 4882 | * Description: A task inherits its parent's cgroup at fork(). |
| 4883 | * |
| 4884 | * A pointer to the shared css_set was automatically copied in |
| 4885 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4886 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4887 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4888 | * have already changed current->cgroups, allowing the previously |
| 4889 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4890 | * |
| 4891 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4892 | * task, and the passed argument 'child' points to the child task. |
| 4893 | */ |
| 4894 | void cgroup_fork(struct task_struct *child) |
| 4895 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4896 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4897 | child->cgroups = current->cgroups; |
| 4898 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4899 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4900 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4904 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4905 | * @child: the task in question |
| 4906 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4907 | * Adds the task to the list running through its css_set if necessary and |
| 4908 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4909 | * visible on the task list in case we race with the first call to |
| 4910 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 4911 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4912 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4913 | void cgroup_post_fork(struct task_struct *child) |
| 4914 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4915 | int i; |
| 4916 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4917 | /* |
| 4918 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4919 | * under the tasklist_lock and we read it here after we added the child |
| 4920 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4921 | * yet in the tasklist when we walked through it from |
| 4922 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4923 | * should be visible now due to the paired locking and barriers implied |
| 4924 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4925 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4926 | * lock on fork. |
| 4927 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4928 | if (use_task_css_set_links) { |
| 4929 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4930 | task_lock(child); |
| 4931 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4932 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4933 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4934 | write_unlock(&css_set_lock); |
| 4935 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4936 | |
| 4937 | /* |
| 4938 | * Call ss->fork(). This must happen after @child is linked on |
| 4939 | * css_set; otherwise, @child might change state between ->fork() |
| 4940 | * and addition to css_set. |
| 4941 | */ |
| 4942 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame^] | 4943 | /* |
| 4944 | * fork/exit callbacks are supported only for builtin |
| 4945 | * subsystems, and the builtin section of the subsys |
| 4946 | * array is immutable, so we don't need to lock the |
| 4947 | * subsys array here. On the other hand, modular section |
| 4948 | * of the array can be freed at module unload, so we |
| 4949 | * can't touch that. |
| 4950 | */ |
| 4951 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4952 | struct cgroup_subsys *ss = subsys[i]; |
| 4953 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4954 | if (ss->fork) |
| 4955 | ss->fork(child); |
| 4956 | } |
| 4957 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4958 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4959 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4960 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4961 | * cgroup_exit - detach cgroup from exiting task |
| 4962 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4963 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4964 | * |
| 4965 | * Description: Detach cgroup from @tsk and release it. |
| 4966 | * |
| 4967 | * Note that cgroups marked notify_on_release force every task in |
| 4968 | * them to take the global cgroup_mutex mutex when exiting. |
| 4969 | * This could impact scaling on very large systems. Be reluctant to |
| 4970 | * use notify_on_release cgroups where very high task exit scaling |
| 4971 | * is required on large systems. |
| 4972 | * |
| 4973 | * the_top_cgroup_hack: |
| 4974 | * |
| 4975 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4976 | * |
| 4977 | * We call cgroup_exit() while the task is still competent to |
| 4978 | * handle notify_on_release(), then leave the task attached to the |
| 4979 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4980 | * |
| 4981 | * To do this properly, we would increment the reference count on |
| 4982 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4983 | * code we would add a second cgroup function call, to drop that |
| 4984 | * reference. This would just create an unnecessary hot spot on |
| 4985 | * the top_cgroup reference count, to no avail. |
| 4986 | * |
| 4987 | * Normally, holding a reference to a cgroup without bumping its |
| 4988 | * count is unsafe. The cgroup could go away, or someone could |
| 4989 | * attach us to a different cgroup, decrementing the count on |
| 4990 | * the first cgroup that we never incremented. But in this case, |
| 4991 | * 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] | 4992 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4993 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4994 | */ |
| 4995 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4996 | { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4997 | struct css_set *cg; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4998 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4999 | |
| 5000 | /* |
| 5001 | * Unlink from the css_set task list if necessary. |
| 5002 | * Optimistically check cg_list before taking |
| 5003 | * css_set_lock |
| 5004 | */ |
| 5005 | if (!list_empty(&tsk->cg_list)) { |
| 5006 | write_lock(&css_set_lock); |
| 5007 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5008 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5009 | write_unlock(&css_set_lock); |
| 5010 | } |
| 5011 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5012 | /* Reassign the task to the init_css_set. */ |
| 5013 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5014 | cg = tsk->cgroups; |
| 5015 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5016 | |
| 5017 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame^] | 5018 | /* |
| 5019 | * fork/exit callbacks are supported only for builtin |
| 5020 | * subsystems, see cgroup_post_fork() for details. |
| 5021 | */ |
| 5022 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5023 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5024 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5025 | if (ss->exit) { |
| 5026 | struct cgroup *old_cgrp = |
| 5027 | rcu_dereference_raw(cg->subsys[i])->cgroup; |
| 5028 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5029 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5030 | } |
| 5031 | } |
| 5032 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5033 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5034 | |
Li Zefan | b5d646f | 2013-01-24 14:43:51 +0800 | [diff] [blame] | 5035 | put_css_set_taskexit(cg); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5036 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5037 | |
| 5038 | /** |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 5039 | * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5040 | * @cgrp: the cgroup in question |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 5041 | * @task: the task in question |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5042 | * |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 5043 | * See if @cgrp is a descendant of @task's cgroup in the appropriate |
| 5044 | * hierarchy. |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5045 | * |
| 5046 | * If we are sending in dummytop, then presumably we are creating |
| 5047 | * the top cgroup in the subsystem. |
| 5048 | * |
| 5049 | * Called only by the ns (nsproxy) cgroup. |
| 5050 | */ |
Grzegorz Nosek | 313e924 | 2009-04-02 16:57:23 -0700 | [diff] [blame] | 5051 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5052 | { |
| 5053 | int ret; |
| 5054 | struct cgroup *target; |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5055 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5056 | if (cgrp == dummytop) |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5057 | return 1; |
| 5058 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5059 | target = task_cgroup_from_root(task, cgrp->root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5060 | while (cgrp != target && cgrp!= cgrp->top_cgroup) |
| 5061 | cgrp = cgrp->parent; |
| 5062 | ret = (cgrp == target); |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5063 | return ret; |
| 5064 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5065 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5066 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5067 | { |
| 5068 | /* All of these checks rely on RCU to keep the cgroup |
| 5069 | * structure alive */ |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5070 | if (cgroup_is_releasable(cgrp) && |
| 5071 | !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) { |
| 5072 | /* |
| 5073 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5074 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5075 | * it now |
| 5076 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5077 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5078 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5079 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5080 | if (!cgroup_is_removed(cgrp) && |
| 5081 | list_empty(&cgrp->release_list)) { |
| 5082 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5083 | need_schedule_work = 1; |
| 5084 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5085 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5086 | if (need_schedule_work) |
| 5087 | schedule_work(&release_agent_work); |
| 5088 | } |
| 5089 | } |
| 5090 | |
Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 5091 | /* Caller must verify that the css is not for root cgroup */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5092 | bool __css_tryget(struct cgroup_subsys_state *css) |
| 5093 | { |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5094 | while (true) { |
| 5095 | int t, v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5096 | |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5097 | v = css_refcnt(css); |
| 5098 | t = atomic_cmpxchg(&css->refcnt, v, v + 1); |
| 5099 | if (likely(t == v)) |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5100 | return true; |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5101 | else if (t < 0) |
| 5102 | return false; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5103 | cpu_relax(); |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5104 | } |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5105 | } |
| 5106 | EXPORT_SYMBOL_GPL(__css_tryget); |
| 5107 | |
| 5108 | /* Caller must verify that the css is not for root cgroup */ |
| 5109 | void __css_put(struct cgroup_subsys_state *css) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5110 | { |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5111 | int v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5112 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5113 | v = css_unbias_refcnt(atomic_dec_return(&css->refcnt)); |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5114 | if (v == 0) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5115 | schedule_work(&css->dput_work); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5116 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5117 | EXPORT_SYMBOL_GPL(__css_put); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5118 | |
| 5119 | /* |
| 5120 | * Notify userspace when a cgroup is released, by running the |
| 5121 | * configured release agent with the name of the cgroup (path |
| 5122 | * relative to the root of cgroup file system) as the argument. |
| 5123 | * |
| 5124 | * Most likely, this user command will try to rmdir this cgroup. |
| 5125 | * |
| 5126 | * This races with the possibility that some other task will be |
| 5127 | * attached to this cgroup before it is removed, or that some other |
| 5128 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5129 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5130 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5131 | * to continue to serve a useful existence. Next time it's released, |
| 5132 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5133 | * |
| 5134 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5135 | * means only wait until the task is successfully execve()'d. The |
| 5136 | * separate release agent task is forked by call_usermodehelper(), |
| 5137 | * then control in this thread returns here, without waiting for the |
| 5138 | * release agent task. We don't bother to wait because the caller of |
| 5139 | * this routine has no use for the exit status of the release agent |
| 5140 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5141 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5142 | static void cgroup_release_agent(struct work_struct *work) |
| 5143 | { |
| 5144 | BUG_ON(work != &release_agent_work); |
| 5145 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5146 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5147 | while (!list_empty(&release_list)) { |
| 5148 | char *argv[3], *envp[3]; |
| 5149 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5150 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5151 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5152 | struct cgroup, |
| 5153 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5154 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5155 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5156 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5157 | if (!pathbuf) |
| 5158 | goto continue_free; |
| 5159 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5160 | goto continue_free; |
| 5161 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5162 | if (!agentbuf) |
| 5163 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5164 | |
| 5165 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5166 | argv[i++] = agentbuf; |
| 5167 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5168 | argv[i] = NULL; |
| 5169 | |
| 5170 | i = 0; |
| 5171 | /* minimal command environment */ |
| 5172 | envp[i++] = "HOME=/"; |
| 5173 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5174 | envp[i] = NULL; |
| 5175 | |
| 5176 | /* Drop the lock while we invoke the usermode helper, |
| 5177 | * since the exec could involve hitting disk and hence |
| 5178 | * be a slow process */ |
| 5179 | mutex_unlock(&cgroup_mutex); |
| 5180 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5181 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5182 | continue_free: |
| 5183 | kfree(pathbuf); |
| 5184 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5185 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5186 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5187 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5188 | mutex_unlock(&cgroup_mutex); |
| 5189 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5190 | |
| 5191 | static int __init cgroup_disable(char *str) |
| 5192 | { |
| 5193 | int i; |
| 5194 | char *token; |
| 5195 | |
| 5196 | while ((token = strsep(&str, ",")) != NULL) { |
| 5197 | if (!*token) |
| 5198 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5199 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5200 | struct cgroup_subsys *ss = subsys[i]; |
| 5201 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5202 | /* |
| 5203 | * cgroup_disable, being at boot time, can't |
| 5204 | * know about module subsystems, so we don't |
| 5205 | * worry about them. |
| 5206 | */ |
| 5207 | if (!ss || ss->module) |
| 5208 | continue; |
| 5209 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5210 | if (!strcmp(token, ss->name)) { |
| 5211 | ss->disabled = 1; |
| 5212 | printk(KERN_INFO "Disabling %s control group" |
| 5213 | " subsystem\n", ss->name); |
| 5214 | break; |
| 5215 | } |
| 5216 | } |
| 5217 | } |
| 5218 | return 1; |
| 5219 | } |
| 5220 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5221 | |
| 5222 | /* |
| 5223 | * Functons for CSS ID. |
| 5224 | */ |
| 5225 | |
| 5226 | /* |
| 5227 | *To get ID other than 0, this should be called when !cgroup_is_removed(). |
| 5228 | */ |
| 5229 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5230 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5231 | struct css_id *cssid; |
| 5232 | |
| 5233 | /* |
| 5234 | * This css_id() can return correct value when somone has refcnt |
| 5235 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5236 | * it's unchanged until freed. |
| 5237 | */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5238 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5239 | |
| 5240 | if (cssid) |
| 5241 | return cssid->id; |
| 5242 | return 0; |
| 5243 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5244 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5245 | |
| 5246 | unsigned short css_depth(struct cgroup_subsys_state *css) |
| 5247 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5248 | struct css_id *cssid; |
| 5249 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5250 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5251 | |
| 5252 | if (cssid) |
| 5253 | return cssid->depth; |
| 5254 | return 0; |
| 5255 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5256 | EXPORT_SYMBOL_GPL(css_depth); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5257 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5258 | /** |
| 5259 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5260 | * @child: the css to be tested. |
| 5261 | * @root: the css supporsed to be an ancestor of the child. |
| 5262 | * |
| 5263 | * 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] | 5264 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5265 | * But, considering usual usage, the csses should be valid objects after test. |
| 5266 | * Assuming that the caller will do some action to the child if this returns |
| 5267 | * returns true, the caller must take "child";s reference count. |
| 5268 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5269 | */ |
| 5270 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5271 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5272 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5273 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5274 | struct css_id *child_id; |
| 5275 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5276 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5277 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5278 | if (!child_id) |
| 5279 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5280 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5281 | if (!root_id) |
| 5282 | return false; |
| 5283 | if (child_id->depth < root_id->depth) |
| 5284 | return false; |
| 5285 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5286 | return false; |
| 5287 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5290 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5291 | { |
| 5292 | struct css_id *id = css->id; |
| 5293 | /* When this is called before css_id initialization, id can be NULL */ |
| 5294 | if (!id) |
| 5295 | return; |
| 5296 | |
| 5297 | BUG_ON(!ss->use_id); |
| 5298 | |
| 5299 | rcu_assign_pointer(id->css, NULL); |
| 5300 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5301 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5302 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5303 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5304 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5305 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5306 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5307 | |
| 5308 | /* |
| 5309 | * This is called by init or create(). Then, calls to this function are |
| 5310 | * always serialized (By cgroup_mutex() at create()). |
| 5311 | */ |
| 5312 | |
| 5313 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5314 | { |
| 5315 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5316 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5317 | |
| 5318 | BUG_ON(!ss->use_id); |
| 5319 | |
| 5320 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5321 | newid = kzalloc(size, GFP_KERNEL); |
| 5322 | if (!newid) |
| 5323 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5324 | |
| 5325 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5326 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5327 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5328 | 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] | 5329 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5330 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5331 | |
| 5332 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5333 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5334 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5335 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5336 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5337 | newid->depth = depth; |
| 5338 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5339 | err_out: |
| 5340 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5341 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5342 | |
| 5343 | } |
| 5344 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5345 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5346 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5347 | { |
| 5348 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5349 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5350 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5351 | idr_init(&ss->idr); |
| 5352 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5353 | newid = get_new_cssid(ss, 0); |
| 5354 | if (IS_ERR(newid)) |
| 5355 | return PTR_ERR(newid); |
| 5356 | |
| 5357 | newid->stack[0] = newid->id; |
| 5358 | newid->css = rootcss; |
| 5359 | rootcss->id = newid; |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
| 5363 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5364 | struct cgroup *child) |
| 5365 | { |
| 5366 | int subsys_id, i, depth = 0; |
| 5367 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5368 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5369 | |
| 5370 | subsys_id = ss->subsys_id; |
| 5371 | parent_css = parent->subsys[subsys_id]; |
| 5372 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5373 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5374 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5375 | |
| 5376 | child_id = get_new_cssid(ss, depth); |
| 5377 | if (IS_ERR(child_id)) |
| 5378 | return PTR_ERR(child_id); |
| 5379 | |
| 5380 | for (i = 0; i < depth; i++) |
| 5381 | child_id->stack[i] = parent_id->stack[i]; |
| 5382 | child_id->stack[depth] = child_id->id; |
| 5383 | /* |
| 5384 | * child_id->css pointer will be set after this cgroup is available |
| 5385 | * see cgroup_populate_dir() |
| 5386 | */ |
| 5387 | rcu_assign_pointer(child_css->id, child_id); |
| 5388 | |
| 5389 | return 0; |
| 5390 | } |
| 5391 | |
| 5392 | /** |
| 5393 | * css_lookup - lookup css by id |
| 5394 | * @ss: cgroup subsys to be looked into. |
| 5395 | * @id: the id |
| 5396 | * |
| 5397 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5398 | * NULL if not. Should be called under rcu_read_lock() |
| 5399 | */ |
| 5400 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5401 | { |
| 5402 | struct css_id *cssid = NULL; |
| 5403 | |
| 5404 | BUG_ON(!ss->use_id); |
| 5405 | cssid = idr_find(&ss->idr, id); |
| 5406 | |
| 5407 | if (unlikely(!cssid)) |
| 5408 | return NULL; |
| 5409 | |
| 5410 | return rcu_dereference(cssid->css); |
| 5411 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5412 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5413 | |
| 5414 | /** |
| 5415 | * css_get_next - lookup next cgroup under specified hierarchy. |
| 5416 | * @ss: pointer to subsystem |
| 5417 | * @id: current position of iteration. |
| 5418 | * @root: pointer to css. search tree under this. |
| 5419 | * @foundid: position of found object. |
| 5420 | * |
| 5421 | * Search next css under the specified hierarchy of rootid. Calling under |
| 5422 | * rcu_read_lock() is necessary. Returns NULL if it reaches the end. |
| 5423 | */ |
| 5424 | struct cgroup_subsys_state * |
| 5425 | css_get_next(struct cgroup_subsys *ss, int id, |
| 5426 | struct cgroup_subsys_state *root, int *foundid) |
| 5427 | { |
| 5428 | struct cgroup_subsys_state *ret = NULL; |
| 5429 | struct css_id *tmp; |
| 5430 | int tmpid; |
| 5431 | int rootid = css_id(root); |
| 5432 | int depth = css_depth(root); |
| 5433 | |
| 5434 | if (!rootid) |
| 5435 | return NULL; |
| 5436 | |
| 5437 | BUG_ON(!ss->use_id); |
Hugh Dickins | ca464d6 | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5438 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 5439 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5440 | /* fill start point for scan */ |
| 5441 | tmpid = id; |
| 5442 | while (1) { |
| 5443 | /* |
| 5444 | * scan next entry from bitmap(tree), tmpid is updated after |
| 5445 | * idr_get_next(). |
| 5446 | */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5447 | tmp = idr_get_next(&ss->idr, &tmpid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5448 | if (!tmp) |
| 5449 | break; |
| 5450 | if (tmp->depth >= depth && tmp->stack[depth] == rootid) { |
| 5451 | ret = rcu_dereference(tmp->css); |
| 5452 | if (ret) { |
| 5453 | *foundid = tmpid; |
| 5454 | break; |
| 5455 | } |
| 5456 | } |
| 5457 | /* continue to scan from next id */ |
| 5458 | tmpid = tmpid + 1; |
| 5459 | } |
| 5460 | return ret; |
| 5461 | } |
| 5462 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5463 | /* |
| 5464 | * get corresponding css from file open on cgroupfs directory |
| 5465 | */ |
| 5466 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5467 | { |
| 5468 | struct cgroup *cgrp; |
| 5469 | struct inode *inode; |
| 5470 | struct cgroup_subsys_state *css; |
| 5471 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5472 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5473 | /* check in cgroup filesystem dir */ |
| 5474 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5475 | return ERR_PTR(-EBADF); |
| 5476 | |
| 5477 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5478 | return ERR_PTR(-EINVAL); |
| 5479 | |
| 5480 | /* get cgroup */ |
| 5481 | cgrp = __d_cgrp(f->f_dentry); |
| 5482 | css = cgrp->subsys[id]; |
| 5483 | return css ? css : ERR_PTR(-ENOENT); |
| 5484 | } |
| 5485 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5486 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5487 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5488 | { |
| 5489 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5490 | |
| 5491 | if (!css) |
| 5492 | return ERR_PTR(-ENOMEM); |
| 5493 | |
| 5494 | return css; |
| 5495 | } |
| 5496 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5497 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5498 | { |
| 5499 | kfree(cont->subsys[debug_subsys_id]); |
| 5500 | } |
| 5501 | |
| 5502 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) |
| 5503 | { |
| 5504 | return atomic_read(&cont->count); |
| 5505 | } |
| 5506 | |
| 5507 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5508 | { |
| 5509 | return cgroup_task_count(cont); |
| 5510 | } |
| 5511 | |
| 5512 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5513 | { |
| 5514 | return (u64)(unsigned long)current->cgroups; |
| 5515 | } |
| 5516 | |
| 5517 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5518 | struct cftype *cft) |
| 5519 | { |
| 5520 | u64 count; |
| 5521 | |
| 5522 | rcu_read_lock(); |
| 5523 | count = atomic_read(¤t->cgroups->refcount); |
| 5524 | rcu_read_unlock(); |
| 5525 | return count; |
| 5526 | } |
| 5527 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5528 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5529 | struct cftype *cft, |
| 5530 | struct seq_file *seq) |
| 5531 | { |
| 5532 | struct cg_cgroup_link *link; |
| 5533 | struct css_set *cg; |
| 5534 | |
| 5535 | read_lock(&css_set_lock); |
| 5536 | rcu_read_lock(); |
| 5537 | cg = rcu_dereference(current->cgroups); |
| 5538 | list_for_each_entry(link, &cg->cg_links, cg_link_list) { |
| 5539 | struct cgroup *c = link->cgrp; |
| 5540 | const char *name; |
| 5541 | |
| 5542 | if (c->dentry) |
| 5543 | name = c->dentry->d_name.name; |
| 5544 | else |
| 5545 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5546 | seq_printf(seq, "Root %d group %s\n", |
| 5547 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5548 | } |
| 5549 | rcu_read_unlock(); |
| 5550 | read_unlock(&css_set_lock); |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
| 5554 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5555 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5556 | struct cftype *cft, |
| 5557 | struct seq_file *seq) |
| 5558 | { |
| 5559 | struct cg_cgroup_link *link; |
| 5560 | |
| 5561 | read_lock(&css_set_lock); |
| 5562 | list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { |
| 5563 | struct css_set *cg = link->cg; |
| 5564 | struct task_struct *task; |
| 5565 | int count = 0; |
| 5566 | seq_printf(seq, "css_set %p\n", cg); |
| 5567 | list_for_each_entry(task, &cg->tasks, cg_list) { |
| 5568 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5569 | seq_puts(seq, " ...\n"); |
| 5570 | break; |
| 5571 | } else { |
| 5572 | seq_printf(seq, " task %d\n", |
| 5573 | task_pid_vnr(task)); |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | read_unlock(&css_set_lock); |
| 5578 | return 0; |
| 5579 | } |
| 5580 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5581 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5582 | { |
| 5583 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5584 | } |
| 5585 | |
| 5586 | static struct cftype debug_files[] = { |
| 5587 | { |
| 5588 | .name = "cgroup_refcount", |
| 5589 | .read_u64 = cgroup_refcount_read, |
| 5590 | }, |
| 5591 | { |
| 5592 | .name = "taskcount", |
| 5593 | .read_u64 = debug_taskcount_read, |
| 5594 | }, |
| 5595 | |
| 5596 | { |
| 5597 | .name = "current_css_set", |
| 5598 | .read_u64 = current_css_set_read, |
| 5599 | }, |
| 5600 | |
| 5601 | { |
| 5602 | .name = "current_css_set_refcount", |
| 5603 | .read_u64 = current_css_set_refcount_read, |
| 5604 | }, |
| 5605 | |
| 5606 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5607 | .name = "current_css_set_cg_links", |
| 5608 | .read_seq_string = current_css_set_cg_links_read, |
| 5609 | }, |
| 5610 | |
| 5611 | { |
| 5612 | .name = "cgroup_css_links", |
| 5613 | .read_seq_string = cgroup_css_links_read, |
| 5614 | }, |
| 5615 | |
| 5616 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5617 | .name = "releasable", |
| 5618 | .read_u64 = releasable_read, |
| 5619 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5620 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5621 | { } /* terminate */ |
| 5622 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5623 | |
| 5624 | struct cgroup_subsys debug_subsys = { |
| 5625 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5626 | .css_alloc = debug_css_alloc, |
| 5627 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5628 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5629 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5630 | }; |
| 5631 | #endif /* CONFIG_CGROUP_DEBUG */ |