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