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