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