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> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 62 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
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 | a1a71b4 | 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 | a1a71b4 | 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 | e788e066 | 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 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 332 | /** |
| 333 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 334 | * @cgrp: the cgroup to be checked for liveness |
| 335 | * |
| 336 | * On success, returns true; the lock should be later released with |
| 337 | * cgroup_unlock(). On failure returns false with no lock held. |
| 338 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame^] | 339 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 340 | { |
| 341 | mutex_lock(&cgroup_mutex); |
| 342 | if (cgroup_is_removed(cgrp)) { |
| 343 | mutex_unlock(&cgroup_mutex); |
| 344 | return false; |
| 345 | } |
| 346 | return true; |
| 347 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 348 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 349 | /* the list of cgroups eligible for automatic release. Protected by |
| 350 | * release_list_lock */ |
| 351 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 352 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 353 | static void cgroup_release_agent(struct work_struct *work); |
| 354 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 355 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 356 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 357 | /* Link structure for associating css_set objects with cgroups */ |
| 358 | struct cg_cgroup_link { |
| 359 | /* |
| 360 | * List running through cg_cgroup_links associated with a |
| 361 | * cgroup, anchored on cgroup->css_sets |
| 362 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 363 | struct list_head cgrp_link_list; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 364 | struct cgroup *cgrp; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 365 | /* |
| 366 | * List running through cg_cgroup_links pointing at a |
| 367 | * single css_set object, anchored on css_set->cg_links |
| 368 | */ |
| 369 | struct list_head cg_link_list; |
| 370 | struct css_set *cg; |
| 371 | }; |
| 372 | |
| 373 | /* The default css_set - used by init and its children prior to any |
| 374 | * hierarchies being mounted. It contains a pointer to the root state |
| 375 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 376 | * reference-counted, to improve performance when child cgroups |
| 377 | * haven't been created. |
| 378 | */ |
| 379 | |
| 380 | static struct css_set init_css_set; |
| 381 | static struct cg_cgroup_link init_css_set_link; |
| 382 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 383 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 384 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 385 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 386 | /* css_set_lock protects the list of css_set objects, and the |
| 387 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 388 | * due to cgroup_iter_start() */ |
| 389 | static DEFINE_RWLOCK(css_set_lock); |
| 390 | static int css_set_count; |
| 391 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 392 | /* |
| 393 | * hash table for cgroup groups. This improves the performance to find |
| 394 | * an existing css_set. This hash doesn't (currently) take into |
| 395 | * account cgroups in empty hierarchies. |
| 396 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 397 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 398 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 399 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 400 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 401 | { |
| 402 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 403 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 404 | |
| 405 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 406 | key += (unsigned long)css[i]; |
| 407 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 408 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 409 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 412 | /* We don't maintain the lists running through each css_set to its |
| 413 | * task until after the first call to cgroup_iter_start(). This |
| 414 | * reduces the fork()/exit() overhead for people who have cgroups |
| 415 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 416 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 417 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 418 | static void __put_css_set(struct css_set *cg, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 419 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 420 | struct cg_cgroup_link *link; |
| 421 | struct cg_cgroup_link *saved_link; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 422 | /* |
| 423 | * Ensure that the refcount doesn't hit zero while any readers |
| 424 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 425 | * rwlock |
| 426 | */ |
| 427 | if (atomic_add_unless(&cg->refcount, -1, 1)) |
| 428 | return; |
| 429 | write_lock(&css_set_lock); |
| 430 | if (!atomic_dec_and_test(&cg->refcount)) { |
| 431 | write_unlock(&css_set_lock); |
| 432 | return; |
| 433 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 434 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 435 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 436 | hash_del(&cg->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 437 | css_set_count--; |
| 438 | |
| 439 | list_for_each_entry_safe(link, saved_link, &cg->cg_links, |
| 440 | cg_link_list) { |
| 441 | struct cgroup *cgrp = link->cgrp; |
| 442 | list_del(&link->cg_link_list); |
| 443 | list_del(&link->cgrp_link_list); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * We may not be holding cgroup_mutex, and if cgrp->count is |
| 447 | * dropped to 0 the cgroup can be destroyed at any time, hence |
| 448 | * rcu_read_lock is used to keep it alive. |
| 449 | */ |
| 450 | rcu_read_lock(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 451 | if (atomic_dec_and_test(&cgrp->count) && |
| 452 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 453 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 454 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 455 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 456 | } |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 457 | rcu_read_unlock(); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 458 | |
| 459 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 460 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 461 | |
| 462 | write_unlock(&css_set_lock); |
Lai Jiangshan | 30088ad | 2011-03-15 17:53:46 +0800 | [diff] [blame] | 463 | kfree_rcu(cg, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
| 467 | * refcounted get/put for css_set objects |
| 468 | */ |
| 469 | static inline void get_css_set(struct css_set *cg) |
| 470 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 471 | atomic_inc(&cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static inline void put_css_set(struct css_set *cg) |
| 475 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 476 | __put_css_set(cg, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 479 | static inline void put_css_set_taskexit(struct css_set *cg) |
| 480 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 481 | __put_css_set(cg, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 484 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 485 | * compare_css_sets - helper function for find_existing_css_set(). |
| 486 | * @cg: candidate css_set being tested |
| 487 | * @old_cg: existing css_set for a task |
| 488 | * @new_cgrp: cgroup that's being entered by the task |
| 489 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 490 | * |
| 491 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 492 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 493 | */ |
| 494 | static bool compare_css_sets(struct css_set *cg, |
| 495 | struct css_set *old_cg, |
| 496 | struct cgroup *new_cgrp, |
| 497 | struct cgroup_subsys_state *template[]) |
| 498 | { |
| 499 | struct list_head *l1, *l2; |
| 500 | |
| 501 | if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { |
| 502 | /* Not all subsystems matched */ |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Compare cgroup pointers in order to distinguish between |
| 508 | * different cgroups in heirarchies with no subsystems. We |
| 509 | * could get by with just this check alone (and skip the |
| 510 | * memcmp above) but on most setups the memcmp check will |
| 511 | * avoid the need for this more expensive check on almost all |
| 512 | * candidates. |
| 513 | */ |
| 514 | |
| 515 | l1 = &cg->cg_links; |
| 516 | l2 = &old_cg->cg_links; |
| 517 | while (1) { |
| 518 | struct cg_cgroup_link *cgl1, *cgl2; |
| 519 | struct cgroup *cg1, *cg2; |
| 520 | |
| 521 | l1 = l1->next; |
| 522 | l2 = l2->next; |
| 523 | /* See if we reached the end - both lists are equal length. */ |
| 524 | if (l1 == &cg->cg_links) { |
| 525 | BUG_ON(l2 != &old_cg->cg_links); |
| 526 | break; |
| 527 | } else { |
| 528 | BUG_ON(l2 == &old_cg->cg_links); |
| 529 | } |
| 530 | /* Locate the cgroups associated with these links. */ |
| 531 | cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); |
| 532 | cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); |
| 533 | cg1 = cgl1->cgrp; |
| 534 | cg2 = cgl2->cgrp; |
| 535 | /* Hierarchies should be linked in the same order. */ |
| 536 | BUG_ON(cg1->root != cg2->root); |
| 537 | |
| 538 | /* |
| 539 | * If this hierarchy is the hierarchy of the cgroup |
| 540 | * that's changing, then we need to check that this |
| 541 | * css_set points to the new cgroup; if it's any other |
| 542 | * hierarchy, then this css_set should point to the |
| 543 | * same cgroup as the old css_set. |
| 544 | */ |
| 545 | if (cg1->root == new_cgrp->root) { |
| 546 | if (cg1 != new_cgrp) |
| 547 | return false; |
| 548 | } else { |
| 549 | if (cg1 != cg2) |
| 550 | return false; |
| 551 | } |
| 552 | } |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | * find_existing_css_set() is a helper for |
| 558 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 559 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 560 | * |
| 561 | * oldcg: the cgroup group that we're using before the cgroup |
| 562 | * transition |
| 563 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 564 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | * |
| 566 | * template: location in which to build the desired set of subsystem |
| 567 | * state objects for the new cgroup group |
| 568 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 569 | static struct css_set *find_existing_css_set( |
| 570 | struct css_set *oldcg, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 571 | struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 572 | struct cgroup_subsys_state *template[]) |
| 573 | { |
| 574 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 575 | struct cgroupfs_root *root = cgrp->root; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 576 | struct css_set *cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 577 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 578 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 579 | /* |
| 580 | * Build the set of subsystem state objects that we want to see in the |
| 581 | * new css_set. while subsystems can change globally, the entries here |
| 582 | * won't change, so no need for locking. |
| 583 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 584 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 585 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 586 | /* Subsystem is in this hierarchy. So we want |
| 587 | * the subsystem state from the new |
| 588 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 589 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 590 | } else { |
| 591 | /* Subsystem is not in this hierarchy, so we |
| 592 | * don't want to change the subsystem state */ |
| 593 | template[i] = oldcg->subsys[i]; |
| 594 | } |
| 595 | } |
| 596 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 597 | key = css_set_hash(template); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 598 | hash_for_each_possible(css_set_table, cg, hlist, key) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 599 | if (!compare_css_sets(cg, oldcg, cgrp, template)) |
| 600 | continue; |
| 601 | |
| 602 | /* This css_set matches what we need */ |
| 603 | return cg; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 604 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* No existing cgroup group matched */ |
| 607 | return NULL; |
| 608 | } |
| 609 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 610 | static void free_cg_links(struct list_head *tmp) |
| 611 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 612 | struct cg_cgroup_link *link; |
| 613 | struct cg_cgroup_link *saved_link; |
| 614 | |
| 615 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 616 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 617 | kfree(link); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 622 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 623 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 624 | * success or a negative error |
| 625 | */ |
| 626 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 627 | { |
| 628 | struct cg_cgroup_link *link; |
| 629 | int i; |
| 630 | INIT_LIST_HEAD(tmp); |
| 631 | for (i = 0; i < count; i++) { |
| 632 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 633 | if (!link) { |
| 634 | free_cg_links(tmp); |
| 635 | return -ENOMEM; |
| 636 | } |
| 637 | list_add(&link->cgrp_link_list, tmp); |
| 638 | } |
| 639 | return 0; |
| 640 | } |
| 641 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 642 | /** |
| 643 | * link_css_set - a helper function to link a css_set to a cgroup |
| 644 | * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() |
| 645 | * @cg: the css_set to be linked |
| 646 | * @cgrp: the destination cgroup |
| 647 | */ |
| 648 | static void link_css_set(struct list_head *tmp_cg_links, |
| 649 | struct css_set *cg, struct cgroup *cgrp) |
| 650 | { |
| 651 | struct cg_cgroup_link *link; |
| 652 | |
| 653 | BUG_ON(list_empty(tmp_cg_links)); |
| 654 | link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, |
| 655 | cgrp_link_list); |
| 656 | link->cg = cg; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 657 | link->cgrp = cgrp; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 658 | atomic_inc(&cgrp->count); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 659 | list_move(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 660 | /* |
| 661 | * Always add links to the tail of the list so that the list |
| 662 | * is sorted by order of hierarchy creation |
| 663 | */ |
| 664 | list_add_tail(&link->cg_link_list, &cg->cg_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 667 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 668 | * find_css_set() takes an existing cgroup group and a |
| 669 | * cgroup object, and returns a css_set object that's |
| 670 | * equivalent to the old group, but with the given cgroup |
| 671 | * substituted into the appropriate hierarchy. Must be called with |
| 672 | * cgroup_mutex held |
| 673 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 674 | static struct css_set *find_css_set( |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 675 | struct css_set *oldcg, struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | { |
| 677 | struct css_set *res; |
| 678 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 679 | |
| 680 | struct list_head tmp_cg_links; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 681 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 682 | struct cg_cgroup_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 683 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 684 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | /* First see if we already have a cgroup group that matches |
| 686 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 687 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 688 | res = find_existing_css_set(oldcg, cgrp, template); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 689 | if (res) |
| 690 | get_css_set(res); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 691 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 692 | |
| 693 | if (res) |
| 694 | return res; |
| 695 | |
| 696 | res = kmalloc(sizeof(*res), GFP_KERNEL); |
| 697 | if (!res) |
| 698 | return NULL; |
| 699 | |
| 700 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 701 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
| 702 | kfree(res); |
| 703 | return NULL; |
| 704 | } |
| 705 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 706 | atomic_set(&res->refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 707 | INIT_LIST_HEAD(&res->cg_links); |
| 708 | INIT_LIST_HEAD(&res->tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 709 | INIT_HLIST_NODE(&res->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 710 | |
| 711 | /* Copy the set of subsystem state objects generated in |
| 712 | * find_existing_css_set() */ |
| 713 | memcpy(res->subsys, template, sizeof(res->subsys)); |
| 714 | |
| 715 | write_lock(&css_set_lock); |
| 716 | /* Add reference counts and links from the new css_set. */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 717 | list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { |
| 718 | struct cgroup *c = link->cgrp; |
| 719 | if (c->root == cgrp->root) |
| 720 | c = cgrp; |
| 721 | link_css_set(&tmp_cg_links, res, c); |
| 722 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 723 | |
| 724 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 725 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 726 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 727 | |
| 728 | /* Add this cgroup group to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 729 | key = css_set_hash(res->subsys); |
| 730 | hash_add(css_set_table, &res->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 731 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 732 | write_unlock(&css_set_lock); |
| 733 | |
| 734 | return res; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 737 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 738 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 739 | * called with cgroup_mutex held. |
| 740 | */ |
| 741 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 742 | struct cgroupfs_root *root) |
| 743 | { |
| 744 | struct css_set *css; |
| 745 | struct cgroup *res = NULL; |
| 746 | |
| 747 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 748 | read_lock(&css_set_lock); |
| 749 | /* |
| 750 | * No need to lock the task - since we hold cgroup_mutex the |
| 751 | * task can't change groups, so the only thing that can happen |
| 752 | * is that it exits and its css is set back to init_css_set. |
| 753 | */ |
| 754 | css = task->cgroups; |
| 755 | if (css == &init_css_set) { |
| 756 | res = &root->top_cgroup; |
| 757 | } else { |
| 758 | struct cg_cgroup_link *link; |
| 759 | list_for_each_entry(link, &css->cg_links, cg_link_list) { |
| 760 | struct cgroup *c = link->cgrp; |
| 761 | if (c->root == root) { |
| 762 | res = c; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | read_unlock(&css_set_lock); |
| 768 | BUG_ON(!res); |
| 769 | return res; |
| 770 | } |
| 771 | |
| 772 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 773 | * There is one global cgroup mutex. We also require taking |
| 774 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 775 | * See "The task_lock() exception", at the end of this comment. |
| 776 | * |
| 777 | * A task must hold cgroup_mutex to modify cgroups. |
| 778 | * |
| 779 | * Any task can increment and decrement the count field without lock. |
| 780 | * So in general, code holding cgroup_mutex can't rely on the count |
| 781 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 782 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 783 | * means that no tasks are currently attached, therefore there is no |
| 784 | * way a task attached to that cgroup can fork (the other way to |
| 785 | * increment the count). So code holding cgroup_mutex can safely |
| 786 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 787 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 788 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 789 | * needs that mutex. |
| 790 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 791 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 792 | * (usually) take cgroup_mutex. These are the two most performance |
| 793 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 794 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 795 | * 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] | 796 | * to the release agent with the name of the cgroup (path relative to |
| 797 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 798 | * |
| 799 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 800 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 801 | * tasks in the system use _some_ cgroup, and since there is always at |
| 802 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 803 | * always has either children cgroups and/or using tasks. So we don't |
| 804 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 805 | * |
| 806 | * The task_lock() exception |
| 807 | * |
| 808 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 809 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 810 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 811 | * several performance critical places that need to reference |
| 812 | * task->cgroup without the expense of grabbing a system global |
| 813 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 814 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 815 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 816 | * the task_struct routinely used for such matters. |
| 817 | * |
| 818 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 819 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 820 | */ |
| 821 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 822 | /** |
| 823 | * cgroup_lock - lock out any changes to cgroup structures |
| 824 | * |
| 825 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame^] | 826 | static void cgroup_lock(void) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 827 | { |
| 828 | mutex_lock(&cgroup_mutex); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * cgroup_unlock - release lock on cgroup changes |
| 833 | * |
| 834 | * Undo the lock taken in a previous cgroup_lock() call. |
| 835 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame^] | 836 | static void cgroup_unlock(void) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 837 | { |
| 838 | mutex_unlock(&cgroup_mutex); |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * A couple of forward declarations required, due to cyclic reference loop: |
| 843 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 844 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 845 | * -> cgroup_mkdir. |
| 846 | */ |
| 847 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 848 | 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] | 849 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 850 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 851 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 852 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 853 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 854 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 855 | |
| 856 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 857 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 858 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 859 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 860 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 861 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 862 | struct cgroup *parent, struct cgroup *child); |
| 863 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 864 | 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] | 865 | { |
| 866 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 867 | |
| 868 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 869 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 870 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 871 | inode->i_uid = current_fsuid(); |
| 872 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 873 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 874 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 875 | } |
| 876 | return inode; |
| 877 | } |
| 878 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 879 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 880 | { |
| 881 | struct cgroup_name *name; |
| 882 | |
| 883 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 884 | if (!name) |
| 885 | return NULL; |
| 886 | strcpy(name->name, dentry->d_name.name); |
| 887 | return name; |
| 888 | } |
| 889 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 890 | static void cgroup_free_fn(struct work_struct *work) |
| 891 | { |
| 892 | struct cgroup *cgrp = container_of(work, struct cgroup, free_work); |
| 893 | struct cgroup_subsys *ss; |
| 894 | |
| 895 | mutex_lock(&cgroup_mutex); |
| 896 | /* |
| 897 | * Release the subsystem state objects. |
| 898 | */ |
| 899 | for_each_subsys(cgrp->root, ss) |
| 900 | ss->css_free(cgrp); |
| 901 | |
| 902 | cgrp->root->number_of_cgroups--; |
| 903 | mutex_unlock(&cgroup_mutex); |
| 904 | |
| 905 | /* |
| 906 | * Drop the active superblock reference that we took when we |
| 907 | * created the cgroup |
| 908 | */ |
| 909 | deactivate_super(cgrp->root->sb); |
| 910 | |
| 911 | /* |
| 912 | * if we're getting rid of the cgroup, refcount should ensure |
| 913 | * that there are no pidlists left. |
| 914 | */ |
| 915 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 916 | |
| 917 | simple_xattrs_free(&cgrp->xattrs); |
| 918 | |
| 919 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 920 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 921 | kfree(cgrp); |
| 922 | } |
| 923 | |
| 924 | static void cgroup_free_rcu(struct rcu_head *head) |
| 925 | { |
| 926 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 927 | |
| 928 | schedule_work(&cgrp->free_work); |
| 929 | } |
| 930 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 932 | { |
| 933 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 934 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 935 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 936 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 937 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 938 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 939 | } else { |
| 940 | struct cfent *cfe = __d_cfe(dentry); |
| 941 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 942 | struct cftype *cft = cfe->type; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 943 | |
| 944 | WARN_ONCE(!list_empty(&cfe->node) && |
| 945 | cgrp != &cgrp->root->top_cgroup, |
| 946 | "cfe still linked for %s\n", cfe->type->name); |
| 947 | kfree(cfe); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 948 | simple_xattrs_free(&cft->xattrs); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 949 | } |
| 950 | iput(inode); |
| 951 | } |
| 952 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 953 | static int cgroup_delete(const struct dentry *d) |
| 954 | { |
| 955 | return 1; |
| 956 | } |
| 957 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 958 | static void remove_dir(struct dentry *d) |
| 959 | { |
| 960 | struct dentry *parent = dget(d->d_parent); |
| 961 | |
| 962 | d_delete(d); |
| 963 | simple_rmdir(parent->d_inode, d); |
| 964 | dput(parent); |
| 965 | } |
| 966 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 967 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 969 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 970 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 971 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 972 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 973 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 974 | /* |
| 975 | * If we're doing cleanup due to failure of cgroup_create(), |
| 976 | * the corresponding @cfe may not exist. |
| 977 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 978 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 979 | struct dentry *d = cfe->dentry; |
| 980 | |
| 981 | if (cft && cfe->type != cft) |
| 982 | continue; |
| 983 | |
| 984 | dget(d); |
| 985 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 986 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 987 | list_del_init(&cfe->node); |
| 988 | dput(d); |
| 989 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 990 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 991 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 994 | /** |
| 995 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 996 | * @dir: directory containing the files |
| 997 | * @base_files: true if the base files should be removed |
| 998 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 999 | */ |
| 1000 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 1001 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1002 | { |
| 1003 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1004 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1005 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1006 | for_each_subsys(cgrp->root, ss) { |
| 1007 | struct cftype_set *set; |
| 1008 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 1009 | continue; |
| 1010 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 1011 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1012 | } |
| 1013 | if (base_files) { |
| 1014 | while (!list_empty(&cgrp->files)) |
| 1015 | cgroup_rm_file(cgrp, NULL); |
| 1016 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * NOTE : the dentry must have been dget()'ed |
| 1021 | */ |
| 1022 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 1023 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1024 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1025 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1026 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1027 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1029 | parent = dentry->d_parent; |
| 1030 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 1031 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1032 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1033 | spin_unlock(&dentry->d_lock); |
| 1034 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1035 | remove_dir(dentry); |
| 1036 | } |
| 1037 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1038 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1039 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1040 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1041 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1042 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1043 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1044 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1046 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1047 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1048 | int i; |
| 1049 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1050 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1051 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1052 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1053 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 1054 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1055 | /* Check that any added subsystems are currently free */ |
| 1056 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1057 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1059 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1060 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1061 | /* |
| 1062 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1063 | * parse_cgroupfs_options should catch that case and refcounts |
| 1064 | * ensure that subsystems won't disappear once selected. |
| 1065 | */ |
| 1066 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1067 | if (ss->root != &rootnode) { |
| 1068 | /* Subsystem isn't free */ |
| 1069 | return -EBUSY; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /* Currently we don't handle adding/removing subsystems when |
| 1074 | * any child cgroups exist. This is theoretically supportable |
| 1075 | * but involves complex error handling, so it's being left until |
| 1076 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1077 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | return -EBUSY; |
| 1079 | |
| 1080 | /* Process each subsystem */ |
| 1081 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1082 | struct cgroup_subsys *ss = subsys[i]; |
| 1083 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1084 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1085 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1086 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1087 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1088 | BUG_ON(!dummytop->subsys[i]); |
| 1089 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1090 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1091 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1092 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1093 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1095 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1096 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1097 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1099 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1100 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1101 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1103 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1104 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1105 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1106 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1107 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1108 | /* subsystem is now free - drop reference on module */ |
| 1109 | module_put(ss->module); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1110 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1111 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1112 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1113 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1114 | /* |
| 1115 | * a refcount was taken, but we already had one, so |
| 1116 | * drop the extra reference. |
| 1117 | */ |
| 1118 | module_put(ss->module); |
| 1119 | #ifdef CONFIG_MODULE_UNLOAD |
| 1120 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1121 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1122 | } else { |
| 1123 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1124 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1127 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1132 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1133 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1134 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1135 | struct cgroup_subsys *ss; |
| 1136 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1137 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1138 | for_each_subsys(root, ss) |
| 1139 | seq_printf(seq, ",%s", ss->name); |
| 1140 | if (test_bit(ROOT_NOPREFIX, &root->flags)) |
| 1141 | seq_puts(seq, ",noprefix"); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1142 | if (test_bit(ROOT_XATTR, &root->flags)) |
| 1143 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1144 | if (strlen(root->release_agent_path)) |
| 1145 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1146 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1147 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1148 | if (strlen(root->name)) |
| 1149 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1150 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1155 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1156 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1157 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1158 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1159 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1160 | /* User explicitly requested empty subsystem */ |
| 1161 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1162 | |
| 1163 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1164 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1165 | }; |
| 1166 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1167 | /* |
| 1168 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1169 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1170 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1171 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1172 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1173 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1174 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1175 | char *token, *o = data; |
| 1176 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1177 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1178 | int i; |
| 1179 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1180 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1181 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1182 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1183 | #ifdef CONFIG_CPUSETS |
| 1184 | mask = ~(1UL << cpuset_subsys_id); |
| 1185 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1186 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1187 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1188 | |
| 1189 | while ((token = strsep(&o, ",")) != NULL) { |
| 1190 | if (!*token) |
| 1191 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1192 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1193 | /* Explicitly have no subsystems */ |
| 1194 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1195 | continue; |
| 1196 | } |
| 1197 | if (!strcmp(token, "all")) { |
| 1198 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1199 | if (one_ss) |
| 1200 | return -EINVAL; |
| 1201 | all_ss = true; |
| 1202 | continue; |
| 1203 | } |
| 1204 | if (!strcmp(token, "noprefix")) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1205 | set_bit(ROOT_NOPREFIX, &opts->flags); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1206 | continue; |
| 1207 | } |
| 1208 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1209 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1210 | continue; |
| 1211 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1212 | if (!strcmp(token, "xattr")) { |
| 1213 | set_bit(ROOT_XATTR, &opts->flags); |
| 1214 | continue; |
| 1215 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1216 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1217 | /* Specifying two release agents is forbidden */ |
| 1218 | if (opts->release_agent) |
| 1219 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1220 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1221 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1222 | if (!opts->release_agent) |
| 1223 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1224 | continue; |
| 1225 | } |
| 1226 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1227 | const char *name = token + 5; |
| 1228 | /* Can't specify an empty name */ |
| 1229 | if (!strlen(name)) |
| 1230 | return -EINVAL; |
| 1231 | /* Must match [\w.-]+ */ |
| 1232 | for (i = 0; i < strlen(name); i++) { |
| 1233 | char c = name[i]; |
| 1234 | if (isalnum(c)) |
| 1235 | continue; |
| 1236 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1237 | continue; |
| 1238 | return -EINVAL; |
| 1239 | } |
| 1240 | /* Specifying two names is forbidden */ |
| 1241 | if (opts->name) |
| 1242 | return -EINVAL; |
| 1243 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1244 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1245 | GFP_KERNEL); |
| 1246 | if (!opts->name) |
| 1247 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1248 | |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1253 | struct cgroup_subsys *ss = subsys[i]; |
| 1254 | if (ss == NULL) |
| 1255 | continue; |
| 1256 | if (strcmp(token, ss->name)) |
| 1257 | continue; |
| 1258 | if (ss->disabled) |
| 1259 | continue; |
| 1260 | |
| 1261 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1262 | if (all_ss) |
| 1263 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1264 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1265 | one_ss = true; |
| 1266 | |
| 1267 | break; |
| 1268 | } |
| 1269 | if (i == CGROUP_SUBSYS_COUNT) |
| 1270 | return -ENOENT; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1275 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1276 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1277 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1278 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1279 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1280 | struct cgroup_subsys *ss = subsys[i]; |
| 1281 | if (ss == NULL) |
| 1282 | continue; |
| 1283 | if (ss->disabled) |
| 1284 | continue; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1285 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1289 | /* Consistency checks */ |
| 1290 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1291 | /* |
| 1292 | * Option noprefix was introduced just for backward compatibility |
| 1293 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1294 | * the cpuset subsystem. |
| 1295 | */ |
| 1296 | if (test_bit(ROOT_NOPREFIX, &opts->flags) && |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1297 | (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1298 | return -EINVAL; |
| 1299 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1300 | |
| 1301 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1302 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1303 | return -EINVAL; |
| 1304 | |
| 1305 | /* |
| 1306 | * We either have to specify by name or by subsystems. (So all |
| 1307 | * empty hierarchies must have a name). |
| 1308 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1309 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1310 | return -EINVAL; |
| 1311 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | /* |
| 1313 | * Grab references on all the modules we'll need, so the subsystems |
| 1314 | * don't dance around before rebind_subsystems attaches them. This may |
| 1315 | * take duplicate reference counts on a subsystem that's already used, |
| 1316 | * but rebind_subsystems handles this case. |
| 1317 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1318 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1319 | unsigned long bit = 1UL << i; |
| 1320 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1321 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1322 | continue; |
| 1323 | if (!try_module_get(subsys[i]->module)) { |
| 1324 | module_pin_failed = true; |
| 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | if (module_pin_failed) { |
| 1329 | /* |
| 1330 | * oops, one of the modules was going away. this means that we |
| 1331 | * raced with a module_delete call, and to the user this is |
| 1332 | * essentially a "subsystem doesn't exist" case. |
| 1333 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1334 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1335 | /* drop refcounts only on the ones we took */ |
| 1336 | unsigned long bit = 1UL << i; |
| 1337 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1338 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1339 | continue; |
| 1340 | module_put(subsys[i]->module); |
| 1341 | } |
| 1342 | return -ENOENT; |
| 1343 | } |
| 1344 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1345 | return 0; |
| 1346 | } |
| 1347 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1348 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1349 | { |
| 1350 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1351 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1352 | unsigned long bit = 1UL << i; |
| 1353 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1354 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1355 | continue; |
| 1356 | module_put(subsys[i]->module); |
| 1357 | } |
| 1358 | } |
| 1359 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1360 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1361 | { |
| 1362 | int ret = 0; |
| 1363 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1364 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1365 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1366 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1367 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1368 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1369 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1370 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1371 | |
| 1372 | /* See what subsystems are wanted */ |
| 1373 | ret = parse_cgroupfs_options(data, &opts); |
| 1374 | if (ret) |
| 1375 | goto out_unlock; |
| 1376 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1377 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1378 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1379 | task_tgid_nr(current), current->comm); |
| 1380 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1381 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1382 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1383 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1384 | /* Don't allow flags or name to change at remount */ |
| 1385 | if (opts.flags != root->flags || |
| 1386 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1387 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1388 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1389 | goto out_unlock; |
| 1390 | } |
| 1391 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1392 | /* |
| 1393 | * Clear out the files of subsystems that should be removed, do |
| 1394 | * this before rebind_subsystems, since rebind_subsystems may |
| 1395 | * change this hierarchy's subsys_list. |
| 1396 | */ |
| 1397 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1398 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1399 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1400 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1401 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1402 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1403 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1404 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1405 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1406 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1407 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1408 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1409 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1410 | if (opts.release_agent) |
| 1411 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1412 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1413 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1414 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1415 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1416 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1417 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1418 | return ret; |
| 1419 | } |
| 1420 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1421 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1422 | .statfs = simple_statfs, |
| 1423 | .drop_inode = generic_delete_inode, |
| 1424 | .show_options = cgroup_show_options, |
| 1425 | .remount_fs = cgroup_remount, |
| 1426 | }; |
| 1427 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1428 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1429 | { |
| 1430 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1431 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1432 | INIT_LIST_HEAD(&cgrp->files); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1433 | INIT_LIST_HEAD(&cgrp->css_sets); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1434 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1435 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1436 | INIT_LIST_HEAD(&cgrp->pidlists); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1437 | INIT_WORK(&cgrp->free_work, cgroup_free_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1438 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1439 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1440 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1441 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1442 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1443 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1444 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1445 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1446 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1447 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1448 | INIT_LIST_HEAD(&root->subsys_list); |
| 1449 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1450 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1451 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1452 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1453 | cgrp->name = &root_cgroup_name; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1454 | cgrp->top_cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1455 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1456 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1459 | static bool init_root_id(struct cgroupfs_root *root) |
| 1460 | { |
| 1461 | int ret = 0; |
| 1462 | |
| 1463 | do { |
| 1464 | if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) |
| 1465 | return false; |
| 1466 | spin_lock(&hierarchy_id_lock); |
| 1467 | /* Try to allocate the next unused ID */ |
| 1468 | ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, |
| 1469 | &root->hierarchy_id); |
| 1470 | if (ret == -ENOSPC) |
| 1471 | /* Try again starting from 0 */ |
| 1472 | ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); |
| 1473 | if (!ret) { |
| 1474 | next_hierarchy_id = root->hierarchy_id + 1; |
| 1475 | } else if (ret != -EAGAIN) { |
| 1476 | /* Can only get here if the 31-bit IDR is full ... */ |
| 1477 | BUG_ON(ret); |
| 1478 | } |
| 1479 | spin_unlock(&hierarchy_id_lock); |
| 1480 | } while (ret); |
| 1481 | return true; |
| 1482 | } |
| 1483 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1484 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1485 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1486 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1487 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1488 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1489 | /* If we asked for a name then it must match */ |
| 1490 | if (opts->name && strcmp(opts->name, root->name)) |
| 1491 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1492 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1493 | /* |
| 1494 | * If we asked for subsystems (or explicitly for no |
| 1495 | * subsystems) then they must match |
| 1496 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1497 | if ((opts->subsys_mask || opts->none) |
| 1498 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1499 | return 0; |
| 1500 | |
| 1501 | return 1; |
| 1502 | } |
| 1503 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1504 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1505 | { |
| 1506 | struct cgroupfs_root *root; |
| 1507 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1508 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1509 | return NULL; |
| 1510 | |
| 1511 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1512 | if (!root) |
| 1513 | return ERR_PTR(-ENOMEM); |
| 1514 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1515 | if (!init_root_id(root)) { |
| 1516 | kfree(root); |
| 1517 | return ERR_PTR(-ENOMEM); |
| 1518 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1519 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1520 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1521 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1522 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1523 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1524 | if (opts->release_agent) |
| 1525 | strcpy(root->release_agent_path, opts->release_agent); |
| 1526 | if (opts->name) |
| 1527 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1528 | if (opts->cpuset_clone_children) |
| 1529 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1530 | return root; |
| 1531 | } |
| 1532 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1533 | static void cgroup_drop_root(struct cgroupfs_root *root) |
| 1534 | { |
| 1535 | if (!root) |
| 1536 | return; |
| 1537 | |
| 1538 | BUG_ON(!root->hierarchy_id); |
| 1539 | spin_lock(&hierarchy_id_lock); |
| 1540 | ida_remove(&hierarchy_ida, root->hierarchy_id); |
| 1541 | spin_unlock(&hierarchy_id_lock); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1542 | ida_destroy(&root->cgroup_ida); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1543 | kfree(root); |
| 1544 | } |
| 1545 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1546 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1547 | { |
| 1548 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1549 | struct cgroup_sb_opts *opts = data; |
| 1550 | |
| 1551 | /* If we don't have a new root, we can't set up a new sb */ |
| 1552 | if (!opts->new_root) |
| 1553 | return -EINVAL; |
| 1554 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1555 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1556 | |
| 1557 | ret = set_anon_super(sb, NULL); |
| 1558 | if (ret) |
| 1559 | return ret; |
| 1560 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1561 | sb->s_fs_info = opts->new_root; |
| 1562 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1563 | |
| 1564 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1565 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1566 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1567 | sb->s_op = &cgroup_ops; |
| 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1573 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1574 | static const struct dentry_operations cgroup_dops = { |
| 1575 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1576 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1577 | }; |
| 1578 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1579 | struct inode *inode = |
| 1580 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1581 | |
| 1582 | if (!inode) |
| 1583 | return -ENOMEM; |
| 1584 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1585 | inode->i_fop = &simple_dir_operations; |
| 1586 | inode->i_op = &cgroup_dir_inode_operations; |
| 1587 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1588 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1589 | sb->s_root = d_make_root(inode); |
| 1590 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1591 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1592 | /* for everything else we want ->d_op set */ |
| 1593 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
| 1596 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1597 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1598 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1599 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1600 | { |
| 1601 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1602 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1603 | int ret = 0; |
| 1604 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1605 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1606 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 | |
| 1608 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1609 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1611 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1612 | if (ret) |
| 1613 | goto out_err; |
| 1614 | |
| 1615 | /* |
| 1616 | * Allocate a new cgroup root. We may not need it if we're |
| 1617 | * reusing an existing hierarchy. |
| 1618 | */ |
| 1619 | new_root = cgroup_root_from_opts(&opts); |
| 1620 | if (IS_ERR(new_root)) { |
| 1621 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1622 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1623 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1624 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1625 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1626 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1627 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1628 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1629 | ret = PTR_ERR(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1630 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1631 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1634 | root = sb->s_fs_info; |
| 1635 | BUG_ON(!root); |
| 1636 | if (root == opts.new_root) { |
| 1637 | /* We used the new root structure, so this is a new hierarchy */ |
| 1638 | struct list_head tmp_cg_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1639 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1640 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1641 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1642 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1643 | struct css_set *cg; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1644 | |
| 1645 | BUG_ON(sb->s_root != NULL); |
| 1646 | |
| 1647 | ret = cgroup_get_rootdir(sb); |
| 1648 | if (ret) |
| 1649 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1650 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1652 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1654 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1656 | /* Check for name clashes with existing mounts */ |
| 1657 | ret = -EBUSY; |
| 1658 | if (strlen(root->name)) |
| 1659 | for_each_active_root(existing_root) |
| 1660 | if (!strcmp(existing_root->name, root->name)) |
| 1661 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1662 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1663 | /* |
| 1664 | * We're accessing css_set_count without locking |
| 1665 | * css_set_lock here, but that's OK - it can only be |
| 1666 | * increased by someone holding cgroup_lock, and |
| 1667 | * that's us. The worst that can happen is that we |
| 1668 | * have some link structures left over |
| 1669 | */ |
| 1670 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1671 | if (ret) |
| 1672 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1673 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1674 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | if (ret == -EBUSY) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1676 | free_cg_links(&tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1677 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1678 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1679 | /* |
| 1680 | * There must be no failure case after here, since rebinding |
| 1681 | * takes care of subsystems' refcounts, which are explicitly |
| 1682 | * dropped in the failure exit path. |
| 1683 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1684 | |
| 1685 | /* EBUSY should be the only error here */ |
| 1686 | BUG_ON(ret); |
| 1687 | |
| 1688 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1689 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1690 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1691 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 | root->top_cgroup.dentry = sb->s_root; |
| 1693 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1694 | /* Link the top cgroup in this hierarchy into all |
| 1695 | * the css_set objects */ |
| 1696 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1697 | hash_for_each(css_set_table, i, cg, hlist) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1698 | link_css_set(&tmp_cg_links, cg, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1699 | write_unlock(&css_set_lock); |
| 1700 | |
| 1701 | free_cg_links(&tmp_cg_links); |
| 1702 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1703 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1704 | BUG_ON(root->number_of_cgroups != 1); |
| 1705 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1706 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1707 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1708 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1709 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1711 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1712 | } else { |
| 1713 | /* |
| 1714 | * We re-used an existing hierarchy - the new root (if |
| 1715 | * any) is not needed |
| 1716 | */ |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1717 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1718 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1719 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1722 | kfree(opts.release_agent); |
| 1723 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1724 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1726 | unlock_drop: |
| 1727 | mutex_unlock(&cgroup_root_mutex); |
| 1728 | mutex_unlock(&cgroup_mutex); |
| 1729 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1730 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1731 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1732 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1733 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1734 | out_err: |
| 1735 | kfree(opts.release_agent); |
| 1736 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1737 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1741 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1742 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1743 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1744 | struct cg_cgroup_link *link; |
| 1745 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1746 | |
| 1747 | BUG_ON(!root); |
| 1748 | |
| 1749 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1750 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1751 | |
| 1752 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1753 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1754 | |
| 1755 | /* Rebind all subsystems back to the default hierarchy */ |
| 1756 | ret = rebind_subsystems(root, 0); |
| 1757 | /* Shouldn't be able to fail ... */ |
| 1758 | BUG_ON(ret); |
| 1759 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1760 | /* |
| 1761 | * Release all the links from css_sets to this hierarchy's |
| 1762 | * root cgroup |
| 1763 | */ |
| 1764 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1765 | |
| 1766 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1767 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1768 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1769 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1770 | kfree(link); |
| 1771 | } |
| 1772 | write_unlock(&css_set_lock); |
| 1773 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1774 | if (!list_empty(&root->root_list)) { |
| 1775 | list_del(&root->root_list); |
| 1776 | root_count--; |
| 1777 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1778 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1779 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1780 | mutex_unlock(&cgroup_mutex); |
| 1781 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1782 | simple_xattrs_free(&cgrp->xattrs); |
| 1783 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1784 | kill_litter_super(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1785 | cgroup_drop_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | static struct file_system_type cgroup_fs_type = { |
| 1789 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1790 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1791 | .kill_sb = cgroup_kill_sb, |
| 1792 | }; |
| 1793 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1794 | static struct kobject *cgroup_kobj; |
| 1795 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1796 | /** |
| 1797 | * cgroup_path - generate the path of a cgroup |
| 1798 | * @cgrp: the cgroup in question |
| 1799 | * @buf: the buffer to write the path into |
| 1800 | * @buflen: the length of the buffer |
| 1801 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1802 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1803 | * |
| 1804 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1805 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1806 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1807 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1809 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1810 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1811 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1813 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1814 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1815 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1816 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1817 | rcu_read_lock(); |
| 1818 | while (cgrp) { |
| 1819 | const char *name = cgroup_name(cgrp); |
| 1820 | int len; |
| 1821 | |
| 1822 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1823 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1824 | goto out; |
| 1825 | memcpy(start, name, len); |
| 1826 | |
| 1827 | if (!cgrp->parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1828 | break; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1829 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1830 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1831 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1832 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1833 | |
| 1834 | cgrp = cgrp->parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1835 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1836 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1837 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1838 | out: |
| 1839 | rcu_read_unlock(); |
| 1840 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1841 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1842 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1843 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1844 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1845 | * Control Group taskset |
| 1846 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1847 | struct task_and_cgroup { |
| 1848 | struct task_struct *task; |
| 1849 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1850 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1851 | }; |
| 1852 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1853 | struct cgroup_taskset { |
| 1854 | struct task_and_cgroup single; |
| 1855 | struct flex_array *tc_array; |
| 1856 | int tc_array_len; |
| 1857 | int idx; |
| 1858 | struct cgroup *cur_cgrp; |
| 1859 | }; |
| 1860 | |
| 1861 | /** |
| 1862 | * cgroup_taskset_first - reset taskset and return the first task |
| 1863 | * @tset: taskset of interest |
| 1864 | * |
| 1865 | * @tset iteration is initialized and the first task is returned. |
| 1866 | */ |
| 1867 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1868 | { |
| 1869 | if (tset->tc_array) { |
| 1870 | tset->idx = 0; |
| 1871 | return cgroup_taskset_next(tset); |
| 1872 | } else { |
| 1873 | tset->cur_cgrp = tset->single.cgrp; |
| 1874 | return tset->single.task; |
| 1875 | } |
| 1876 | } |
| 1877 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1878 | |
| 1879 | /** |
| 1880 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1881 | * @tset: taskset of interest |
| 1882 | * |
| 1883 | * Return the next task in @tset. Iteration must have been initialized |
| 1884 | * with cgroup_taskset_first(). |
| 1885 | */ |
| 1886 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1887 | { |
| 1888 | struct task_and_cgroup *tc; |
| 1889 | |
| 1890 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1891 | return NULL; |
| 1892 | |
| 1893 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1894 | tset->cur_cgrp = tc->cgrp; |
| 1895 | return tc->task; |
| 1896 | } |
| 1897 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1898 | |
| 1899 | /** |
| 1900 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1901 | * @tset: taskset of interest |
| 1902 | * |
| 1903 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1904 | * function must be preceded by either cgroup_taskset_first() or |
| 1905 | * cgroup_taskset_next(). |
| 1906 | */ |
| 1907 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1908 | { |
| 1909 | return tset->cur_cgrp; |
| 1910 | } |
| 1911 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1912 | |
| 1913 | /** |
| 1914 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1915 | * @tset: taskset of interest |
| 1916 | */ |
| 1917 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1918 | { |
| 1919 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1920 | } |
| 1921 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1922 | |
| 1923 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1924 | /* |
| 1925 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1926 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1927 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1928 | */ |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 1929 | static void cgroup_task_migrate(struct cgroup *oldcgrp, |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1930 | struct task_struct *tsk, struct css_set *newcg) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1931 | { |
| 1932 | struct css_set *oldcg; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1933 | |
| 1934 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1935 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1936 | * setting such that we can't race against cgroup_exit() changing the |
| 1937 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1938 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1939 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1940 | oldcg = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1941 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | task_lock(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1943 | rcu_assign_pointer(tsk->cgroups, newcg); |
| 1944 | task_unlock(tsk); |
| 1945 | |
| 1946 | /* Update the css_set linked lists if we're using them */ |
| 1947 | write_lock(&css_set_lock); |
| 1948 | if (!list_empty(&tsk->cg_list)) |
| 1949 | list_move(&tsk->cg_list, &newcg->tasks); |
| 1950 | write_unlock(&css_set_lock); |
| 1951 | |
| 1952 | /* |
| 1953 | * We just gained a reference on oldcg by taking it from the task. As |
| 1954 | * trading it for newcg is protected by cgroup_mutex, we're safe to drop |
| 1955 | * it here; it will be freed under RCU. |
| 1956 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1957 | set_bit(CGRP_RELEASABLE, &oldcgrp->flags); |
Daisuke Nishimura | 1f5320d | 2012-10-04 16:37:16 +0900 | [diff] [blame] | 1958 | put_css_set(oldcg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1961 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1962 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1964 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1965 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1966 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1967 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1968 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1969 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1970 | int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1971 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1972 | { |
| 1973 | int retval, i, group_size; |
| 1974 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | struct cgroupfs_root *root = cgrp->root; |
| 1976 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1977 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1978 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1979 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1980 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1981 | |
| 1982 | /* |
| 1983 | * step 0: in order to do expensive, possibly blocking operations for |
| 1984 | * every thread, we cannot iterate the thread group list, since it needs |
| 1985 | * 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] | 1986 | * group - group_rwsem prevents new threads from appearing, and if |
| 1987 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1988 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1989 | if (threadgroup) |
| 1990 | group_size = get_nr_threads(tsk); |
| 1991 | else |
| 1992 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1993 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1994 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1995 | if (!group) |
| 1996 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1997 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1998 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1999 | if (retval) |
| 2000 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2001 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2002 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2003 | /* |
| 2004 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2005 | * already PF_EXITING could be freed from underneath us unless we |
| 2006 | * take an rcu_read_lock. |
| 2007 | */ |
| 2008 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2009 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2010 | struct task_and_cgroup ent; |
| 2011 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2012 | /* @tsk either already exited or can't exit until the end */ |
| 2013 | if (tsk->flags & PF_EXITING) |
| 2014 | continue; |
| 2015 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2016 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2017 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2018 | ent.task = tsk; |
| 2019 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2020 | /* nothing to do if this task is already in the cgroup */ |
| 2021 | if (ent.cgrp == cgrp) |
| 2022 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2023 | /* |
| 2024 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2025 | * earlier, but it's good form to communicate our expectations. |
| 2026 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2027 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2028 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2030 | |
| 2031 | if (!threadgroup) |
| 2032 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2033 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2034 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 | /* remember the number of threads in the array for later. */ |
| 2036 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2037 | tset.tc_array = group; |
| 2038 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2039 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2040 | /* methods shouldn't be called if no task is actually migrating */ |
| 2041 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2042 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2043 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2044 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2045 | /* |
| 2046 | * step 1: check that we can legitimately attach to the cgroup. |
| 2047 | */ |
| 2048 | for_each_subsys(root, ss) { |
| 2049 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2050 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2051 | if (retval) { |
| 2052 | failed_ss = ss; |
| 2053 | goto out_cancel_attach; |
| 2054 | } |
| 2055 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2060 | * we use find_css_set, which allocates a new one if necessary. |
| 2061 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2062 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2063 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2064 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2065 | if (!tc->cg) { |
| 2066 | retval = -ENOMEM; |
| 2067 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2072 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2073 | * proceed to move all tasks to the new cgroup. There are no |
| 2074 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2076 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2077 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2078 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | } |
| 2080 | /* nothing is sensitive to fork() after this point. */ |
| 2081 | |
| 2082 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2083 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | */ |
| 2085 | for_each_subsys(root, ss) { |
| 2086 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2087 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * step 5: success! and cleanup |
| 2092 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2093 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2094 | out_put_css_set_refs: |
| 2095 | if (retval) { |
| 2096 | for (i = 0; i < group_size; i++) { |
| 2097 | tc = flex_array_get(group, i); |
| 2098 | if (!tc->cg) |
| 2099 | break; |
| 2100 | put_css_set(tc->cg); |
| 2101 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2102 | } |
| 2103 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | if (retval) { |
| 2105 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2106 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2107 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2109 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2110 | } |
| 2111 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2113 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2114 | return retval; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | * 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] | 2119 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2120 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2121 | */ |
| 2122 | 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] | 2123 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2124 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2125 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2126 | int ret; |
| 2127 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2128 | if (!cgroup_lock_live_group(cgrp)) |
| 2129 | return -ENODEV; |
| 2130 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2131 | retry_find_task: |
| 2132 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2133 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2134 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2135 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2136 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2137 | ret= -ESRCH; |
| 2138 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2139 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2140 | /* |
| 2141 | * even if we're attaching all tasks in the thread group, we |
| 2142 | * only need to check permissions on one of them. |
| 2143 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2144 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2145 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2146 | !uid_eq(cred->euid, tcred->uid) && |
| 2147 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2148 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2149 | ret = -EACCES; |
| 2150 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2151 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2152 | } else |
| 2153 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2154 | |
| 2155 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2156 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2157 | |
| 2158 | /* |
| 2159 | * Workqueue threads may acquire PF_THREAD_BOUND and become |
| 2160 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2161 | * with no rt_runtime allocated. Just say no. |
| 2162 | */ |
| 2163 | if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) { |
| 2164 | ret = -EINVAL; |
| 2165 | rcu_read_unlock(); |
| 2166 | goto out_unlock_cgroup; |
| 2167 | } |
| 2168 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2169 | get_task_struct(tsk); |
| 2170 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2171 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2172 | threadgroup_lock(tsk); |
| 2173 | if (threadgroup) { |
| 2174 | if (!thread_group_leader(tsk)) { |
| 2175 | /* |
| 2176 | * a race with de_thread from another thread's exec() |
| 2177 | * may strip us of our leadership, if this happens, |
| 2178 | * there is no choice but to throw this task away and |
| 2179 | * try again; this is |
| 2180 | * "double-double-toil-and-trouble-check locking". |
| 2181 | */ |
| 2182 | threadgroup_unlock(tsk); |
| 2183 | put_task_struct(tsk); |
| 2184 | goto retry_find_task; |
| 2185 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2189 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2190 | threadgroup_unlock(tsk); |
| 2191 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2192 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2193 | out_unlock_cgroup: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2194 | cgroup_unlock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2195 | return ret; |
| 2196 | } |
| 2197 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2198 | /** |
| 2199 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2200 | * @from: attach to all cgroups of a given task |
| 2201 | * @tsk: the task to be attached |
| 2202 | */ |
| 2203 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2204 | { |
| 2205 | struct cgroupfs_root *root; |
| 2206 | int retval = 0; |
| 2207 | |
| 2208 | cgroup_lock(); |
| 2209 | for_each_active_root(root) { |
| 2210 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2211 | |
| 2212 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2213 | if (retval) |
| 2214 | break; |
| 2215 | } |
| 2216 | cgroup_unlock(); |
| 2217 | |
| 2218 | return retval; |
| 2219 | } |
| 2220 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2221 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2222 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2223 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2224 | return attach_task_by_pid(cgrp, pid, false); |
| 2225 | } |
| 2226 | |
| 2227 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2228 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2229 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2232 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2233 | const char *buffer) |
| 2234 | { |
| 2235 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2236 | if (strlen(buffer) >= PATH_MAX) |
| 2237 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2238 | if (!cgroup_lock_live_group(cgrp)) |
| 2239 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2240 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2241 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2242 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2243 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2248 | struct seq_file *seq) |
| 2249 | { |
| 2250 | if (!cgroup_lock_live_group(cgrp)) |
| 2251 | return -ENODEV; |
| 2252 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2253 | seq_putc(seq, '\n'); |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2254 | cgroup_unlock(); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2255 | return 0; |
| 2256 | } |
| 2257 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2258 | /* A buffer size big enough for numbers or short strings */ |
| 2259 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2260 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2261 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2262 | struct file *file, |
| 2263 | const char __user *userbuf, |
| 2264 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2265 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2266 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2267 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2268 | char *end; |
| 2269 | |
| 2270 | if (!nbytes) |
| 2271 | return -EINVAL; |
| 2272 | if (nbytes >= sizeof(buffer)) |
| 2273 | return -E2BIG; |
| 2274 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2275 | return -EFAULT; |
| 2276 | |
| 2277 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2278 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2279 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2280 | if (*end) |
| 2281 | return -EINVAL; |
| 2282 | retval = cft->write_u64(cgrp, cft, val); |
| 2283 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2284 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2285 | if (*end) |
| 2286 | return -EINVAL; |
| 2287 | retval = cft->write_s64(cgrp, cft, val); |
| 2288 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2289 | if (!retval) |
| 2290 | retval = nbytes; |
| 2291 | return retval; |
| 2292 | } |
| 2293 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2294 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2295 | struct file *file, |
| 2296 | const char __user *userbuf, |
| 2297 | size_t nbytes, loff_t *unused_ppos) |
| 2298 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2299 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2300 | int retval = 0; |
| 2301 | size_t max_bytes = cft->max_write_len; |
| 2302 | char *buffer = local_buffer; |
| 2303 | |
| 2304 | if (!max_bytes) |
| 2305 | max_bytes = sizeof(local_buffer) - 1; |
| 2306 | if (nbytes >= max_bytes) |
| 2307 | return -E2BIG; |
| 2308 | /* Allocate a dynamic buffer if we need one */ |
| 2309 | if (nbytes >= sizeof(local_buffer)) { |
| 2310 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2311 | if (buffer == NULL) |
| 2312 | return -ENOMEM; |
| 2313 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2314 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2315 | retval = -EFAULT; |
| 2316 | goto out; |
| 2317 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2318 | |
| 2319 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2320 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2321 | if (!retval) |
| 2322 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2323 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2324 | if (buffer != local_buffer) |
| 2325 | kfree(buffer); |
| 2326 | return retval; |
| 2327 | } |
| 2328 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2329 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2330 | size_t nbytes, loff_t *ppos) |
| 2331 | { |
| 2332 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2333 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2334 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2335 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2336 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2337 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2338 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2339 | if (cft->write_u64 || cft->write_s64) |
| 2340 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2341 | if (cft->write_string) |
| 2342 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2343 | if (cft->trigger) { |
| 2344 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2345 | return ret ? ret : nbytes; |
| 2346 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2347 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2350 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2351 | struct file *file, |
| 2352 | char __user *buf, size_t nbytes, |
| 2353 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2354 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2355 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2356 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2357 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2358 | |
| 2359 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2360 | } |
| 2361 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2362 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2363 | struct file *file, |
| 2364 | char __user *buf, size_t nbytes, |
| 2365 | loff_t *ppos) |
| 2366 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2367 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2368 | s64 val = cft->read_s64(cgrp, cft); |
| 2369 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2370 | |
| 2371 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2372 | } |
| 2373 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2374 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2375 | size_t nbytes, loff_t *ppos) |
| 2376 | { |
| 2377 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2378 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2379 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2380 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2381 | return -ENODEV; |
| 2382 | |
| 2383 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2384 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2385 | if (cft->read_u64) |
| 2386 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2387 | if (cft->read_s64) |
| 2388 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2389 | return -EINVAL; |
| 2390 | } |
| 2391 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2392 | /* |
| 2393 | * seqfile ops/methods for returning structured data. Currently just |
| 2394 | * supports string->u64 maps, but can be extended in future. |
| 2395 | */ |
| 2396 | |
| 2397 | struct cgroup_seqfile_state { |
| 2398 | struct cftype *cft; |
| 2399 | struct cgroup *cgroup; |
| 2400 | }; |
| 2401 | |
| 2402 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2403 | { |
| 2404 | struct seq_file *sf = cb->state; |
| 2405 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2406 | } |
| 2407 | |
| 2408 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2409 | { |
| 2410 | struct cgroup_seqfile_state *state = m->private; |
| 2411 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2412 | if (cft->read_map) { |
| 2413 | struct cgroup_map_cb cb = { |
| 2414 | .fill = cgroup_map_add, |
| 2415 | .state = m, |
| 2416 | }; |
| 2417 | return cft->read_map(state->cgroup, cft, &cb); |
| 2418 | } |
| 2419 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2422 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2423 | { |
| 2424 | struct seq_file *seq = file->private_data; |
| 2425 | kfree(seq->private); |
| 2426 | return single_release(inode, file); |
| 2427 | } |
| 2428 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2429 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2430 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2431 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2432 | .llseek = seq_lseek, |
| 2433 | .release = cgroup_seqfile_release, |
| 2434 | }; |
| 2435 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2436 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2437 | { |
| 2438 | int err; |
| 2439 | struct cftype *cft; |
| 2440 | |
| 2441 | err = generic_file_open(inode, file); |
| 2442 | if (err) |
| 2443 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2444 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2445 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2446 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2447 | struct cgroup_seqfile_state *state = |
| 2448 | kzalloc(sizeof(*state), GFP_USER); |
| 2449 | if (!state) |
| 2450 | return -ENOMEM; |
| 2451 | state->cft = cft; |
| 2452 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2453 | file->f_op = &cgroup_seqfile_operations; |
| 2454 | err = single_open(file, cgroup_seqfile_show, state); |
| 2455 | if (err < 0) |
| 2456 | kfree(state); |
| 2457 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2458 | err = cft->open(inode, file); |
| 2459 | else |
| 2460 | err = 0; |
| 2461 | |
| 2462 | return err; |
| 2463 | } |
| 2464 | |
| 2465 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2466 | { |
| 2467 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2468 | if (cft->release) |
| 2469 | return cft->release(inode, file); |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2475 | */ |
| 2476 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2477 | struct inode *new_dir, struct dentry *new_dentry) |
| 2478 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2479 | int ret; |
| 2480 | struct cgroup_name *name, *old_name; |
| 2481 | struct cgroup *cgrp; |
| 2482 | |
| 2483 | /* |
| 2484 | * It's convinient to use parent dir's i_mutex to protected |
| 2485 | * cgrp->name. |
| 2486 | */ |
| 2487 | lockdep_assert_held(&old_dir->i_mutex); |
| 2488 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2489 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2490 | return -ENOTDIR; |
| 2491 | if (new_dentry->d_inode) |
| 2492 | return -EEXIST; |
| 2493 | if (old_dir != new_dir) |
| 2494 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2495 | |
| 2496 | cgrp = __d_cgrp(old_dentry); |
| 2497 | |
| 2498 | name = cgroup_alloc_name(new_dentry); |
| 2499 | if (!name) |
| 2500 | return -ENOMEM; |
| 2501 | |
| 2502 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2503 | if (ret) { |
| 2504 | kfree(name); |
| 2505 | return ret; |
| 2506 | } |
| 2507 | |
| 2508 | old_name = cgrp->name; |
| 2509 | rcu_assign_pointer(cgrp->name, name); |
| 2510 | |
| 2511 | kfree_rcu(old_name, rcu_head); |
| 2512 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2515 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2516 | { |
| 2517 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2518 | return &__d_cgrp(dentry)->xattrs; |
| 2519 | else |
| 2520 | return &__d_cft(dentry)->xattrs; |
| 2521 | } |
| 2522 | |
| 2523 | static inline int xattr_enabled(struct dentry *dentry) |
| 2524 | { |
| 2525 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
| 2526 | return test_bit(ROOT_XATTR, &root->flags); |
| 2527 | } |
| 2528 | |
| 2529 | static bool is_valid_xattr(const char *name) |
| 2530 | { |
| 2531 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2532 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2533 | return true; |
| 2534 | return false; |
| 2535 | } |
| 2536 | |
| 2537 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2538 | const void *val, size_t size, int flags) |
| 2539 | { |
| 2540 | if (!xattr_enabled(dentry)) |
| 2541 | return -EOPNOTSUPP; |
| 2542 | if (!is_valid_xattr(name)) |
| 2543 | return -EINVAL; |
| 2544 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2545 | } |
| 2546 | |
| 2547 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2548 | { |
| 2549 | if (!xattr_enabled(dentry)) |
| 2550 | return -EOPNOTSUPP; |
| 2551 | if (!is_valid_xattr(name)) |
| 2552 | return -EINVAL; |
| 2553 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2554 | } |
| 2555 | |
| 2556 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2557 | void *buf, size_t size) |
| 2558 | { |
| 2559 | if (!xattr_enabled(dentry)) |
| 2560 | return -EOPNOTSUPP; |
| 2561 | if (!is_valid_xattr(name)) |
| 2562 | return -EINVAL; |
| 2563 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2564 | } |
| 2565 | |
| 2566 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2567 | { |
| 2568 | if (!xattr_enabled(dentry)) |
| 2569 | return -EOPNOTSUPP; |
| 2570 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2571 | } |
| 2572 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2573 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2574 | .read = cgroup_file_read, |
| 2575 | .write = cgroup_file_write, |
| 2576 | .llseek = generic_file_llseek, |
| 2577 | .open = cgroup_file_open, |
| 2578 | .release = cgroup_file_release, |
| 2579 | }; |
| 2580 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2581 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2582 | .setxattr = cgroup_setxattr, |
| 2583 | .getxattr = cgroup_getxattr, |
| 2584 | .listxattr = cgroup_listxattr, |
| 2585 | .removexattr = cgroup_removexattr, |
| 2586 | }; |
| 2587 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2588 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2589 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2590 | .mkdir = cgroup_mkdir, |
| 2591 | .rmdir = cgroup_rmdir, |
| 2592 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2593 | .setxattr = cgroup_setxattr, |
| 2594 | .getxattr = cgroup_getxattr, |
| 2595 | .listxattr = cgroup_listxattr, |
| 2596 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2597 | }; |
| 2598 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2599 | 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] | 2600 | { |
| 2601 | if (dentry->d_name.len > NAME_MAX) |
| 2602 | return ERR_PTR(-ENAMETOOLONG); |
| 2603 | d_add(dentry, NULL); |
| 2604 | return NULL; |
| 2605 | } |
| 2606 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2607 | /* |
| 2608 | * Check if a file is a control file |
| 2609 | */ |
| 2610 | static inline struct cftype *__file_cft(struct file *file) |
| 2611 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2612 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2613 | return ERR_PTR(-EINVAL); |
| 2614 | return __d_cft(file->f_dentry); |
| 2615 | } |
| 2616 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2617 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2618 | struct super_block *sb) |
| 2619 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2620 | struct inode *inode; |
| 2621 | |
| 2622 | if (!dentry) |
| 2623 | return -ENOENT; |
| 2624 | if (dentry->d_inode) |
| 2625 | return -EEXIST; |
| 2626 | |
| 2627 | inode = cgroup_new_inode(mode, sb); |
| 2628 | if (!inode) |
| 2629 | return -ENOMEM; |
| 2630 | |
| 2631 | if (S_ISDIR(mode)) { |
| 2632 | inode->i_op = &cgroup_dir_inode_operations; |
| 2633 | inode->i_fop = &simple_dir_operations; |
| 2634 | |
| 2635 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2636 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2637 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2638 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2639 | /* |
| 2640 | * Control reaches here with cgroup_mutex held. |
| 2641 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2642 | * want to populate it immediately without releasing |
| 2643 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2644 | * yet, trylock will always succeed without affecting |
| 2645 | * lockdep checks. |
| 2646 | */ |
| 2647 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2648 | } else if (S_ISREG(mode)) { |
| 2649 | inode->i_size = 0; |
| 2650 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2651 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2652 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2653 | d_instantiate(dentry, inode); |
| 2654 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2658 | /** |
| 2659 | * cgroup_file_mode - deduce file mode of a control file |
| 2660 | * @cft: the control file in question |
| 2661 | * |
| 2662 | * returns cft->mode if ->mode is not 0 |
| 2663 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2664 | * returns S_IRUGO if it has only a read handler |
| 2665 | * returns S_IWUSR if it has only a write hander |
| 2666 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2667 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2668 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2669 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2670 | |
| 2671 | if (cft->mode) |
| 2672 | return cft->mode; |
| 2673 | |
| 2674 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2675 | cft->read_map || cft->read_seq_string) |
| 2676 | mode |= S_IRUGO; |
| 2677 | |
| 2678 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2679 | cft->write_string || cft->trigger) |
| 2680 | mode |= S_IWUSR; |
| 2681 | |
| 2682 | return mode; |
| 2683 | } |
| 2684 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2685 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2686 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2687 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2688 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2689 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2690 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2691 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2692 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2693 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2694 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2695 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2696 | simple_xattrs_init(&cft->xattrs); |
| 2697 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2698 | if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2699 | strcpy(name, subsys->name); |
| 2700 | strcat(name, "."); |
| 2701 | } |
| 2702 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2703 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2704 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2705 | |
| 2706 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2707 | if (!cfe) |
| 2708 | return -ENOMEM; |
| 2709 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2710 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2711 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2712 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2713 | goto out; |
| 2714 | } |
| 2715 | |
| 2716 | mode = cgroup_file_mode(cft); |
| 2717 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2718 | if (!error) { |
| 2719 | cfe->type = (void *)cft; |
| 2720 | cfe->dentry = dentry; |
| 2721 | dentry->d_fsdata = cfe; |
| 2722 | list_add_tail(&cfe->node, &parent->files); |
| 2723 | cfe = NULL; |
| 2724 | } |
| 2725 | dput(dentry); |
| 2726 | out: |
| 2727 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2728 | return error; |
| 2729 | } |
| 2730 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2731 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2732 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2733 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2734 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2735 | int err, ret = 0; |
| 2736 | |
| 2737 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2738 | /* does cft->flags tell us to skip this file on @cgrp? */ |
| 2739 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2740 | continue; |
| 2741 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2742 | continue; |
| 2743 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2744 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2745 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2746 | if (err) |
| 2747 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2748 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2749 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2750 | } else { |
| 2751 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2752 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2753 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2754 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2755 | } |
| 2756 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2757 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2758 | |
| 2759 | static void cgroup_cfts_prepare(void) |
| 2760 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2761 | { |
| 2762 | /* |
| 2763 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2764 | * the existing cgroups under cgroup_mutex and create files. |
| 2765 | * Instead, we increment reference on all cgroups and build list of |
| 2766 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2767 | * exclusive access to the field. |
| 2768 | */ |
| 2769 | mutex_lock(&cgroup_cft_mutex); |
| 2770 | mutex_lock(&cgroup_mutex); |
| 2771 | } |
| 2772 | |
| 2773 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2774 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2775 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2776 | { |
| 2777 | LIST_HEAD(pending); |
| 2778 | struct cgroup *cgrp, *n; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2779 | |
| 2780 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
| 2781 | if (cfts && ss->root != &rootnode) { |
| 2782 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2783 | dget(cgrp->dentry); |
| 2784 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | mutex_unlock(&cgroup_mutex); |
| 2789 | |
| 2790 | /* |
| 2791 | * All new cgroups will see @cfts update on @ss->cftsets. Add/rm |
| 2792 | * files for all cgroups which were created before. |
| 2793 | */ |
| 2794 | list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { |
| 2795 | struct inode *inode = cgrp->dentry->d_inode; |
| 2796 | |
| 2797 | mutex_lock(&inode->i_mutex); |
| 2798 | mutex_lock(&cgroup_mutex); |
| 2799 | if (!cgroup_is_removed(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2800 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2801 | mutex_unlock(&cgroup_mutex); |
| 2802 | mutex_unlock(&inode->i_mutex); |
| 2803 | |
| 2804 | list_del_init(&cgrp->cft_q_node); |
| 2805 | dput(cgrp->dentry); |
| 2806 | } |
| 2807 | |
| 2808 | mutex_unlock(&cgroup_cft_mutex); |
| 2809 | } |
| 2810 | |
| 2811 | /** |
| 2812 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2813 | * @ss: target cgroup subsystem |
| 2814 | * @cfts: zero-length name terminated array of cftypes |
| 2815 | * |
| 2816 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2817 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2818 | * have them too. This function can be called anytime whether @ss is |
| 2819 | * attached or not. |
| 2820 | * |
| 2821 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2822 | * function currently returns 0 as long as @cfts registration is successful |
| 2823 | * even if some file creation attempts on existing cgroups fail. |
| 2824 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2825 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2826 | { |
| 2827 | struct cftype_set *set; |
| 2828 | |
| 2829 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2830 | if (!set) |
| 2831 | return -ENOMEM; |
| 2832 | |
| 2833 | cgroup_cfts_prepare(); |
| 2834 | set->cfts = cfts; |
| 2835 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2836 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2837 | |
| 2838 | return 0; |
| 2839 | } |
| 2840 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2841 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2842 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2843 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2844 | * @ss: target cgroup subsystem |
| 2845 | * @cfts: zero-length name terminated array of cftypes |
| 2846 | * |
| 2847 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2848 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2849 | * won't have them either. This function can be called anytime whether @ss |
| 2850 | * is attached or not. |
| 2851 | * |
| 2852 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2853 | * registered with @ss. |
| 2854 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2855 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2856 | { |
| 2857 | struct cftype_set *set; |
| 2858 | |
| 2859 | cgroup_cfts_prepare(); |
| 2860 | |
| 2861 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2862 | if (set->cfts == cfts) { |
| 2863 | list_del_init(&set->node); |
| 2864 | cgroup_cfts_commit(ss, cfts, false); |
| 2865 | return 0; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | cgroup_cfts_commit(ss, NULL, false); |
| 2870 | return -ENOENT; |
| 2871 | } |
| 2872 | |
| 2873 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2874 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2875 | * @cgrp: the cgroup in question |
| 2876 | * |
| 2877 | * Return the number of tasks in the cgroup. |
| 2878 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2879 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2880 | { |
| 2881 | int count = 0; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2882 | struct cg_cgroup_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2883 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2884 | read_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2885 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2886 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2889 | return count; |
| 2890 | } |
| 2891 | |
| 2892 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2893 | * Advance a list_head iterator. The iterator should be positioned at |
| 2894 | * the start of a css_set |
| 2895 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2896 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2897 | struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2898 | { |
| 2899 | struct list_head *l = it->cg_link; |
| 2900 | struct cg_cgroup_link *link; |
| 2901 | struct css_set *cg; |
| 2902 | |
| 2903 | /* Advance to the next non-empty css_set */ |
| 2904 | do { |
| 2905 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2906 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2907 | it->cg_link = NULL; |
| 2908 | return; |
| 2909 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2910 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2911 | cg = link->cg; |
| 2912 | } while (list_empty(&cg->tasks)); |
| 2913 | it->cg_link = l; |
| 2914 | it->task = cg->tasks.next; |
| 2915 | } |
| 2916 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2917 | /* |
| 2918 | * To reduce the fork() overhead for systems that are not actually |
| 2919 | * using their cgroups capability, we don't maintain the lists running |
| 2920 | * through each css_set to its tasks until we see the list actually |
| 2921 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2922 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2923 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2924 | { |
| 2925 | struct task_struct *p, *g; |
| 2926 | write_lock(&css_set_lock); |
| 2927 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2928 | /* |
| 2929 | * We need tasklist_lock because RCU is not safe against |
| 2930 | * while_each_thread(). Besides, a forking task that has passed |
| 2931 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2932 | * is not guaranteed to have its child immediately visible in the |
| 2933 | * tasklist if we walk through it with RCU. |
| 2934 | */ |
| 2935 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2936 | do_each_thread(g, p) { |
| 2937 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2938 | /* |
| 2939 | * We should check if the process is exiting, otherwise |
| 2940 | * it will race with cgroup_exit() in that the list |
| 2941 | * entry won't be deleted though the process has exited. |
| 2942 | */ |
| 2943 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2944 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2945 | task_unlock(p); |
| 2946 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2947 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2948 | write_unlock(&css_set_lock); |
| 2949 | } |
| 2950 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2951 | /** |
| 2952 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 2953 | * @pos: the current position (%NULL to initiate traversal) |
| 2954 | * @cgroup: cgroup whose descendants to walk |
| 2955 | * |
| 2956 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 2957 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
| 2958 | */ |
| 2959 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 2960 | struct cgroup *cgroup) |
| 2961 | { |
| 2962 | struct cgroup *next; |
| 2963 | |
| 2964 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2965 | |
| 2966 | /* if first iteration, pretend we just visited @cgroup */ |
| 2967 | if (!pos) { |
| 2968 | if (list_empty(&cgroup->children)) |
| 2969 | return NULL; |
| 2970 | pos = cgroup; |
| 2971 | } |
| 2972 | |
| 2973 | /* visit the first child if exists */ |
| 2974 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 2975 | if (next) |
| 2976 | return next; |
| 2977 | |
| 2978 | /* no child, visit my or the closest ancestor's next sibling */ |
| 2979 | do { |
| 2980 | next = list_entry_rcu(pos->sibling.next, struct cgroup, |
| 2981 | sibling); |
| 2982 | if (&next->sibling != &pos->parent->children) |
| 2983 | return next; |
| 2984 | |
| 2985 | pos = pos->parent; |
| 2986 | } while (pos != cgroup); |
| 2987 | |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 2991 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2992 | /** |
| 2993 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 2994 | * @pos: cgroup of interest |
| 2995 | * |
| 2996 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 2997 | * @pos is returned. This can be used during pre-order traversal to skip |
| 2998 | * subtree of @pos. |
| 2999 | */ |
| 3000 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3001 | { |
| 3002 | struct cgroup *last, *tmp; |
| 3003 | |
| 3004 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3005 | |
| 3006 | do { |
| 3007 | last = pos; |
| 3008 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3009 | pos = NULL; |
| 3010 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3011 | pos = tmp; |
| 3012 | } while (pos); |
| 3013 | |
| 3014 | return last; |
| 3015 | } |
| 3016 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3017 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3018 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3019 | { |
| 3020 | struct cgroup *last; |
| 3021 | |
| 3022 | do { |
| 3023 | last = pos; |
| 3024 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3025 | sibling); |
| 3026 | } while (pos); |
| 3027 | |
| 3028 | return last; |
| 3029 | } |
| 3030 | |
| 3031 | /** |
| 3032 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3033 | * @pos: the current position (%NULL to initiate traversal) |
| 3034 | * @cgroup: cgroup whose descendants to walk |
| 3035 | * |
| 3036 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3037 | * descendant to visit for post-order traversal of @cgroup's descendants. |
| 3038 | */ |
| 3039 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3040 | struct cgroup *cgroup) |
| 3041 | { |
| 3042 | struct cgroup *next; |
| 3043 | |
| 3044 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3045 | |
| 3046 | /* if first iteration, visit the leftmost descendant */ |
| 3047 | if (!pos) { |
| 3048 | next = cgroup_leftmost_descendant(cgroup); |
| 3049 | return next != cgroup ? next : NULL; |
| 3050 | } |
| 3051 | |
| 3052 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 3053 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3054 | if (&next->sibling != &pos->parent->children) |
| 3055 | return cgroup_leftmost_descendant(next); |
| 3056 | |
| 3057 | /* no sibling left, visit parent */ |
| 3058 | next = pos->parent; |
| 3059 | return next != cgroup ? next : NULL; |
| 3060 | } |
| 3061 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3062 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3063 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3064 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3065 | { |
| 3066 | /* |
| 3067 | * The first time anyone tries to iterate across a cgroup, |
| 3068 | * we need to enable the list linking each css_set to its |
| 3069 | * tasks, and fix up all existing tasks. |
| 3070 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3071 | if (!use_task_css_set_links) |
| 3072 | cgroup_enable_task_cg_lists(); |
| 3073 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3074 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3075 | it->cg_link = &cgrp->css_sets; |
| 3076 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3077 | } |
| 3078 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3079 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3080 | struct cgroup_iter *it) |
| 3081 | { |
| 3082 | struct task_struct *res; |
| 3083 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3084 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3085 | |
| 3086 | /* If the iterator cg is NULL, we have no tasks */ |
| 3087 | if (!it->cg_link) |
| 3088 | return NULL; |
| 3089 | res = list_entry(l, struct task_struct, cg_list); |
| 3090 | /* Advance iterator to find next entry */ |
| 3091 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3092 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 3093 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3094 | /* We reached the end of this task list - move on to |
| 3095 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3096 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3097 | } else { |
| 3098 | it->task = l; |
| 3099 | } |
| 3100 | return res; |
| 3101 | } |
| 3102 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3103 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3104 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3105 | { |
| 3106 | read_unlock(&css_set_lock); |
| 3107 | } |
| 3108 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3109 | static inline int started_after_time(struct task_struct *t1, |
| 3110 | struct timespec *time, |
| 3111 | struct task_struct *t2) |
| 3112 | { |
| 3113 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3114 | if (start_diff > 0) { |
| 3115 | return 1; |
| 3116 | } else if (start_diff < 0) { |
| 3117 | return 0; |
| 3118 | } else { |
| 3119 | /* |
| 3120 | * Arbitrarily, if two processes started at the same |
| 3121 | * time, we'll say that the lower pointer value |
| 3122 | * started first. Note that t2 may have exited by now |
| 3123 | * so this may not be a valid pointer any longer, but |
| 3124 | * that's fine - it still serves to distinguish |
| 3125 | * between two tasks started (effectively) simultaneously. |
| 3126 | */ |
| 3127 | return t1 > t2; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | /* |
| 3132 | * This function is a callback from heap_insert() and is used to order |
| 3133 | * the heap. |
| 3134 | * In this case we order the heap in descending task start time. |
| 3135 | */ |
| 3136 | static inline int started_after(void *p1, void *p2) |
| 3137 | { |
| 3138 | struct task_struct *t1 = p1; |
| 3139 | struct task_struct *t2 = p2; |
| 3140 | return started_after_time(t1, &t2->start_time, t2); |
| 3141 | } |
| 3142 | |
| 3143 | /** |
| 3144 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3145 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3146 | * |
| 3147 | * Arguments include pointers to callback functions test_task() and |
| 3148 | * process_task(). |
| 3149 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3150 | * and if it returns true, call process_task() for it also. |
| 3151 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3152 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3153 | * but does not lock css_set_lock for the call to process_task(). |
| 3154 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3155 | * creation. |
| 3156 | * It is guaranteed that process_task() will act on every task that |
| 3157 | * is a member of the cgroup for the duration of this call. This |
| 3158 | * function may or may not call process_task() for tasks that exit |
| 3159 | * or move to a different cgroup during the call, or are forked or |
| 3160 | * move into the cgroup during the call. |
| 3161 | * |
| 3162 | * Note that test_task() may be called with locks held, and may in some |
| 3163 | * situations be called multiple times for the same task, so it should |
| 3164 | * be cheap. |
| 3165 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3166 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3167 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3168 | * may cause this function to fail). |
| 3169 | */ |
| 3170 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3171 | { |
| 3172 | int retval, i; |
| 3173 | struct cgroup_iter it; |
| 3174 | struct task_struct *p, *dropped; |
| 3175 | /* Never dereference latest_task, since it's not refcounted */ |
| 3176 | struct task_struct *latest_task = NULL; |
| 3177 | struct ptr_heap tmp_heap; |
| 3178 | struct ptr_heap *heap; |
| 3179 | struct timespec latest_time = { 0, 0 }; |
| 3180 | |
| 3181 | if (scan->heap) { |
| 3182 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3183 | heap = scan->heap; |
| 3184 | heap->gt = &started_after; |
| 3185 | } else { |
| 3186 | /* We need to allocate our own heap memory */ |
| 3187 | heap = &tmp_heap; |
| 3188 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3189 | if (retval) |
| 3190 | /* cannot allocate the heap */ |
| 3191 | return retval; |
| 3192 | } |
| 3193 | |
| 3194 | again: |
| 3195 | /* |
| 3196 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3197 | * to determine which are of interest, and using the scanner's |
| 3198 | * "process_task" callback to process any of them that need an update. |
| 3199 | * Since we don't want to hold any locks during the task updates, |
| 3200 | * gather tasks to be processed in a heap structure. |
| 3201 | * The heap is sorted by descending task start time. |
| 3202 | * If the statically-sized heap fills up, we overflow tasks that |
| 3203 | * started later, and in future iterations only consider tasks that |
| 3204 | * started after the latest task in the previous pass. This |
| 3205 | * guarantees forward progress and that we don't miss any tasks. |
| 3206 | */ |
| 3207 | heap->size = 0; |
| 3208 | cgroup_iter_start(scan->cg, &it); |
| 3209 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3210 | /* |
| 3211 | * Only affect tasks that qualify per the caller's callback, |
| 3212 | * if he provided one |
| 3213 | */ |
| 3214 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3215 | continue; |
| 3216 | /* |
| 3217 | * Only process tasks that started after the last task |
| 3218 | * we processed |
| 3219 | */ |
| 3220 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3221 | continue; |
| 3222 | dropped = heap_insert(heap, p); |
| 3223 | if (dropped == NULL) { |
| 3224 | /* |
| 3225 | * The new task was inserted; the heap wasn't |
| 3226 | * previously full |
| 3227 | */ |
| 3228 | get_task_struct(p); |
| 3229 | } else if (dropped != p) { |
| 3230 | /* |
| 3231 | * The new task was inserted, and pushed out a |
| 3232 | * different task |
| 3233 | */ |
| 3234 | get_task_struct(p); |
| 3235 | put_task_struct(dropped); |
| 3236 | } |
| 3237 | /* |
| 3238 | * Else the new task was newer than anything already in |
| 3239 | * the heap and wasn't inserted |
| 3240 | */ |
| 3241 | } |
| 3242 | cgroup_iter_end(scan->cg, &it); |
| 3243 | |
| 3244 | if (heap->size) { |
| 3245 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3246 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3247 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3248 | latest_time = q->start_time; |
| 3249 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3250 | } |
| 3251 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3252 | scan->process_task(q, scan); |
| 3253 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3254 | } |
| 3255 | /* |
| 3256 | * If we had to process any tasks at all, scan again |
| 3257 | * in case some of them were in the middle of forking |
| 3258 | * children that didn't get processed. |
| 3259 | * Not the most efficient way to do it, but it avoids |
| 3260 | * having to take callback_mutex in the fork path |
| 3261 | */ |
| 3262 | goto again; |
| 3263 | } |
| 3264 | if (heap == &tmp_heap) |
| 3265 | heap_free(&tmp_heap); |
| 3266 | return 0; |
| 3267 | } |
| 3268 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3269 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3270 | struct cgroup_scanner *scan) |
| 3271 | { |
| 3272 | struct cgroup *new_cgroup = scan->data; |
| 3273 | |
| 3274 | cgroup_lock(); |
| 3275 | cgroup_attach_task(new_cgroup, task, false); |
| 3276 | cgroup_unlock(); |
| 3277 | } |
| 3278 | |
| 3279 | /** |
| 3280 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3281 | * @to: cgroup to which the tasks will be moved |
| 3282 | * @from: cgroup in which the tasks currently reside |
| 3283 | */ |
| 3284 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3285 | { |
| 3286 | struct cgroup_scanner scan; |
| 3287 | |
| 3288 | scan.cg = from; |
| 3289 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3290 | scan.process_task = cgroup_transfer_one_task; |
| 3291 | scan.heap = NULL; |
| 3292 | scan.data = to; |
| 3293 | |
| 3294 | return cgroup_scan_tasks(&scan); |
| 3295 | } |
| 3296 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3297 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3298 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3299 | * |
| 3300 | * Reading this file can return large amounts of data if a cgroup has |
| 3301 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3302 | * but we cannot guarantee that the information we produce is correct |
| 3303 | * unless we produce it entirely atomically. |
| 3304 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3305 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3306 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3307 | /* which pidlist file are we talking about? */ |
| 3308 | enum cgroup_filetype { |
| 3309 | CGROUP_FILE_PROCS, |
| 3310 | CGROUP_FILE_TASKS, |
| 3311 | }; |
| 3312 | |
| 3313 | /* |
| 3314 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3315 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3316 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3317 | * to the cgroup. |
| 3318 | */ |
| 3319 | struct cgroup_pidlist { |
| 3320 | /* |
| 3321 | * used to find which pidlist is wanted. doesn't change as long as |
| 3322 | * this particular list stays in the list. |
| 3323 | */ |
| 3324 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3325 | /* array of xids */ |
| 3326 | pid_t *list; |
| 3327 | /* how many elements the above list has */ |
| 3328 | int length; |
| 3329 | /* how many files are using the current array */ |
| 3330 | int use_count; |
| 3331 | /* each of these stored in a list by its cgroup */ |
| 3332 | struct list_head links; |
| 3333 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3334 | struct cgroup *owner; |
| 3335 | /* protects the other fields */ |
| 3336 | struct rw_semaphore mutex; |
| 3337 | }; |
| 3338 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3339 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3340 | * The following two functions "fix" the issue where there are more pids |
| 3341 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3342 | * TODO: replace with a kernel-wide solution to this problem |
| 3343 | */ |
| 3344 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3345 | static void *pidlist_allocate(int count) |
| 3346 | { |
| 3347 | if (PIDLIST_TOO_LARGE(count)) |
| 3348 | return vmalloc(count * sizeof(pid_t)); |
| 3349 | else |
| 3350 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3351 | } |
| 3352 | static void pidlist_free(void *p) |
| 3353 | { |
| 3354 | if (is_vmalloc_addr(p)) |
| 3355 | vfree(p); |
| 3356 | else |
| 3357 | kfree(p); |
| 3358 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3359 | |
| 3360 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3361 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3362 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3363 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3364 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3365 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3366 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3367 | |
| 3368 | /* |
| 3369 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3370 | * edge cases first; no work needs to be done for either |
| 3371 | */ |
| 3372 | if (length == 0 || length == 1) |
| 3373 | return length; |
| 3374 | /* src and dest walk down the list; dest counts unique elements */ |
| 3375 | for (src = 1; src < length; src++) { |
| 3376 | /* find next unique element */ |
| 3377 | while (list[src] == list[src-1]) { |
| 3378 | src++; |
| 3379 | if (src == length) |
| 3380 | goto after; |
| 3381 | } |
| 3382 | /* dest always points to where the next unique element goes */ |
| 3383 | list[dest] = list[src]; |
| 3384 | dest++; |
| 3385 | } |
| 3386 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3387 | return dest; |
| 3388 | } |
| 3389 | |
| 3390 | static int cmppid(const void *a, const void *b) |
| 3391 | { |
| 3392 | return *(pid_t *)a - *(pid_t *)b; |
| 3393 | } |
| 3394 | |
| 3395 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3396 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3397 | * returns with the lock on that pidlist already held, and takes care |
| 3398 | * of the use count, or returns NULL with no locks held if we're out of |
| 3399 | * memory. |
| 3400 | */ |
| 3401 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3402 | enum cgroup_filetype type) |
| 3403 | { |
| 3404 | struct cgroup_pidlist *l; |
| 3405 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3406 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3407 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3408 | /* |
| 3409 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3410 | * the last ref-holder is trying to remove l from the list at the same |
| 3411 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3412 | * list we find out from under us - compare release_pid_array(). |
| 3413 | */ |
| 3414 | mutex_lock(&cgrp->pidlist_mutex); |
| 3415 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3416 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3417 | /* make sure l doesn't vanish out from under us */ |
| 3418 | down_write(&l->mutex); |
| 3419 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3420 | return l; |
| 3421 | } |
| 3422 | } |
| 3423 | /* entry not found; create a new one */ |
| 3424 | l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 3425 | if (!l) { |
| 3426 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3427 | return l; |
| 3428 | } |
| 3429 | init_rwsem(&l->mutex); |
| 3430 | down_write(&l->mutex); |
| 3431 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3432 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3433 | l->use_count = 0; /* don't increment here */ |
| 3434 | l->list = NULL; |
| 3435 | l->owner = cgrp; |
| 3436 | list_add(&l->links, &cgrp->pidlists); |
| 3437 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3438 | return l; |
| 3439 | } |
| 3440 | |
| 3441 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3442 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3443 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3444 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3445 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3446 | { |
| 3447 | pid_t *array; |
| 3448 | int length; |
| 3449 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3450 | struct cgroup_iter it; |
| 3451 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3452 | struct cgroup_pidlist *l; |
| 3453 | |
| 3454 | /* |
| 3455 | * If cgroup gets more users after we read count, we won't have |
| 3456 | * enough space - tough. This race is indistinguishable to the |
| 3457 | * caller from the case that the additional cgroup users didn't |
| 3458 | * show up until sometime later on. |
| 3459 | */ |
| 3460 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3461 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3462 | if (!array) |
| 3463 | return -ENOMEM; |
| 3464 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3465 | cgroup_iter_start(cgrp, &it); |
| 3466 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3467 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3468 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3469 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3470 | if (type == CGROUP_FILE_PROCS) |
| 3471 | pid = task_tgid_vnr(tsk); |
| 3472 | else |
| 3473 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3474 | if (pid > 0) /* make sure to only use valid results */ |
| 3475 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3476 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3477 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3478 | length = n; |
| 3479 | /* now sort & (if procs) strip out duplicates */ |
| 3480 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3481 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3482 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3483 | l = cgroup_pidlist_find(cgrp, type); |
| 3484 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3485 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3486 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3487 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3488 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3489 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3490 | l->list = array; |
| 3491 | l->length = length; |
| 3492 | l->use_count++; |
| 3493 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3494 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3495 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3498 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3499 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3500 | * @stats: cgroupstats to fill information into |
| 3501 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3502 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3503 | * |
| 3504 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3505 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3506 | */ |
| 3507 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3508 | { |
| 3509 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3510 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3511 | struct cgroup_iter it; |
| 3512 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3513 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3514 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3515 | * Validate dentry by checking the superblock operations, |
| 3516 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3517 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3518 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3519 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3520 | goto err; |
| 3521 | |
| 3522 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3523 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3524 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3525 | cgroup_iter_start(cgrp, &it); |
| 3526 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3527 | switch (tsk->state) { |
| 3528 | case TASK_RUNNING: |
| 3529 | stats->nr_running++; |
| 3530 | break; |
| 3531 | case TASK_INTERRUPTIBLE: |
| 3532 | stats->nr_sleeping++; |
| 3533 | break; |
| 3534 | case TASK_UNINTERRUPTIBLE: |
| 3535 | stats->nr_uninterruptible++; |
| 3536 | break; |
| 3537 | case TASK_STOPPED: |
| 3538 | stats->nr_stopped++; |
| 3539 | break; |
| 3540 | default: |
| 3541 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3542 | stats->nr_io_wait++; |
| 3543 | break; |
| 3544 | } |
| 3545 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3546 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3547 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3548 | err: |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3552 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3553 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3554 | * 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] | 3555 | * 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] | 3556 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3557 | */ |
| 3558 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3559 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3560 | { |
| 3561 | /* |
| 3562 | * Initially we receive a position value that corresponds to |
| 3563 | * one more than the last pid shown (or 0 on the first call or |
| 3564 | * after a seek to the start). Use a binary-search to find the |
| 3565 | * next pid to display, if any |
| 3566 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3567 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3568 | int index = 0, pid = *pos; |
| 3569 | int *iter; |
| 3570 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3571 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3572 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3573 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3574 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3575 | while (index < end) { |
| 3576 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3577 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3578 | index = mid; |
| 3579 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3580 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3581 | index = mid + 1; |
| 3582 | else |
| 3583 | end = mid; |
| 3584 | } |
| 3585 | } |
| 3586 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3587 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3588 | return NULL; |
| 3589 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3590 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3591 | *pos = *iter; |
| 3592 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3595 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3596 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3597 | struct cgroup_pidlist *l = s->private; |
| 3598 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3599 | } |
| 3600 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3601 | 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] | 3602 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3603 | struct cgroup_pidlist *l = s->private; |
| 3604 | pid_t *p = v; |
| 3605 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3606 | /* |
| 3607 | * Advance to the next pid in the array. If this goes off the |
| 3608 | * end, we're done |
| 3609 | */ |
| 3610 | p++; |
| 3611 | if (p >= end) { |
| 3612 | return NULL; |
| 3613 | } else { |
| 3614 | *pos = *p; |
| 3615 | return p; |
| 3616 | } |
| 3617 | } |
| 3618 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3619 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3620 | { |
| 3621 | return seq_printf(s, "%d\n", *(int *)v); |
| 3622 | } |
| 3623 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3624 | /* |
| 3625 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3626 | * independent of whether it's tasks or procs |
| 3627 | */ |
| 3628 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3629 | .start = cgroup_pidlist_start, |
| 3630 | .stop = cgroup_pidlist_stop, |
| 3631 | .next = cgroup_pidlist_next, |
| 3632 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3633 | }; |
| 3634 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3635 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3636 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3637 | /* |
| 3638 | * the case where we're the last user of this particular pidlist will |
| 3639 | * have us remove it from the cgroup's list, which entails taking the |
| 3640 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3641 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3642 | */ |
| 3643 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3644 | down_write(&l->mutex); |
| 3645 | BUG_ON(!l->use_count); |
| 3646 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3647 | /* we're the last user if refcount is 0; remove and free */ |
| 3648 | list_del(&l->links); |
| 3649 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3650 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3651 | put_pid_ns(l->key.ns); |
| 3652 | up_write(&l->mutex); |
| 3653 | kfree(l); |
| 3654 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3655 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3656 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3657 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3658 | } |
| 3659 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3660 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3661 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3663 | if (!(file->f_mode & FMODE_READ)) |
| 3664 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | /* |
| 3666 | * the seq_file will only be initialized if the file was opened for |
| 3667 | * reading; hence we check if it's not null only in that case. |
| 3668 | */ |
| 3669 | l = ((struct seq_file *)file->private_data)->private; |
| 3670 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3671 | return seq_release(inode, file); |
| 3672 | } |
| 3673 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3674 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3675 | .read = seq_read, |
| 3676 | .llseek = seq_lseek, |
| 3677 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3678 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3679 | }; |
| 3680 | |
| 3681 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3682 | * The following functions handle opens on a file that displays a pidlist |
| 3683 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3684 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3685 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3686 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3687 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3688 | { |
| 3689 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3690 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3691 | int retval; |
| 3692 | |
| 3693 | /* Nothing to do for write-only files */ |
| 3694 | if (!(file->f_mode & FMODE_READ)) |
| 3695 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3696 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3697 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3698 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3699 | if (retval) |
| 3700 | return retval; |
| 3701 | /* configure file information */ |
| 3702 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3703 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3704 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3705 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3706 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3707 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3708 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3710 | return 0; |
| 3711 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3712 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3713 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3714 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3715 | } |
| 3716 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3717 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3718 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3719 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3720 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3721 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3722 | struct cftype *cft) |
| 3723 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3724 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3727 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3728 | struct cftype *cft, |
| 3729 | u64 val) |
| 3730 | { |
| 3731 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3732 | if (val) |
| 3733 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3734 | else |
| 3735 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3736 | return 0; |
| 3737 | } |
| 3738 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3739 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3740 | * Unregister event and free resources. |
| 3741 | * |
| 3742 | * Gets called from workqueue. |
| 3743 | */ |
| 3744 | static void cgroup_event_remove(struct work_struct *work) |
| 3745 | { |
| 3746 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3747 | remove); |
| 3748 | struct cgroup *cgrp = event->cgrp; |
| 3749 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3750 | remove_wait_queue(event->wqh, &event->wait); |
| 3751 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3752 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3753 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3754 | /* Notify userspace the event is going away. */ |
| 3755 | eventfd_signal(event->eventfd, 1); |
| 3756 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3757 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3758 | kfree(event); |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3759 | dput(cgrp->dentry); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | /* |
| 3763 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3764 | * |
| 3765 | * Called with wqh->lock held and interrupts disabled. |
| 3766 | */ |
| 3767 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3768 | int sync, void *key) |
| 3769 | { |
| 3770 | struct cgroup_event *event = container_of(wait, |
| 3771 | struct cgroup_event, wait); |
| 3772 | struct cgroup *cgrp = event->cgrp; |
| 3773 | unsigned long flags = (unsigned long)key; |
| 3774 | |
| 3775 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3776 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3777 | * If the event has been detached at cgroup removal, we |
| 3778 | * can simply return knowing the other side will cleanup |
| 3779 | * for us. |
| 3780 | * |
| 3781 | * We can't race against event freeing since the other |
| 3782 | * side will require wqh->lock via remove_wait_queue(), |
| 3783 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3784 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3785 | spin_lock(&cgrp->event_list_lock); |
| 3786 | if (!list_empty(&event->list)) { |
| 3787 | list_del_init(&event->list); |
| 3788 | /* |
| 3789 | * We are in atomic context, but cgroup_event_remove() |
| 3790 | * may sleep, so we have to call it in workqueue. |
| 3791 | */ |
| 3792 | schedule_work(&event->remove); |
| 3793 | } |
| 3794 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
| 3800 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3801 | wait_queue_head_t *wqh, poll_table *pt) |
| 3802 | { |
| 3803 | struct cgroup_event *event = container_of(pt, |
| 3804 | struct cgroup_event, pt); |
| 3805 | |
| 3806 | event->wqh = wqh; |
| 3807 | add_wait_queue(wqh, &event->wait); |
| 3808 | } |
| 3809 | |
| 3810 | /* |
| 3811 | * Parse input and register new cgroup event handler. |
| 3812 | * |
| 3813 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3814 | * Interpretation of args is defined by control file implementation. |
| 3815 | */ |
| 3816 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3817 | const char *buffer) |
| 3818 | { |
| 3819 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3820 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3821 | unsigned int efd, cfd; |
| 3822 | struct file *efile = NULL; |
| 3823 | struct file *cfile = NULL; |
| 3824 | char *endp; |
| 3825 | int ret; |
| 3826 | |
| 3827 | efd = simple_strtoul(buffer, &endp, 10); |
| 3828 | if (*endp != ' ') |
| 3829 | return -EINVAL; |
| 3830 | buffer = endp + 1; |
| 3831 | |
| 3832 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3833 | if ((*endp != ' ') && (*endp != '\0')) |
| 3834 | return -EINVAL; |
| 3835 | buffer = endp + 1; |
| 3836 | |
| 3837 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3838 | if (!event) |
| 3839 | return -ENOMEM; |
| 3840 | event->cgrp = cgrp; |
| 3841 | INIT_LIST_HEAD(&event->list); |
| 3842 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3843 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3844 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3845 | |
| 3846 | efile = eventfd_fget(efd); |
| 3847 | if (IS_ERR(efile)) { |
| 3848 | ret = PTR_ERR(efile); |
| 3849 | goto fail; |
| 3850 | } |
| 3851 | |
| 3852 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3853 | if (IS_ERR(event->eventfd)) { |
| 3854 | ret = PTR_ERR(event->eventfd); |
| 3855 | goto fail; |
| 3856 | } |
| 3857 | |
| 3858 | cfile = fget(cfd); |
| 3859 | if (!cfile) { |
| 3860 | ret = -EBADF; |
| 3861 | goto fail; |
| 3862 | } |
| 3863 | |
| 3864 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3865 | /* 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] | 3866 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3867 | if (ret < 0) |
| 3868 | goto fail; |
| 3869 | |
| 3870 | event->cft = __file_cft(cfile); |
| 3871 | if (IS_ERR(event->cft)) { |
| 3872 | ret = PTR_ERR(event->cft); |
| 3873 | goto fail; |
| 3874 | } |
| 3875 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3876 | /* |
| 3877 | * The file to be monitored must be in the same cgroup as |
| 3878 | * cgroup.event_control is. |
| 3879 | */ |
| 3880 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3881 | if (cgrp_cfile != cgrp) { |
| 3882 | ret = -EINVAL; |
| 3883 | goto fail; |
| 3884 | } |
| 3885 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3886 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3887 | ret = -EINVAL; |
| 3888 | goto fail; |
| 3889 | } |
| 3890 | |
| 3891 | ret = event->cft->register_event(cgrp, event->cft, |
| 3892 | event->eventfd, buffer); |
| 3893 | if (ret) |
| 3894 | goto fail; |
| 3895 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3896 | /* |
| 3897 | * Events should be removed after rmdir of cgroup directory, but before |
| 3898 | * destroying subsystem state objects. Let's take reference to cgroup |
| 3899 | * directory dentry to do that. |
| 3900 | */ |
| 3901 | dget(cgrp->dentry); |
| 3902 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3903 | spin_lock(&cgrp->event_list_lock); |
| 3904 | list_add(&event->list, &cgrp->event_list); |
| 3905 | spin_unlock(&cgrp->event_list_lock); |
| 3906 | |
| 3907 | fput(cfile); |
| 3908 | fput(efile); |
| 3909 | |
| 3910 | return 0; |
| 3911 | |
| 3912 | fail: |
| 3913 | if (cfile) |
| 3914 | fput(cfile); |
| 3915 | |
| 3916 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 3917 | eventfd_ctx_put(event->eventfd); |
| 3918 | |
| 3919 | if (!IS_ERR_OR_NULL(efile)) |
| 3920 | fput(efile); |
| 3921 | |
| 3922 | kfree(event); |
| 3923 | |
| 3924 | return ret; |
| 3925 | } |
| 3926 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3927 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 3928 | struct cftype *cft) |
| 3929 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3930 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 3934 | struct cftype *cft, |
| 3935 | u64 val) |
| 3936 | { |
| 3937 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3938 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3939 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3940 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3941 | return 0; |
| 3942 | } |
| 3943 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3944 | /* |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3945 | * for the common functions, 'private' gives the type of file |
| 3946 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3947 | /* for hysterical raisins, we can't put this on the older files */ |
| 3948 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3949 | static struct cftype files[] = { |
| 3950 | { |
| 3951 | .name = "tasks", |
| 3952 | .open = cgroup_tasks_open, |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3953 | .write_u64 = cgroup_tasks_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3954 | .release = cgroup_pidlist_release, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 3955 | .mode = S_IRUGO | S_IWUSR, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3956 | }, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3957 | { |
| 3958 | .name = CGROUP_FILE_GENERIC_PREFIX "procs", |
| 3959 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3960 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3961 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3962 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3963 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3964 | { |
| 3965 | .name = "notify_on_release", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 3966 | .read_u64 = cgroup_read_notify_on_release, |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3967 | .write_u64 = cgroup_write_notify_on_release, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3968 | }, |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3969 | { |
| 3970 | .name = CGROUP_FILE_GENERIC_PREFIX "event_control", |
| 3971 | .write_string = cgroup_write_event_control, |
| 3972 | .mode = S_IWUGO, |
| 3973 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3974 | { |
| 3975 | .name = "cgroup.clone_children", |
| 3976 | .read_u64 = cgroup_clone_children_read, |
| 3977 | .write_u64 = cgroup_clone_children_write, |
| 3978 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3979 | { |
| 3980 | .name = "release_agent", |
| 3981 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 3982 | .read_seq_string = cgroup_release_agent_show, |
| 3983 | .write_string = cgroup_release_agent_write, |
| 3984 | .max_write_len = PATH_MAX, |
| 3985 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3986 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3987 | }; |
| 3988 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3989 | /** |
| 3990 | * cgroup_populate_dir - selectively creation of files in a directory |
| 3991 | * @cgrp: target cgroup |
| 3992 | * @base_files: true if the base files should be added |
| 3993 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 3994 | */ |
| 3995 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 3996 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3997 | { |
| 3998 | int err; |
| 3999 | struct cgroup_subsys *ss; |
| 4000 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4001 | if (base_files) { |
| 4002 | err = cgroup_addrm_files(cgrp, NULL, files, true); |
| 4003 | if (err < 0) |
| 4004 | return err; |
| 4005 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4006 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4007 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4008 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4009 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4010 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4011 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4012 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4013 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4014 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4015 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4016 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4017 | /* This cgroup is ready now */ |
| 4018 | for_each_subsys(cgrp->root, ss) { |
| 4019 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4020 | /* |
| 4021 | * Update id->css pointer and make this css visible from |
| 4022 | * CSS ID functions. This pointer will be dereferened |
| 4023 | * from RCU-read-side without locks. |
| 4024 | */ |
| 4025 | if (css->id) |
| 4026 | rcu_assign_pointer(css->id->css, css); |
| 4027 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4028 | |
| 4029 | return 0; |
| 4030 | } |
| 4031 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4032 | static void css_dput_fn(struct work_struct *work) |
| 4033 | { |
| 4034 | struct cgroup_subsys_state *css = |
| 4035 | container_of(work, struct cgroup_subsys_state, dput_work); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4036 | struct dentry *dentry = css->cgroup->dentry; |
| 4037 | struct super_block *sb = dentry->d_sb; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4038 | |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4039 | atomic_inc(&sb->s_active); |
| 4040 | dput(dentry); |
| 4041 | deactivate_super(sb); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4044 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4045 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4046 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4047 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4048 | css->cgroup = cgrp; |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 4049 | atomic_set(&css->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4050 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4051 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4052 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4053 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4054 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4055 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4056 | |
| 4057 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4058 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4059 | * css_put(). dput() requires process context, which css_put() may |
| 4060 | * be called without. @css->dput_work will be used to invoke |
| 4061 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4062 | */ |
| 4063 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4066 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4067 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4068 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4069 | int ret = 0; |
| 4070 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4071 | lockdep_assert_held(&cgroup_mutex); |
| 4072 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4073 | if (ss->css_online) |
| 4074 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4075 | if (!ret) |
| 4076 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4077 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4081 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4082 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4083 | { |
| 4084 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4085 | |
| 4086 | lockdep_assert_held(&cgroup_mutex); |
| 4087 | |
| 4088 | if (!(css->flags & CSS_ONLINE)) |
| 4089 | return; |
| 4090 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4091 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4092 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4093 | |
| 4094 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4095 | } |
| 4096 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4097 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4098 | * cgroup_create - create a cgroup |
| 4099 | * @parent: cgroup that will be parent of the new cgroup |
| 4100 | * @dentry: dentry of the new cgroup |
| 4101 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4102 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4103 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4104 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4105 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4106 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4107 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4108 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4109 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4110 | struct cgroupfs_root *root = parent->root; |
| 4111 | int err = 0; |
| 4112 | struct cgroup_subsys *ss; |
| 4113 | struct super_block *sb = root->sb; |
| 4114 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4115 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4116 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4117 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4118 | return -ENOMEM; |
| 4119 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4120 | name = cgroup_alloc_name(dentry); |
| 4121 | if (!name) |
| 4122 | goto err_free_cgrp; |
| 4123 | rcu_assign_pointer(cgrp->name, name); |
| 4124 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4125 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4126 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4127 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4128 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4129 | /* |
| 4130 | * Only live parents can have children. Note that the liveliness |
| 4131 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4132 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4133 | * anyway so that locking is contained inside cgroup proper and we |
| 4134 | * don't get nasty surprises if we ever grow another caller. |
| 4135 | */ |
| 4136 | if (!cgroup_lock_live_group(parent)) { |
| 4137 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4138 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4139 | } |
| 4140 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4141 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4142 | * get deleted on unmount if there are child cgroups. This |
| 4143 | * can be done outside cgroup_mutex, since the sb can't |
| 4144 | * disappear while someone has an open control file on the |
| 4145 | * fs */ |
| 4146 | atomic_inc(&sb->s_active); |
| 4147 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4148 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4149 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4150 | dentry->d_fsdata = cgrp; |
| 4151 | cgrp->dentry = dentry; |
| 4152 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4153 | cgrp->parent = parent; |
| 4154 | cgrp->root = parent->root; |
| 4155 | cgrp->top_cgroup = parent->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4156 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4157 | if (notify_on_release(parent)) |
| 4158 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4159 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4160 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4161 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4162 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4163 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4164 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4165 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4166 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4167 | if (IS_ERR(css)) { |
| 4168 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4169 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4170 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4171 | init_cgroup_css(css, ss, cgrp); |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4172 | if (ss->use_id) { |
| 4173 | err = alloc_css_id(ss, parent, cgrp); |
| 4174 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4175 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4176 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4179 | /* |
| 4180 | * Create directory. cgroup_create_file() returns with the new |
| 4181 | * directory locked on success so that it can be populated without |
| 4182 | * dropping cgroup_mutex. |
| 4183 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4184 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4185 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4186 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4187 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4188 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4189 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4190 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4191 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4192 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4193 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4194 | /* each css holds a ref to the cgroup's dentry */ |
| 4195 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4196 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4197 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4198 | /* creation succeeded, notify subsystems */ |
| 4199 | for_each_subsys(root, ss) { |
| 4200 | err = online_css(ss, cgrp); |
| 4201 | if (err) |
| 4202 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4203 | |
| 4204 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4205 | parent->parent) { |
| 4206 | 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", |
| 4207 | current->comm, current->pid, ss->name); |
| 4208 | if (!strcmp(ss->name, "memory")) |
| 4209 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4210 | ss->warned_broken_hierarchy = true; |
| 4211 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4212 | } |
| 4213 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4214 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4215 | if (err) |
| 4216 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4217 | |
| 4218 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4219 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4220 | |
| 4221 | return 0; |
| 4222 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4223 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4224 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4225 | if (cgrp->subsys[ss->subsys_id]) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4226 | ss->css_free(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4227 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4228 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4229 | /* Release the reference count that we took on the superblock */ |
| 4230 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4231 | err_free_id: |
| 4232 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4233 | err_free_name: |
| 4234 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4235 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4236 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4237 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4238 | |
| 4239 | err_destroy: |
| 4240 | cgroup_destroy_locked(cgrp); |
| 4241 | mutex_unlock(&cgroup_mutex); |
| 4242 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4243 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4246 | 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] | 4247 | { |
| 4248 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4249 | |
| 4250 | /* the vfs holds inode->i_mutex already */ |
| 4251 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4252 | } |
| 4253 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4254 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4255 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4256 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4257 | struct dentry *d = cgrp->dentry; |
| 4258 | struct cgroup *parent = cgrp->parent; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4259 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4260 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4261 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4262 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4263 | lockdep_assert_held(&cgroup_mutex); |
| 4264 | |
| 4265 | if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4266 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4267 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4268 | /* |
| 4269 | * Block new css_tryget() by deactivating refcnt and mark @cgrp |
| 4270 | * removed. This makes future css_tryget() and child creation |
| 4271 | * attempts fail thus maintaining the removal conditions verified |
| 4272 | * above. |
| 4273 | */ |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4274 | for_each_subsys(cgrp->root, ss) { |
| 4275 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4276 | |
| 4277 | WARN_ON(atomic_read(&css->refcnt) < 0); |
| 4278 | atomic_add(CSS_DEACT_BIAS, &css->refcnt); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4279 | } |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4280 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 4281 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4282 | /* tell subsystems to initate destruction */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4283 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4284 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4285 | |
| 4286 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4287 | * Put all the base refs. Each css holds an extra reference to the |
| 4288 | * cgroup's dentry and cgroup removal proceeds regardless of css |
| 4289 | * refs. On the last put of each css, whenever that may be, the |
| 4290 | * extra dentry ref is put so that dentry destruction happens only |
| 4291 | * after all css's are released. |
| 4292 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4293 | for_each_subsys(cgrp->root, ss) |
| 4294 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4295 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4296 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4297 | if (!list_empty(&cgrp->release_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4298 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4299 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4300 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4301 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4302 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4303 | list_del_init(&cgrp->allcg_node); |
| 4304 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4305 | dget(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4306 | cgroup_d_remove_dir(d); |
| 4307 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4308 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4309 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4310 | check_for_release(parent); |
| 4311 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4312 | /* |
| 4313 | * Unregister events and notify userspace. |
| 4314 | * Notify userspace about cgroup removing only after rmdir of cgroup |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4315 | * directory to avoid race between userspace and kernelspace. |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4316 | */ |
| 4317 | spin_lock(&cgrp->event_list_lock); |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4318 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 4319 | list_del_init(&event->list); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4320 | schedule_work(&event->remove); |
| 4321 | } |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4322 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4323 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4324 | return 0; |
| 4325 | } |
| 4326 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4327 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4328 | { |
| 4329 | int ret; |
| 4330 | |
| 4331 | mutex_lock(&cgroup_mutex); |
| 4332 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4333 | mutex_unlock(&cgroup_mutex); |
| 4334 | |
| 4335 | return ret; |
| 4336 | } |
| 4337 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4338 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4339 | { |
| 4340 | INIT_LIST_HEAD(&ss->cftsets); |
| 4341 | |
| 4342 | /* |
| 4343 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4344 | * deregistration. |
| 4345 | */ |
| 4346 | if (ss->base_cftypes) { |
| 4347 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4348 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4349 | } |
| 4350 | } |
| 4351 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4352 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4353 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4354 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4355 | |
| 4356 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4357 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4358 | mutex_lock(&cgroup_mutex); |
| 4359 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4360 | /* init base cftset */ |
| 4361 | cgroup_init_cftsets(ss); |
| 4362 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4363 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4364 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4365 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4366 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4367 | /* We don't handle early failures gracefully */ |
| 4368 | BUG_ON(IS_ERR(css)); |
| 4369 | init_cgroup_css(css, ss, dummytop); |
| 4370 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4371 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4372 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4373 | * newly registered, all tasks and hence the |
| 4374 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4375 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4376 | |
| 4377 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4378 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4379 | /* At system boot, before all subsystems have been |
| 4380 | * registered, no tasks have been forked, so we don't |
| 4381 | * need to invoke fork callbacks here. */ |
| 4382 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4383 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4384 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4385 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4386 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4387 | mutex_unlock(&cgroup_mutex); |
| 4388 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4389 | /* this function shouldn't be used with modular subsystems, since they |
| 4390 | * need to register a subsys_id, among other things */ |
| 4391 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4395 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4396 | * @ss: the subsystem to load |
| 4397 | * |
| 4398 | * 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] | 4399 | * 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] | 4400 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4401 | * simpler cgroup_init_subsys. |
| 4402 | */ |
| 4403 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4404 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4405 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4406 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4407 | struct hlist_node *tmp; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4408 | struct css_set *cg; |
| 4409 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4410 | |
| 4411 | /* check name and function validity */ |
| 4412 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4413 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4414 | return -EINVAL; |
| 4415 | |
| 4416 | /* |
| 4417 | * we don't support callbacks in modular subsystems. this check is |
| 4418 | * before the ss->module check for consistency; a subsystem that could |
| 4419 | * be a module should still have no callbacks even if the user isn't |
| 4420 | * compiling it as one. |
| 4421 | */ |
| 4422 | if (ss->fork || ss->exit) |
| 4423 | return -EINVAL; |
| 4424 | |
| 4425 | /* |
| 4426 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4427 | * since cgroup_init_subsys will have already taken care of it. |
| 4428 | */ |
| 4429 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4430 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4431 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4432 | return 0; |
| 4433 | } |
| 4434 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4435 | /* init base cftset */ |
| 4436 | cgroup_init_cftsets(ss); |
| 4437 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4438 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4439 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4440 | |
| 4441 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4442 | * no ss->css_alloc seems to need anything important in the ss |
| 4443 | * struct, so this can happen first (i.e. before the rootnode |
| 4444 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4445 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4446 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4447 | if (IS_ERR(css)) { |
| 4448 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4449 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4450 | mutex_unlock(&cgroup_mutex); |
| 4451 | return PTR_ERR(css); |
| 4452 | } |
| 4453 | |
| 4454 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4455 | ss->root = &rootnode; |
| 4456 | |
| 4457 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4458 | init_cgroup_css(css, ss, dummytop); |
| 4459 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4460 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4461 | ret = cgroup_init_idr(ss, css); |
| 4462 | if (ret) |
| 4463 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | /* |
| 4467 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4468 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4469 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4470 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4471 | * table state, so each changed css_set will need its hash recomputed. |
| 4472 | * this is all done under the css_set_lock. |
| 4473 | */ |
| 4474 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4475 | hash_for_each_safe(css_set_table, i, tmp, cg, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4476 | /* skip entries that we already rehashed */ |
| 4477 | if (cg->subsys[ss->subsys_id]) |
| 4478 | continue; |
| 4479 | /* remove existing entry */ |
| 4480 | hash_del(&cg->hlist); |
| 4481 | /* set new value */ |
| 4482 | cg->subsys[ss->subsys_id] = css; |
| 4483 | /* recompute hash and restore entry */ |
| 4484 | key = css_set_hash(cg->subsys); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4485 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4486 | } |
| 4487 | write_unlock(&css_set_lock); |
| 4488 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4489 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4490 | ret = online_css(ss, dummytop); |
| 4491 | if (ret) |
| 4492 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4493 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4494 | /* success! */ |
| 4495 | mutex_unlock(&cgroup_mutex); |
| 4496 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4497 | |
| 4498 | err_unload: |
| 4499 | mutex_unlock(&cgroup_mutex); |
| 4500 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4501 | cgroup_unload_subsys(ss); |
| 4502 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4503 | } |
| 4504 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4505 | |
| 4506 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4507 | * cgroup_unload_subsys: unload a modular subsystem |
| 4508 | * @ss: the subsystem to unload |
| 4509 | * |
| 4510 | * This function should be called in a modular subsystem's exitcall. When this |
| 4511 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4512 | * the subsystem will not be attached to any hierarchy. |
| 4513 | */ |
| 4514 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4515 | { |
| 4516 | struct cg_cgroup_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4517 | |
| 4518 | BUG_ON(ss->module == NULL); |
| 4519 | |
| 4520 | /* |
| 4521 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4522 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4523 | * doesn't start being used while we're killing it off. |
| 4524 | */ |
| 4525 | BUG_ON(ss->root != &rootnode); |
| 4526 | |
| 4527 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4528 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4529 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4530 | ss->active = 0; |
| 4531 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4532 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4533 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4534 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4535 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4536 | subsys[ss->subsys_id] = NULL; |
| 4537 | |
| 4538 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4539 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4540 | |
| 4541 | /* |
| 4542 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4543 | * in loading, we need to pay our respects to the hashtable gods. |
| 4544 | */ |
| 4545 | write_lock(&css_set_lock); |
| 4546 | list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { |
| 4547 | struct css_set *cg = link->cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4548 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4549 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4550 | hash_del(&cg->hlist); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4551 | cg->subsys[ss->subsys_id] = NULL; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4552 | key = css_set_hash(cg->subsys); |
| 4553 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4554 | } |
| 4555 | write_unlock(&css_set_lock); |
| 4556 | |
| 4557 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4558 | * remove subsystem's css from the dummytop and free it - need to |
| 4559 | * free before marking as null because ss->css_free needs the |
| 4560 | * cgrp->subsys pointer to find their state. note that this also |
| 4561 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4562 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4563 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4564 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4565 | |
| 4566 | mutex_unlock(&cgroup_mutex); |
| 4567 | } |
| 4568 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4569 | |
| 4570 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4571 | * cgroup_init_early - cgroup initialization at system boot |
| 4572 | * |
| 4573 | * Initialize cgroups at system boot, and initialize any |
| 4574 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4575 | */ |
| 4576 | int __init cgroup_init_early(void) |
| 4577 | { |
| 4578 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4579 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4580 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 4581 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4582 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4583 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4584 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4585 | root_count = 1; |
| 4586 | init_task.cgroups = &init_css_set; |
| 4587 | |
| 4588 | init_css_set_link.cg = &init_css_set; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4589 | init_css_set_link.cgrp = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4590 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4591 | &rootnode.top_cgroup.css_sets); |
| 4592 | list_add(&init_css_set_link.cg_link_list, |
| 4593 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4594 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4595 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4596 | struct cgroup_subsys *ss = subsys[i]; |
| 4597 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4598 | /* at bootup time, we don't worry about modular subsystems */ |
| 4599 | if (!ss || ss->module) |
| 4600 | continue; |
| 4601 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4602 | BUG_ON(!ss->name); |
| 4603 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4604 | BUG_ON(!ss->css_alloc); |
| 4605 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4606 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4607 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4608 | ss->name, ss->subsys_id); |
| 4609 | BUG(); |
| 4610 | } |
| 4611 | |
| 4612 | if (ss->early_init) |
| 4613 | cgroup_init_subsys(ss); |
| 4614 | } |
| 4615 | return 0; |
| 4616 | } |
| 4617 | |
| 4618 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4619 | * cgroup_init - cgroup initialization |
| 4620 | * |
| 4621 | * Register cgroup filesystem and /proc file, and initialize |
| 4622 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4623 | */ |
| 4624 | int __init cgroup_init(void) |
| 4625 | { |
| 4626 | int err; |
| 4627 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4628 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4629 | |
| 4630 | err = bdi_init(&cgroup_backing_dev_info); |
| 4631 | if (err) |
| 4632 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4633 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4634 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4635 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4636 | |
| 4637 | /* at bootup time, we don't worry about modular subsystems */ |
| 4638 | if (!ss || ss->module) |
| 4639 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4640 | if (!ss->early_init) |
| 4641 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4642 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4643 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4644 | } |
| 4645 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4646 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4647 | key = css_set_hash(init_css_set.subsys); |
| 4648 | hash_add(css_set_table, &init_css_set.hlist, key); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4649 | BUG_ON(!init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4650 | |
| 4651 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4652 | if (!cgroup_kobj) { |
| 4653 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4654 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | err = register_filesystem(&cgroup_fs_type); |
| 4658 | if (err < 0) { |
| 4659 | kobject_put(cgroup_kobj); |
| 4660 | goto out; |
| 4661 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4662 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4663 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4664 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4665 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4666 | if (err) |
| 4667 | bdi_destroy(&cgroup_backing_dev_info); |
| 4668 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4669 | return err; |
| 4670 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4671 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4672 | /* |
| 4673 | * proc_cgroup_show() |
| 4674 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4675 | * - Used for /proc/<pid>/cgroup. |
| 4676 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4677 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4678 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4679 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4680 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4681 | * cgroup to top_cgroup. |
| 4682 | */ |
| 4683 | |
| 4684 | /* TODO: Use a proper seq_file iterator */ |
| 4685 | static int proc_cgroup_show(struct seq_file *m, void *v) |
| 4686 | { |
| 4687 | struct pid *pid; |
| 4688 | struct task_struct *tsk; |
| 4689 | char *buf; |
| 4690 | int retval; |
| 4691 | struct cgroupfs_root *root; |
| 4692 | |
| 4693 | retval = -ENOMEM; |
| 4694 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4695 | if (!buf) |
| 4696 | goto out; |
| 4697 | |
| 4698 | retval = -ESRCH; |
| 4699 | pid = m->private; |
| 4700 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4701 | if (!tsk) |
| 4702 | goto out_free; |
| 4703 | |
| 4704 | retval = 0; |
| 4705 | |
| 4706 | mutex_lock(&cgroup_mutex); |
| 4707 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4708 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4709 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4710 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4711 | int count = 0; |
| 4712 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4713 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4714 | for_each_subsys(root, ss) |
| 4715 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4716 | if (strlen(root->name)) |
| 4717 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4718 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4719 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4720 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4721 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4722 | if (retval < 0) |
| 4723 | goto out_unlock; |
| 4724 | seq_puts(m, buf); |
| 4725 | seq_putc(m, '\n'); |
| 4726 | } |
| 4727 | |
| 4728 | out_unlock: |
| 4729 | mutex_unlock(&cgroup_mutex); |
| 4730 | put_task_struct(tsk); |
| 4731 | out_free: |
| 4732 | kfree(buf); |
| 4733 | out: |
| 4734 | return retval; |
| 4735 | } |
| 4736 | |
| 4737 | static int cgroup_open(struct inode *inode, struct file *file) |
| 4738 | { |
| 4739 | struct pid *pid = PROC_I(inode)->pid; |
| 4740 | return single_open(file, proc_cgroup_show, pid); |
| 4741 | } |
| 4742 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4743 | const struct file_operations proc_cgroup_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4744 | .open = cgroup_open, |
| 4745 | .read = seq_read, |
| 4746 | .llseek = seq_lseek, |
| 4747 | .release = single_release, |
| 4748 | }; |
| 4749 | |
| 4750 | /* Display information about each subsystem and each hierarchy */ |
| 4751 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4752 | { |
| 4753 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4754 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4755 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4756 | /* |
| 4757 | * ideally we don't want subsystems moving around while we do this. |
| 4758 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4759 | * subsys/hierarchy state. |
| 4760 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4761 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4762 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4763 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4764 | if (ss == NULL) |
| 4765 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4766 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4767 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4768 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4769 | } |
| 4770 | mutex_unlock(&cgroup_mutex); |
| 4771 | return 0; |
| 4772 | } |
| 4773 | |
| 4774 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4775 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4776 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4779 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4780 | .open = cgroupstats_open, |
| 4781 | .read = seq_read, |
| 4782 | .llseek = seq_lseek, |
| 4783 | .release = single_release, |
| 4784 | }; |
| 4785 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4786 | /** |
| 4787 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4788 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4789 | * |
| 4790 | * Description: A task inherits its parent's cgroup at fork(). |
| 4791 | * |
| 4792 | * A pointer to the shared css_set was automatically copied in |
| 4793 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4794 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4795 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4796 | * have already changed current->cgroups, allowing the previously |
| 4797 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4798 | * |
| 4799 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4800 | * task, and the passed argument 'child' points to the child task. |
| 4801 | */ |
| 4802 | void cgroup_fork(struct task_struct *child) |
| 4803 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4804 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4805 | child->cgroups = current->cgroups; |
| 4806 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4807 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4808 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4812 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4813 | * @child: the task in question |
| 4814 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4815 | * Adds the task to the list running through its css_set if necessary and |
| 4816 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4817 | * visible on the task list in case we race with the first call to |
| 4818 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 4819 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4820 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4821 | void cgroup_post_fork(struct task_struct *child) |
| 4822 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4823 | int i; |
| 4824 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4825 | /* |
| 4826 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4827 | * under the tasklist_lock and we read it here after we added the child |
| 4828 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4829 | * yet in the tasklist when we walked through it from |
| 4830 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4831 | * should be visible now due to the paired locking and barriers implied |
| 4832 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4833 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4834 | * lock on fork. |
| 4835 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4836 | if (use_task_css_set_links) { |
| 4837 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4838 | task_lock(child); |
| 4839 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4840 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4841 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4842 | write_unlock(&css_set_lock); |
| 4843 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4844 | |
| 4845 | /* |
| 4846 | * Call ss->fork(). This must happen after @child is linked on |
| 4847 | * css_set; otherwise, @child might change state between ->fork() |
| 4848 | * and addition to css_set. |
| 4849 | */ |
| 4850 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 4851 | /* |
| 4852 | * fork/exit callbacks are supported only for builtin |
| 4853 | * subsystems, and the builtin section of the subsys |
| 4854 | * array is immutable, so we don't need to lock the |
| 4855 | * subsys array here. On the other hand, modular section |
| 4856 | * of the array can be freed at module unload, so we |
| 4857 | * can't touch that. |
| 4858 | */ |
| 4859 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4860 | struct cgroup_subsys *ss = subsys[i]; |
| 4861 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4862 | if (ss->fork) |
| 4863 | ss->fork(child); |
| 4864 | } |
| 4865 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4866 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4867 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4868 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4869 | * cgroup_exit - detach cgroup from exiting task |
| 4870 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4871 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4872 | * |
| 4873 | * Description: Detach cgroup from @tsk and release it. |
| 4874 | * |
| 4875 | * Note that cgroups marked notify_on_release force every task in |
| 4876 | * them to take the global cgroup_mutex mutex when exiting. |
| 4877 | * This could impact scaling on very large systems. Be reluctant to |
| 4878 | * use notify_on_release cgroups where very high task exit scaling |
| 4879 | * is required on large systems. |
| 4880 | * |
| 4881 | * the_top_cgroup_hack: |
| 4882 | * |
| 4883 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4884 | * |
| 4885 | * We call cgroup_exit() while the task is still competent to |
| 4886 | * handle notify_on_release(), then leave the task attached to the |
| 4887 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4888 | * |
| 4889 | * To do this properly, we would increment the reference count on |
| 4890 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4891 | * code we would add a second cgroup function call, to drop that |
| 4892 | * reference. This would just create an unnecessary hot spot on |
| 4893 | * the top_cgroup reference count, to no avail. |
| 4894 | * |
| 4895 | * Normally, holding a reference to a cgroup without bumping its |
| 4896 | * count is unsafe. The cgroup could go away, or someone could |
| 4897 | * attach us to a different cgroup, decrementing the count on |
| 4898 | * the first cgroup that we never incremented. But in this case, |
| 4899 | * 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] | 4900 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4901 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4902 | */ |
| 4903 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4904 | { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4905 | struct css_set *cg; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4906 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4907 | |
| 4908 | /* |
| 4909 | * Unlink from the css_set task list if necessary. |
| 4910 | * Optimistically check cg_list before taking |
| 4911 | * css_set_lock |
| 4912 | */ |
| 4913 | if (!list_empty(&tsk->cg_list)) { |
| 4914 | write_lock(&css_set_lock); |
| 4915 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4916 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4917 | write_unlock(&css_set_lock); |
| 4918 | } |
| 4919 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4920 | /* Reassign the task to the init_css_set. */ |
| 4921 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4922 | cg = tsk->cgroups; |
| 4923 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4924 | |
| 4925 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 4926 | /* |
| 4927 | * fork/exit callbacks are supported only for builtin |
| 4928 | * subsystems, see cgroup_post_fork() for details. |
| 4929 | */ |
| 4930 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4931 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4932 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4933 | if (ss->exit) { |
| 4934 | struct cgroup *old_cgrp = |
| 4935 | rcu_dereference_raw(cg->subsys[i])->cgroup; |
| 4936 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4937 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4941 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4942 | |
Li Zefan | b5d646f | 2013-01-24 14:43:51 +0800 | [diff] [blame] | 4943 | put_css_set_taskexit(cg); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4944 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4945 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4946 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4947 | { |
| 4948 | /* All of these checks rely on RCU to keep the cgroup |
| 4949 | * structure alive */ |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4950 | if (cgroup_is_releasable(cgrp) && |
| 4951 | !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) { |
| 4952 | /* |
| 4953 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4954 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4955 | * it now |
| 4956 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4957 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4958 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4959 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4960 | if (!cgroup_is_removed(cgrp) && |
| 4961 | list_empty(&cgrp->release_list)) { |
| 4962 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4963 | need_schedule_work = 1; |
| 4964 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4965 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4966 | if (need_schedule_work) |
| 4967 | schedule_work(&release_agent_work); |
| 4968 | } |
| 4969 | } |
| 4970 | |
Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 4971 | /* Caller must verify that the css is not for root cgroup */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4972 | bool __css_tryget(struct cgroup_subsys_state *css) |
| 4973 | { |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4974 | while (true) { |
| 4975 | int t, v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4976 | |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4977 | v = css_refcnt(css); |
| 4978 | t = atomic_cmpxchg(&css->refcnt, v, v + 1); |
| 4979 | if (likely(t == v)) |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4980 | return true; |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4981 | else if (t < 0) |
| 4982 | return false; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4983 | cpu_relax(); |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4984 | } |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4985 | } |
| 4986 | EXPORT_SYMBOL_GPL(__css_tryget); |
| 4987 | |
| 4988 | /* Caller must verify that the css is not for root cgroup */ |
| 4989 | void __css_put(struct cgroup_subsys_state *css) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4990 | { |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 4991 | int v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4992 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 4993 | v = css_unbias_refcnt(atomic_dec_return(&css->refcnt)); |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4994 | if (v == 0) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4995 | schedule_work(&css->dput_work); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4996 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4997 | EXPORT_SYMBOL_GPL(__css_put); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4998 | |
| 4999 | /* |
| 5000 | * Notify userspace when a cgroup is released, by running the |
| 5001 | * configured release agent with the name of the cgroup (path |
| 5002 | * relative to the root of cgroup file system) as the argument. |
| 5003 | * |
| 5004 | * Most likely, this user command will try to rmdir this cgroup. |
| 5005 | * |
| 5006 | * This races with the possibility that some other task will be |
| 5007 | * attached to this cgroup before it is removed, or that some other |
| 5008 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5009 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5010 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5011 | * to continue to serve a useful existence. Next time it's released, |
| 5012 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5013 | * |
| 5014 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5015 | * means only wait until the task is successfully execve()'d. The |
| 5016 | * separate release agent task is forked by call_usermodehelper(), |
| 5017 | * then control in this thread returns here, without waiting for the |
| 5018 | * release agent task. We don't bother to wait because the caller of |
| 5019 | * this routine has no use for the exit status of the release agent |
| 5020 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5021 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5022 | static void cgroup_release_agent(struct work_struct *work) |
| 5023 | { |
| 5024 | BUG_ON(work != &release_agent_work); |
| 5025 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5026 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5027 | while (!list_empty(&release_list)) { |
| 5028 | char *argv[3], *envp[3]; |
| 5029 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5030 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5031 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5032 | struct cgroup, |
| 5033 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5034 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5035 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5036 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5037 | if (!pathbuf) |
| 5038 | goto continue_free; |
| 5039 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5040 | goto continue_free; |
| 5041 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5042 | if (!agentbuf) |
| 5043 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5044 | |
| 5045 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5046 | argv[i++] = agentbuf; |
| 5047 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5048 | argv[i] = NULL; |
| 5049 | |
| 5050 | i = 0; |
| 5051 | /* minimal command environment */ |
| 5052 | envp[i++] = "HOME=/"; |
| 5053 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5054 | envp[i] = NULL; |
| 5055 | |
| 5056 | /* Drop the lock while we invoke the usermode helper, |
| 5057 | * since the exec could involve hitting disk and hence |
| 5058 | * be a slow process */ |
| 5059 | mutex_unlock(&cgroup_mutex); |
| 5060 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5061 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5062 | continue_free: |
| 5063 | kfree(pathbuf); |
| 5064 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5065 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5066 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5067 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5068 | mutex_unlock(&cgroup_mutex); |
| 5069 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5070 | |
| 5071 | static int __init cgroup_disable(char *str) |
| 5072 | { |
| 5073 | int i; |
| 5074 | char *token; |
| 5075 | |
| 5076 | while ((token = strsep(&str, ",")) != NULL) { |
| 5077 | if (!*token) |
| 5078 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5079 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5080 | struct cgroup_subsys *ss = subsys[i]; |
| 5081 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5082 | /* |
| 5083 | * cgroup_disable, being at boot time, can't |
| 5084 | * know about module subsystems, so we don't |
| 5085 | * worry about them. |
| 5086 | */ |
| 5087 | if (!ss || ss->module) |
| 5088 | continue; |
| 5089 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5090 | if (!strcmp(token, ss->name)) { |
| 5091 | ss->disabled = 1; |
| 5092 | printk(KERN_INFO "Disabling %s control group" |
| 5093 | " subsystem\n", ss->name); |
| 5094 | break; |
| 5095 | } |
| 5096 | } |
| 5097 | } |
| 5098 | return 1; |
| 5099 | } |
| 5100 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5101 | |
| 5102 | /* |
| 5103 | * Functons for CSS ID. |
| 5104 | */ |
| 5105 | |
| 5106 | /* |
| 5107 | *To get ID other than 0, this should be called when !cgroup_is_removed(). |
| 5108 | */ |
| 5109 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5110 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5111 | struct css_id *cssid; |
| 5112 | |
| 5113 | /* |
| 5114 | * This css_id() can return correct value when somone has refcnt |
| 5115 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5116 | * it's unchanged until freed. |
| 5117 | */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5118 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5119 | |
| 5120 | if (cssid) |
| 5121 | return cssid->id; |
| 5122 | return 0; |
| 5123 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5124 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5125 | |
| 5126 | unsigned short css_depth(struct cgroup_subsys_state *css) |
| 5127 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5128 | struct css_id *cssid; |
| 5129 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5130 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5131 | |
| 5132 | if (cssid) |
| 5133 | return cssid->depth; |
| 5134 | return 0; |
| 5135 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5136 | EXPORT_SYMBOL_GPL(css_depth); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5137 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5138 | /** |
| 5139 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5140 | * @child: the css to be tested. |
| 5141 | * @root: the css supporsed to be an ancestor of the child. |
| 5142 | * |
| 5143 | * 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] | 5144 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5145 | * But, considering usual usage, the csses should be valid objects after test. |
| 5146 | * Assuming that the caller will do some action to the child if this returns |
| 5147 | * returns true, the caller must take "child";s reference count. |
| 5148 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5149 | */ |
| 5150 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5151 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5152 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5153 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5154 | struct css_id *child_id; |
| 5155 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5156 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5157 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5158 | if (!child_id) |
| 5159 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5160 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5161 | if (!root_id) |
| 5162 | return false; |
| 5163 | if (child_id->depth < root_id->depth) |
| 5164 | return false; |
| 5165 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5166 | return false; |
| 5167 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5168 | } |
| 5169 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5170 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5171 | { |
| 5172 | struct css_id *id = css->id; |
| 5173 | /* When this is called before css_id initialization, id can be NULL */ |
| 5174 | if (!id) |
| 5175 | return; |
| 5176 | |
| 5177 | BUG_ON(!ss->use_id); |
| 5178 | |
| 5179 | rcu_assign_pointer(id->css, NULL); |
| 5180 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5181 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5182 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5183 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5184 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5185 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5186 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5187 | |
| 5188 | /* |
| 5189 | * This is called by init or create(). Then, calls to this function are |
| 5190 | * always serialized (By cgroup_mutex() at create()). |
| 5191 | */ |
| 5192 | |
| 5193 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5194 | { |
| 5195 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5196 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5197 | |
| 5198 | BUG_ON(!ss->use_id); |
| 5199 | |
| 5200 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5201 | newid = kzalloc(size, GFP_KERNEL); |
| 5202 | if (!newid) |
| 5203 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5204 | |
| 5205 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5206 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5207 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5208 | 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] | 5209 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5210 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5211 | |
| 5212 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5213 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5214 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5215 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5216 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5217 | newid->depth = depth; |
| 5218 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5219 | err_out: |
| 5220 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5221 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5222 | |
| 5223 | } |
| 5224 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5225 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5226 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5227 | { |
| 5228 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5229 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5230 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5231 | idr_init(&ss->idr); |
| 5232 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5233 | newid = get_new_cssid(ss, 0); |
| 5234 | if (IS_ERR(newid)) |
| 5235 | return PTR_ERR(newid); |
| 5236 | |
| 5237 | newid->stack[0] = newid->id; |
| 5238 | newid->css = rootcss; |
| 5239 | rootcss->id = newid; |
| 5240 | return 0; |
| 5241 | } |
| 5242 | |
| 5243 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5244 | struct cgroup *child) |
| 5245 | { |
| 5246 | int subsys_id, i, depth = 0; |
| 5247 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5248 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5249 | |
| 5250 | subsys_id = ss->subsys_id; |
| 5251 | parent_css = parent->subsys[subsys_id]; |
| 5252 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5253 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5254 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5255 | |
| 5256 | child_id = get_new_cssid(ss, depth); |
| 5257 | if (IS_ERR(child_id)) |
| 5258 | return PTR_ERR(child_id); |
| 5259 | |
| 5260 | for (i = 0; i < depth; i++) |
| 5261 | child_id->stack[i] = parent_id->stack[i]; |
| 5262 | child_id->stack[depth] = child_id->id; |
| 5263 | /* |
| 5264 | * child_id->css pointer will be set after this cgroup is available |
| 5265 | * see cgroup_populate_dir() |
| 5266 | */ |
| 5267 | rcu_assign_pointer(child_css->id, child_id); |
| 5268 | |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
| 5272 | /** |
| 5273 | * css_lookup - lookup css by id |
| 5274 | * @ss: cgroup subsys to be looked into. |
| 5275 | * @id: the id |
| 5276 | * |
| 5277 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5278 | * NULL if not. Should be called under rcu_read_lock() |
| 5279 | */ |
| 5280 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5281 | { |
| 5282 | struct css_id *cssid = NULL; |
| 5283 | |
| 5284 | BUG_ON(!ss->use_id); |
| 5285 | cssid = idr_find(&ss->idr, id); |
| 5286 | |
| 5287 | if (unlikely(!cssid)) |
| 5288 | return NULL; |
| 5289 | |
| 5290 | return rcu_dereference(cssid->css); |
| 5291 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5292 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5293 | |
| 5294 | /** |
| 5295 | * css_get_next - lookup next cgroup under specified hierarchy. |
| 5296 | * @ss: pointer to subsystem |
| 5297 | * @id: current position of iteration. |
| 5298 | * @root: pointer to css. search tree under this. |
| 5299 | * @foundid: position of found object. |
| 5300 | * |
| 5301 | * Search next css under the specified hierarchy of rootid. Calling under |
| 5302 | * rcu_read_lock() is necessary. Returns NULL if it reaches the end. |
| 5303 | */ |
| 5304 | struct cgroup_subsys_state * |
| 5305 | css_get_next(struct cgroup_subsys *ss, int id, |
| 5306 | struct cgroup_subsys_state *root, int *foundid) |
| 5307 | { |
| 5308 | struct cgroup_subsys_state *ret = NULL; |
| 5309 | struct css_id *tmp; |
| 5310 | int tmpid; |
| 5311 | int rootid = css_id(root); |
| 5312 | int depth = css_depth(root); |
| 5313 | |
| 5314 | if (!rootid) |
| 5315 | return NULL; |
| 5316 | |
| 5317 | BUG_ON(!ss->use_id); |
Hugh Dickins | ca464d6 | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5318 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 5319 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5320 | /* fill start point for scan */ |
| 5321 | tmpid = id; |
| 5322 | while (1) { |
| 5323 | /* |
| 5324 | * scan next entry from bitmap(tree), tmpid is updated after |
| 5325 | * idr_get_next(). |
| 5326 | */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5327 | tmp = idr_get_next(&ss->idr, &tmpid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5328 | if (!tmp) |
| 5329 | break; |
| 5330 | if (tmp->depth >= depth && tmp->stack[depth] == rootid) { |
| 5331 | ret = rcu_dereference(tmp->css); |
| 5332 | if (ret) { |
| 5333 | *foundid = tmpid; |
| 5334 | break; |
| 5335 | } |
| 5336 | } |
| 5337 | /* continue to scan from next id */ |
| 5338 | tmpid = tmpid + 1; |
| 5339 | } |
| 5340 | return ret; |
| 5341 | } |
| 5342 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5343 | /* |
| 5344 | * get corresponding css from file open on cgroupfs directory |
| 5345 | */ |
| 5346 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5347 | { |
| 5348 | struct cgroup *cgrp; |
| 5349 | struct inode *inode; |
| 5350 | struct cgroup_subsys_state *css; |
| 5351 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5352 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5353 | /* check in cgroup filesystem dir */ |
| 5354 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5355 | return ERR_PTR(-EBADF); |
| 5356 | |
| 5357 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5358 | return ERR_PTR(-EINVAL); |
| 5359 | |
| 5360 | /* get cgroup */ |
| 5361 | cgrp = __d_cgrp(f->f_dentry); |
| 5362 | css = cgrp->subsys[id]; |
| 5363 | return css ? css : ERR_PTR(-ENOENT); |
| 5364 | } |
| 5365 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5366 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5367 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5368 | { |
| 5369 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5370 | |
| 5371 | if (!css) |
| 5372 | return ERR_PTR(-ENOMEM); |
| 5373 | |
| 5374 | return css; |
| 5375 | } |
| 5376 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5377 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5378 | { |
| 5379 | kfree(cont->subsys[debug_subsys_id]); |
| 5380 | } |
| 5381 | |
| 5382 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) |
| 5383 | { |
| 5384 | return atomic_read(&cont->count); |
| 5385 | } |
| 5386 | |
| 5387 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5388 | { |
| 5389 | return cgroup_task_count(cont); |
| 5390 | } |
| 5391 | |
| 5392 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5393 | { |
| 5394 | return (u64)(unsigned long)current->cgroups; |
| 5395 | } |
| 5396 | |
| 5397 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5398 | struct cftype *cft) |
| 5399 | { |
| 5400 | u64 count; |
| 5401 | |
| 5402 | rcu_read_lock(); |
| 5403 | count = atomic_read(¤t->cgroups->refcount); |
| 5404 | rcu_read_unlock(); |
| 5405 | return count; |
| 5406 | } |
| 5407 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5408 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5409 | struct cftype *cft, |
| 5410 | struct seq_file *seq) |
| 5411 | { |
| 5412 | struct cg_cgroup_link *link; |
| 5413 | struct css_set *cg; |
| 5414 | |
| 5415 | read_lock(&css_set_lock); |
| 5416 | rcu_read_lock(); |
| 5417 | cg = rcu_dereference(current->cgroups); |
| 5418 | list_for_each_entry(link, &cg->cg_links, cg_link_list) { |
| 5419 | struct cgroup *c = link->cgrp; |
| 5420 | const char *name; |
| 5421 | |
| 5422 | if (c->dentry) |
| 5423 | name = c->dentry->d_name.name; |
| 5424 | else |
| 5425 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5426 | seq_printf(seq, "Root %d group %s\n", |
| 5427 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5428 | } |
| 5429 | rcu_read_unlock(); |
| 5430 | read_unlock(&css_set_lock); |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
| 5434 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5435 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5436 | struct cftype *cft, |
| 5437 | struct seq_file *seq) |
| 5438 | { |
| 5439 | struct cg_cgroup_link *link; |
| 5440 | |
| 5441 | read_lock(&css_set_lock); |
| 5442 | list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { |
| 5443 | struct css_set *cg = link->cg; |
| 5444 | struct task_struct *task; |
| 5445 | int count = 0; |
| 5446 | seq_printf(seq, "css_set %p\n", cg); |
| 5447 | list_for_each_entry(task, &cg->tasks, cg_list) { |
| 5448 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5449 | seq_puts(seq, " ...\n"); |
| 5450 | break; |
| 5451 | } else { |
| 5452 | seq_printf(seq, " task %d\n", |
| 5453 | task_pid_vnr(task)); |
| 5454 | } |
| 5455 | } |
| 5456 | } |
| 5457 | read_unlock(&css_set_lock); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5461 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5462 | { |
| 5463 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5464 | } |
| 5465 | |
| 5466 | static struct cftype debug_files[] = { |
| 5467 | { |
| 5468 | .name = "cgroup_refcount", |
| 5469 | .read_u64 = cgroup_refcount_read, |
| 5470 | }, |
| 5471 | { |
| 5472 | .name = "taskcount", |
| 5473 | .read_u64 = debug_taskcount_read, |
| 5474 | }, |
| 5475 | |
| 5476 | { |
| 5477 | .name = "current_css_set", |
| 5478 | .read_u64 = current_css_set_read, |
| 5479 | }, |
| 5480 | |
| 5481 | { |
| 5482 | .name = "current_css_set_refcount", |
| 5483 | .read_u64 = current_css_set_refcount_read, |
| 5484 | }, |
| 5485 | |
| 5486 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5487 | .name = "current_css_set_cg_links", |
| 5488 | .read_seq_string = current_css_set_cg_links_read, |
| 5489 | }, |
| 5490 | |
| 5491 | { |
| 5492 | .name = "cgroup_css_links", |
| 5493 | .read_seq_string = cgroup_css_links_read, |
| 5494 | }, |
| 5495 | |
| 5496 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5497 | .name = "releasable", |
| 5498 | .read_u64 = releasable_read, |
| 5499 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5500 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5501 | { } /* terminate */ |
| 5502 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5503 | |
| 5504 | struct cgroup_subsys debug_subsys = { |
| 5505 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5506 | .css_alloc = debug_css_alloc, |
| 5507 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5508 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5509 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5510 | }; |
| 5511 | #endif /* CONFIG_CGROUP_DEBUG */ |