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