Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/fs.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 34 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 35 | #include <linux/kernel.h> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/mutex.h> |
| 39 | #include <linux/mount.h> |
| 40 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 41 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 44 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 45 | #include <linux/seq_file.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/magic.h> |
| 48 | #include <linux/spinlock.h> |
| 49 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 50 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 51 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 52 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 53 | #include <linux/delayacct.h> |
| 54 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 55 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 56 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 57 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 58 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 59 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 60 | #include <linux/eventfd.h> |
| 61 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 62 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 63 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 64 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 65 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 66 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 67 | /* css deactivation bias, makes css->refcnt negative to deny new trygets */ |
| 68 | #define CSS_DEACT_BIAS INT_MIN |
| 69 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 70 | /* |
| 71 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 72 | * hierarchy must be performed while holding it. |
| 73 | * |
| 74 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 75 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 76 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 77 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 78 | * break the following locking order cycle. |
| 79 | * |
| 80 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 81 | * B. namespace_sem -> cgroup_mutex |
| 82 | * |
| 83 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 84 | * breaks it. |
| 85 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame^] | 86 | #ifdef CONFIG_PROVE_RCU |
| 87 | DEFINE_MUTEX(cgroup_mutex); |
| 88 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 89 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame^] | 91 | #endif |
| 92 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 93 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 94 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 95 | /* |
| 96 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 97 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 98 | * registered after that. The mutable section of this array is protected by |
| 99 | * cgroup_mutex. |
| 100 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 101 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 102 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 103 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 104 | #include <linux/cgroup_subsys.h> |
| 105 | }; |
| 106 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 107 | #define MAX_CGROUP_ROOT_NAMELEN 64 |
| 108 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 109 | /* |
| 110 | * A cgroupfs_root represents the root of a cgroup hierarchy, |
| 111 | * and may be associated with a superblock to form an active |
| 112 | * hierarchy |
| 113 | */ |
| 114 | struct cgroupfs_root { |
| 115 | struct super_block *sb; |
| 116 | |
| 117 | /* |
| 118 | * The bitmask of subsystems intended to be attached to this |
| 119 | * hierarchy |
| 120 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 121 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 122 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 123 | /* Unique id for this hierarchy. */ |
| 124 | int hierarchy_id; |
| 125 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 126 | /* The bitmask of subsystems currently attached to this hierarchy */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 127 | unsigned long actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 128 | |
| 129 | /* A list running through the attached subsystems */ |
| 130 | struct list_head subsys_list; |
| 131 | |
| 132 | /* The root cgroup for this hierarchy */ |
| 133 | struct cgroup top_cgroup; |
| 134 | |
| 135 | /* Tracks how many cgroups are currently defined in hierarchy.*/ |
| 136 | int number_of_cgroups; |
| 137 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 138 | /* A list running through the active hierarchies */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 139 | struct list_head root_list; |
| 140 | |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 141 | /* All cgroups on this root, cgroup_mutex protected */ |
| 142 | struct list_head allcg_list; |
| 143 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 144 | /* Hierarchy-specific flags */ |
| 145 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 146 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 147 | /* IDs for cgroups in this hierarchy */ |
| 148 | struct ida cgroup_ida; |
| 149 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 150 | /* The path to use for release notifications. */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 151 | char release_agent_path[PATH_MAX]; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 152 | |
| 153 | /* The name for this hierarchy - may be empty */ |
| 154 | char name[MAX_CGROUP_ROOT_NAMELEN]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 157 | /* |
| 158 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 159 | * subsystems that are otherwise unattached - it never has more than a |
| 160 | * single cgroup, and all tasks are part of that cgroup. |
| 161 | */ |
| 162 | static struct cgroupfs_root rootnode; |
| 163 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 164 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 165 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 166 | */ |
| 167 | struct cfent { |
| 168 | struct list_head node; |
| 169 | struct dentry *dentry; |
| 170 | struct cftype *type; |
| 171 | }; |
| 172 | |
| 173 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 174 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 175 | * cgroup_subsys->use_id != 0. |
| 176 | */ |
| 177 | #define CSS_ID_MAX (65535) |
| 178 | struct css_id { |
| 179 | /* |
| 180 | * The css to which this ID points. This pointer is set to valid value |
| 181 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 182 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 183 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 184 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 185 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 186 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 187 | /* |
| 188 | * ID of this css. |
| 189 | */ |
| 190 | unsigned short id; |
| 191 | /* |
| 192 | * Depth in hierarchy which this ID belongs to. |
| 193 | */ |
| 194 | unsigned short depth; |
| 195 | /* |
| 196 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 197 | */ |
| 198 | struct rcu_head rcu_head; |
| 199 | /* |
| 200 | * Hierarchy of CSS ID belongs to. |
| 201 | */ |
| 202 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 203 | }; |
| 204 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 205 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 206 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 207 | */ |
| 208 | struct cgroup_event { |
| 209 | /* |
| 210 | * Cgroup which the event belongs to. |
| 211 | */ |
| 212 | struct cgroup *cgrp; |
| 213 | /* |
| 214 | * Control file which the event associated. |
| 215 | */ |
| 216 | struct cftype *cft; |
| 217 | /* |
| 218 | * eventfd to signal userspace about the event. |
| 219 | */ |
| 220 | struct eventfd_ctx *eventfd; |
| 221 | /* |
| 222 | * Each of these stored in a list by the cgroup. |
| 223 | */ |
| 224 | struct list_head list; |
| 225 | /* |
| 226 | * All fields below needed to unregister event when |
| 227 | * userspace closes eventfd. |
| 228 | */ |
| 229 | poll_table pt; |
| 230 | wait_queue_head_t *wqh; |
| 231 | wait_queue_t wait; |
| 232 | struct work_struct remove; |
| 233 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 234 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 235 | /* The list of hierarchy roots */ |
| 236 | |
| 237 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 238 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 239 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 240 | static DEFINE_IDA(hierarchy_ida); |
| 241 | static int next_hierarchy_id; |
| 242 | static DEFINE_SPINLOCK(hierarchy_id_lock); |
| 243 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 244 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 245 | #define dummytop (&rootnode.top_cgroup) |
| 246 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 247 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 248 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 249 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 250 | * check for fork/exit handlers to call. This avoids us having to do |
| 251 | * extra work in the fork/exit path if none of the subsystems need to |
| 252 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 253 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 254 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 255 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 256 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 257 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 258 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 259 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 260 | static int css_unbias_refcnt(int refcnt) |
| 261 | { |
| 262 | return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS; |
| 263 | } |
| 264 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 265 | /* the current nr of refs, always >= 0 whether @css is deactivated or not */ |
| 266 | static int css_refcnt(struct cgroup_subsys_state *css) |
| 267 | { |
| 268 | int v = atomic_read(&css->refcnt); |
| 269 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 270 | return css_unbias_refcnt(v); |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 273 | /* convenient tests for these bits */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 274 | inline int cgroup_is_removed(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 275 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 276 | return test_bit(CGRP_REMOVED, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* bits in struct cgroupfs_root flags field */ |
| 280 | enum { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 281 | ROOT_NOPREFIX, /* mounted subsystems have no named prefix */ |
| 282 | ROOT_XATTR, /* supports extended attributes */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 285 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 286 | { |
| 287 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 288 | (1 << CGRP_RELEASABLE) | |
| 289 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 290 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 293 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 294 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 295 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 298 | /* |
| 299 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 300 | * an active hierarchy |
| 301 | */ |
| 302 | #define for_each_subsys(_root, _ss) \ |
| 303 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 304 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 305 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 306 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 307 | list_for_each_entry(_root, &roots, root_list) |
| 308 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 309 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 310 | { |
| 311 | return dentry->d_fsdata; |
| 312 | } |
| 313 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 314 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 315 | { |
| 316 | return dentry->d_fsdata; |
| 317 | } |
| 318 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 319 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 320 | { |
| 321 | return __d_cfe(dentry)->type; |
| 322 | } |
| 323 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 324 | /** |
| 325 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 326 | * @cgrp: the cgroup to be checked for liveness |
| 327 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 328 | * On success, returns true; the mutex should be later unlocked. On |
| 329 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 330 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 331 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 332 | { |
| 333 | mutex_lock(&cgroup_mutex); |
| 334 | if (cgroup_is_removed(cgrp)) { |
| 335 | mutex_unlock(&cgroup_mutex); |
| 336 | return false; |
| 337 | } |
| 338 | return true; |
| 339 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 340 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 341 | /* the list of cgroups eligible for automatic release. Protected by |
| 342 | * release_list_lock */ |
| 343 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 344 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 345 | static void cgroup_release_agent(struct work_struct *work); |
| 346 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 347 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 348 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 349 | /* Link structure for associating css_set objects with cgroups */ |
| 350 | struct cg_cgroup_link { |
| 351 | /* |
| 352 | * List running through cg_cgroup_links associated with a |
| 353 | * cgroup, anchored on cgroup->css_sets |
| 354 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 355 | struct list_head cgrp_link_list; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 356 | struct cgroup *cgrp; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 357 | /* |
| 358 | * List running through cg_cgroup_links pointing at a |
| 359 | * single css_set object, anchored on css_set->cg_links |
| 360 | */ |
| 361 | struct list_head cg_link_list; |
| 362 | struct css_set *cg; |
| 363 | }; |
| 364 | |
| 365 | /* The default css_set - used by init and its children prior to any |
| 366 | * hierarchies being mounted. It contains a pointer to the root state |
| 367 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 368 | * reference-counted, to improve performance when child cgroups |
| 369 | * haven't been created. |
| 370 | */ |
| 371 | |
| 372 | static struct css_set init_css_set; |
| 373 | static struct cg_cgroup_link init_css_set_link; |
| 374 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 375 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 376 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 377 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 378 | /* css_set_lock protects the list of css_set objects, and the |
| 379 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 380 | * due to cgroup_iter_start() */ |
| 381 | static DEFINE_RWLOCK(css_set_lock); |
| 382 | static int css_set_count; |
| 383 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 384 | /* |
| 385 | * hash table for cgroup groups. This improves the performance to find |
| 386 | * an existing css_set. This hash doesn't (currently) take into |
| 387 | * account cgroups in empty hierarchies. |
| 388 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 389 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 390 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 391 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 392 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 393 | { |
| 394 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 395 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 396 | |
| 397 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 398 | key += (unsigned long)css[i]; |
| 399 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 400 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 401 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 404 | /* We don't maintain the lists running through each css_set to its |
| 405 | * task until after the first call to cgroup_iter_start(). This |
| 406 | * reduces the fork()/exit() overhead for people who have cgroups |
| 407 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 408 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 409 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 410 | static void __put_css_set(struct css_set *cg, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 411 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 412 | struct cg_cgroup_link *link; |
| 413 | struct cg_cgroup_link *saved_link; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 414 | /* |
| 415 | * Ensure that the refcount doesn't hit zero while any readers |
| 416 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 417 | * rwlock |
| 418 | */ |
| 419 | if (atomic_add_unless(&cg->refcount, -1, 1)) |
| 420 | return; |
| 421 | write_lock(&css_set_lock); |
| 422 | if (!atomic_dec_and_test(&cg->refcount)) { |
| 423 | write_unlock(&css_set_lock); |
| 424 | return; |
| 425 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 426 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 427 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 428 | hash_del(&cg->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 429 | css_set_count--; |
| 430 | |
| 431 | list_for_each_entry_safe(link, saved_link, &cg->cg_links, |
| 432 | cg_link_list) { |
| 433 | struct cgroup *cgrp = link->cgrp; |
| 434 | list_del(&link->cg_link_list); |
| 435 | list_del(&link->cgrp_link_list); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * We may not be holding cgroup_mutex, and if cgrp->count is |
| 439 | * dropped to 0 the cgroup can be destroyed at any time, hence |
| 440 | * rcu_read_lock is used to keep it alive. |
| 441 | */ |
| 442 | rcu_read_lock(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 443 | if (atomic_dec_and_test(&cgrp->count) && |
| 444 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 445 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 446 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 447 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 448 | } |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 449 | rcu_read_unlock(); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 450 | |
| 451 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 452 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 453 | |
| 454 | write_unlock(&css_set_lock); |
Lai Jiangshan | 30088ad | 2011-03-15 17:53:46 +0800 | [diff] [blame] | 455 | kfree_rcu(cg, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* |
| 459 | * refcounted get/put for css_set objects |
| 460 | */ |
| 461 | static inline void get_css_set(struct css_set *cg) |
| 462 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 463 | atomic_inc(&cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static inline void put_css_set(struct css_set *cg) |
| 467 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 468 | __put_css_set(cg, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 471 | static inline void put_css_set_taskexit(struct css_set *cg) |
| 472 | { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 473 | __put_css_set(cg, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 476 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 477 | * compare_css_sets - helper function for find_existing_css_set(). |
| 478 | * @cg: candidate css_set being tested |
| 479 | * @old_cg: existing css_set for a task |
| 480 | * @new_cgrp: cgroup that's being entered by the task |
| 481 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 482 | * |
| 483 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 484 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 485 | */ |
| 486 | static bool compare_css_sets(struct css_set *cg, |
| 487 | struct css_set *old_cg, |
| 488 | struct cgroup *new_cgrp, |
| 489 | struct cgroup_subsys_state *template[]) |
| 490 | { |
| 491 | struct list_head *l1, *l2; |
| 492 | |
| 493 | if (memcmp(template, cg->subsys, sizeof(cg->subsys))) { |
| 494 | /* Not all subsystems matched */ |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Compare cgroup pointers in order to distinguish between |
| 500 | * different cgroups in heirarchies with no subsystems. We |
| 501 | * could get by with just this check alone (and skip the |
| 502 | * memcmp above) but on most setups the memcmp check will |
| 503 | * avoid the need for this more expensive check on almost all |
| 504 | * candidates. |
| 505 | */ |
| 506 | |
| 507 | l1 = &cg->cg_links; |
| 508 | l2 = &old_cg->cg_links; |
| 509 | while (1) { |
| 510 | struct cg_cgroup_link *cgl1, *cgl2; |
| 511 | struct cgroup *cg1, *cg2; |
| 512 | |
| 513 | l1 = l1->next; |
| 514 | l2 = l2->next; |
| 515 | /* See if we reached the end - both lists are equal length. */ |
| 516 | if (l1 == &cg->cg_links) { |
| 517 | BUG_ON(l2 != &old_cg->cg_links); |
| 518 | break; |
| 519 | } else { |
| 520 | BUG_ON(l2 == &old_cg->cg_links); |
| 521 | } |
| 522 | /* Locate the cgroups associated with these links. */ |
| 523 | cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); |
| 524 | cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); |
| 525 | cg1 = cgl1->cgrp; |
| 526 | cg2 = cgl2->cgrp; |
| 527 | /* Hierarchies should be linked in the same order. */ |
| 528 | BUG_ON(cg1->root != cg2->root); |
| 529 | |
| 530 | /* |
| 531 | * If this hierarchy is the hierarchy of the cgroup |
| 532 | * that's changing, then we need to check that this |
| 533 | * css_set points to the new cgroup; if it's any other |
| 534 | * hierarchy, then this css_set should point to the |
| 535 | * same cgroup as the old css_set. |
| 536 | */ |
| 537 | if (cg1->root == new_cgrp->root) { |
| 538 | if (cg1 != new_cgrp) |
| 539 | return false; |
| 540 | } else { |
| 541 | if (cg1 != cg2) |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 549 | * find_existing_css_set() is a helper for |
| 550 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 551 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 552 | * |
| 553 | * oldcg: the cgroup group that we're using before the cgroup |
| 554 | * transition |
| 555 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 556 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | * |
| 558 | * template: location in which to build the desired set of subsystem |
| 559 | * state objects for the new cgroup group |
| 560 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | static struct css_set *find_existing_css_set( |
| 562 | struct css_set *oldcg, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 563 | struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 564 | struct cgroup_subsys_state *template[]) |
| 565 | { |
| 566 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 567 | struct cgroupfs_root *root = cgrp->root; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 568 | struct css_set *cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 569 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 570 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 571 | /* |
| 572 | * Build the set of subsystem state objects that we want to see in the |
| 573 | * new css_set. while subsystems can change globally, the entries here |
| 574 | * won't change, so no need for locking. |
| 575 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 576 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 577 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 578 | /* Subsystem is in this hierarchy. So we want |
| 579 | * the subsystem state from the new |
| 580 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 581 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 582 | } else { |
| 583 | /* Subsystem is not in this hierarchy, so we |
| 584 | * don't want to change the subsystem state */ |
| 585 | template[i] = oldcg->subsys[i]; |
| 586 | } |
| 587 | } |
| 588 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 589 | key = css_set_hash(template); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 590 | hash_for_each_possible(css_set_table, cg, hlist, key) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 591 | if (!compare_css_sets(cg, oldcg, cgrp, template)) |
| 592 | continue; |
| 593 | |
| 594 | /* This css_set matches what we need */ |
| 595 | return cg; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 596 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 597 | |
| 598 | /* No existing cgroup group matched */ |
| 599 | return NULL; |
| 600 | } |
| 601 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 602 | static void free_cg_links(struct list_head *tmp) |
| 603 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 604 | struct cg_cgroup_link *link; |
| 605 | struct cg_cgroup_link *saved_link; |
| 606 | |
| 607 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 608 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 609 | kfree(link); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 614 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 615 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 616 | * success or a negative error |
| 617 | */ |
| 618 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 619 | { |
| 620 | struct cg_cgroup_link *link; |
| 621 | int i; |
| 622 | INIT_LIST_HEAD(tmp); |
| 623 | for (i = 0; i < count; i++) { |
| 624 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 625 | if (!link) { |
| 626 | free_cg_links(tmp); |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | list_add(&link->cgrp_link_list, tmp); |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 634 | /** |
| 635 | * link_css_set - a helper function to link a css_set to a cgroup |
| 636 | * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() |
| 637 | * @cg: the css_set to be linked |
| 638 | * @cgrp: the destination cgroup |
| 639 | */ |
| 640 | static void link_css_set(struct list_head *tmp_cg_links, |
| 641 | struct css_set *cg, struct cgroup *cgrp) |
| 642 | { |
| 643 | struct cg_cgroup_link *link; |
| 644 | |
| 645 | BUG_ON(list_empty(tmp_cg_links)); |
| 646 | link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, |
| 647 | cgrp_link_list); |
| 648 | link->cg = cg; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 649 | link->cgrp = cgrp; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 650 | atomic_inc(&cgrp->count); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 651 | list_move(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 652 | /* |
| 653 | * Always add links to the tail of the list so that the list |
| 654 | * is sorted by order of hierarchy creation |
| 655 | */ |
| 656 | list_add_tail(&link->cg_link_list, &cg->cg_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 659 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 660 | * find_css_set() takes an existing cgroup group and a |
| 661 | * cgroup object, and returns a css_set object that's |
| 662 | * equivalent to the old group, but with the given cgroup |
| 663 | * substituted into the appropriate hierarchy. Must be called with |
| 664 | * cgroup_mutex held |
| 665 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | static struct css_set *find_css_set( |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 667 | struct css_set *oldcg, struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 668 | { |
| 669 | struct css_set *res; |
| 670 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 671 | |
| 672 | struct list_head tmp_cg_links; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 673 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 674 | struct cg_cgroup_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 675 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 676 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | /* First see if we already have a cgroup group that matches |
| 678 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 679 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 680 | res = find_existing_css_set(oldcg, cgrp, template); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 681 | if (res) |
| 682 | get_css_set(res); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 683 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 684 | |
| 685 | if (res) |
| 686 | return res; |
| 687 | |
| 688 | res = kmalloc(sizeof(*res), GFP_KERNEL); |
| 689 | if (!res) |
| 690 | return NULL; |
| 691 | |
| 692 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 693 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
| 694 | kfree(res); |
| 695 | return NULL; |
| 696 | } |
| 697 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 698 | atomic_set(&res->refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 699 | INIT_LIST_HEAD(&res->cg_links); |
| 700 | INIT_LIST_HEAD(&res->tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 701 | INIT_HLIST_NODE(&res->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 702 | |
| 703 | /* Copy the set of subsystem state objects generated in |
| 704 | * find_existing_css_set() */ |
| 705 | memcpy(res->subsys, template, sizeof(res->subsys)); |
| 706 | |
| 707 | write_lock(&css_set_lock); |
| 708 | /* Add reference counts and links from the new css_set. */ |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 709 | list_for_each_entry(link, &oldcg->cg_links, cg_link_list) { |
| 710 | struct cgroup *c = link->cgrp; |
| 711 | if (c->root == cgrp->root) |
| 712 | c = cgrp; |
| 713 | link_css_set(&tmp_cg_links, res, c); |
| 714 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 715 | |
| 716 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 717 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 718 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 719 | |
| 720 | /* Add this cgroup group to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 721 | key = css_set_hash(res->subsys); |
| 722 | hash_add(css_set_table, &res->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 723 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 724 | write_unlock(&css_set_lock); |
| 725 | |
| 726 | return res; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 729 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 730 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 731 | * called with cgroup_mutex held. |
| 732 | */ |
| 733 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 734 | struct cgroupfs_root *root) |
| 735 | { |
| 736 | struct css_set *css; |
| 737 | struct cgroup *res = NULL; |
| 738 | |
| 739 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 740 | read_lock(&css_set_lock); |
| 741 | /* |
| 742 | * No need to lock the task - since we hold cgroup_mutex the |
| 743 | * task can't change groups, so the only thing that can happen |
| 744 | * is that it exits and its css is set back to init_css_set. |
| 745 | */ |
| 746 | css = task->cgroups; |
| 747 | if (css == &init_css_set) { |
| 748 | res = &root->top_cgroup; |
| 749 | } else { |
| 750 | struct cg_cgroup_link *link; |
| 751 | list_for_each_entry(link, &css->cg_links, cg_link_list) { |
| 752 | struct cgroup *c = link->cgrp; |
| 753 | if (c->root == root) { |
| 754 | res = c; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | read_unlock(&css_set_lock); |
| 760 | BUG_ON(!res); |
| 761 | return res; |
| 762 | } |
| 763 | |
| 764 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 765 | * There is one global cgroup mutex. We also require taking |
| 766 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 767 | * See "The task_lock() exception", at the end of this comment. |
| 768 | * |
| 769 | * A task must hold cgroup_mutex to modify cgroups. |
| 770 | * |
| 771 | * Any task can increment and decrement the count field without lock. |
| 772 | * So in general, code holding cgroup_mutex can't rely on the count |
| 773 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 774 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 | * means that no tasks are currently attached, therefore there is no |
| 776 | * way a task attached to that cgroup can fork (the other way to |
| 777 | * increment the count). So code holding cgroup_mutex can safely |
| 778 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 779 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 780 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 781 | * needs that mutex. |
| 782 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 783 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 784 | * (usually) take cgroup_mutex. These are the two most performance |
| 785 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 786 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 787 | * 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] | 788 | * to the release agent with the name of the cgroup (path relative to |
| 789 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 790 | * |
| 791 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 792 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 793 | * tasks in the system use _some_ cgroup, and since there is always at |
| 794 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 795 | * always has either children cgroups and/or using tasks. So we don't |
| 796 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 797 | * |
| 798 | * The task_lock() exception |
| 799 | * |
| 800 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 801 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 802 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | * several performance critical places that need to reference |
| 804 | * task->cgroup without the expense of grabbing a system global |
| 805 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 806 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 807 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 808 | * the task_struct routinely used for such matters. |
| 809 | * |
| 810 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 811 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 812 | */ |
| 813 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 814 | /* |
| 815 | * A couple of forward declarations required, due to cyclic reference loop: |
| 816 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 817 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 818 | * -> cgroup_mkdir. |
| 819 | */ |
| 820 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 821 | 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] | 822 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 823 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 824 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 825 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 826 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 827 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 828 | |
| 829 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 830 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 831 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 832 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 833 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 834 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 835 | struct cgroup *parent, struct cgroup *child); |
| 836 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 837 | 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] | 838 | { |
| 839 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 840 | |
| 841 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 842 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 843 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 844 | inode->i_uid = current_fsuid(); |
| 845 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 846 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 847 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 848 | } |
| 849 | return inode; |
| 850 | } |
| 851 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 852 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 853 | { |
| 854 | struct cgroup_name *name; |
| 855 | |
| 856 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 857 | if (!name) |
| 858 | return NULL; |
| 859 | strcpy(name->name, dentry->d_name.name); |
| 860 | return name; |
| 861 | } |
| 862 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 863 | static void cgroup_free_fn(struct work_struct *work) |
| 864 | { |
| 865 | struct cgroup *cgrp = container_of(work, struct cgroup, free_work); |
| 866 | struct cgroup_subsys *ss; |
| 867 | |
| 868 | mutex_lock(&cgroup_mutex); |
| 869 | /* |
| 870 | * Release the subsystem state objects. |
| 871 | */ |
| 872 | for_each_subsys(cgrp->root, ss) |
| 873 | ss->css_free(cgrp); |
| 874 | |
| 875 | cgrp->root->number_of_cgroups--; |
| 876 | mutex_unlock(&cgroup_mutex); |
| 877 | |
| 878 | /* |
| 879 | * Drop the active superblock reference that we took when we |
| 880 | * created the cgroup |
| 881 | */ |
| 882 | deactivate_super(cgrp->root->sb); |
| 883 | |
| 884 | /* |
| 885 | * if we're getting rid of the cgroup, refcount should ensure |
| 886 | * that there are no pidlists left. |
| 887 | */ |
| 888 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 889 | |
| 890 | simple_xattrs_free(&cgrp->xattrs); |
| 891 | |
| 892 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 893 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 894 | kfree(cgrp); |
| 895 | } |
| 896 | |
| 897 | static void cgroup_free_rcu(struct rcu_head *head) |
| 898 | { |
| 899 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 900 | |
| 901 | schedule_work(&cgrp->free_work); |
| 902 | } |
| 903 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 904 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 905 | { |
| 906 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 907 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 908 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 909 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 910 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 911 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 912 | } else { |
| 913 | struct cfent *cfe = __d_cfe(dentry); |
| 914 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 915 | struct cftype *cft = cfe->type; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 916 | |
| 917 | WARN_ONCE(!list_empty(&cfe->node) && |
| 918 | cgrp != &cgrp->root->top_cgroup, |
| 919 | "cfe still linked for %s\n", cfe->type->name); |
| 920 | kfree(cfe); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 921 | simple_xattrs_free(&cft->xattrs); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 922 | } |
| 923 | iput(inode); |
| 924 | } |
| 925 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 926 | static int cgroup_delete(const struct dentry *d) |
| 927 | { |
| 928 | return 1; |
| 929 | } |
| 930 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 931 | static void remove_dir(struct dentry *d) |
| 932 | { |
| 933 | struct dentry *parent = dget(d->d_parent); |
| 934 | |
| 935 | d_delete(d); |
| 936 | simple_rmdir(parent->d_inode, d); |
| 937 | dput(parent); |
| 938 | } |
| 939 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 940 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 941 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 942 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 943 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 944 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 945 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 946 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 947 | /* |
| 948 | * If we're doing cleanup due to failure of cgroup_create(), |
| 949 | * the corresponding @cfe may not exist. |
| 950 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 951 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 952 | struct dentry *d = cfe->dentry; |
| 953 | |
| 954 | if (cft && cfe->type != cft) |
| 955 | continue; |
| 956 | |
| 957 | dget(d); |
| 958 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 959 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 960 | list_del_init(&cfe->node); |
| 961 | dput(d); |
| 962 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 963 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 964 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 967 | /** |
| 968 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 969 | * @dir: directory containing the files |
| 970 | * @base_files: true if the base files should be removed |
| 971 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 972 | */ |
| 973 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 974 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 975 | { |
| 976 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 977 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 978 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 979 | for_each_subsys(cgrp->root, ss) { |
| 980 | struct cftype_set *set; |
| 981 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 982 | continue; |
| 983 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 984 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 985 | } |
| 986 | if (base_files) { |
| 987 | while (!list_empty(&cgrp->files)) |
| 988 | cgroup_rm_file(cgrp, NULL); |
| 989 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
| 993 | * NOTE : the dentry must have been dget()'ed |
| 994 | */ |
| 995 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 996 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 997 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 998 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 999 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1000 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1002 | parent = dentry->d_parent; |
| 1003 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 1004 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1005 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 1006 | spin_unlock(&dentry->d_lock); |
| 1007 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1008 | remove_dir(dentry); |
| 1009 | } |
| 1010 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1011 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1012 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1013 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1014 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1015 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1016 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1017 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1018 | { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1019 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1020 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1021 | int i; |
| 1022 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1023 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1024 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1025 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1026 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 1027 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1028 | /* Check that any added subsystems are currently free */ |
| 1029 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1030 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1031 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1032 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1034 | /* |
| 1035 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1036 | * parse_cgroupfs_options should catch that case and refcounts |
| 1037 | * ensure that subsystems won't disappear once selected. |
| 1038 | */ |
| 1039 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1040 | if (ss->root != &rootnode) { |
| 1041 | /* Subsystem isn't free */ |
| 1042 | return -EBUSY; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /* Currently we don't handle adding/removing subsystems when |
| 1047 | * any child cgroups exist. This is theoretically supportable |
| 1048 | * but involves complex error handling, so it's being left until |
| 1049 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1050 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1051 | return -EBUSY; |
| 1052 | |
| 1053 | /* Process each subsystem */ |
| 1054 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1055 | struct cgroup_subsys *ss = subsys[i]; |
| 1056 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1057 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1059 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1060 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1061 | BUG_ON(!dummytop->subsys[i]); |
| 1062 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1063 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1064 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1065 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1066 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1067 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1068 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1069 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1070 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1071 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1072 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1073 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1074 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1076 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1077 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1078 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1079 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1080 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1081 | /* subsystem is now free - drop reference on module */ |
| 1082 | module_put(ss->module); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1083 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1085 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1086 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1087 | /* |
| 1088 | * a refcount was taken, but we already had one, so |
| 1089 | * drop the extra reference. |
| 1090 | */ |
| 1091 | module_put(ss->module); |
| 1092 | #ifdef CONFIG_MODULE_UNLOAD |
| 1093 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1094 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1095 | } else { |
| 1096 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1097 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1100 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1105 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1106 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1107 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1108 | struct cgroup_subsys *ss; |
| 1109 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1110 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1111 | for_each_subsys(root, ss) |
| 1112 | seq_printf(seq, ",%s", ss->name); |
| 1113 | if (test_bit(ROOT_NOPREFIX, &root->flags)) |
| 1114 | seq_puts(seq, ",noprefix"); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1115 | if (test_bit(ROOT_XATTR, &root->flags)) |
| 1116 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1117 | if (strlen(root->release_agent_path)) |
| 1118 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1119 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1120 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1121 | if (strlen(root->name)) |
| 1122 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1123 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1128 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1129 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1130 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1131 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1132 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1133 | /* User explicitly requested empty subsystem */ |
| 1134 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1135 | |
| 1136 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1137 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1138 | }; |
| 1139 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1140 | /* |
| 1141 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1142 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1143 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1144 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1145 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1146 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1147 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1148 | char *token, *o = data; |
| 1149 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1150 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1151 | int i; |
| 1152 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1153 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1154 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1155 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1156 | #ifdef CONFIG_CPUSETS |
| 1157 | mask = ~(1UL << cpuset_subsys_id); |
| 1158 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1159 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1160 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1161 | |
| 1162 | while ((token = strsep(&o, ",")) != NULL) { |
| 1163 | if (!*token) |
| 1164 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1165 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1166 | /* Explicitly have no subsystems */ |
| 1167 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1168 | continue; |
| 1169 | } |
| 1170 | if (!strcmp(token, "all")) { |
| 1171 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1172 | if (one_ss) |
| 1173 | return -EINVAL; |
| 1174 | all_ss = true; |
| 1175 | continue; |
| 1176 | } |
| 1177 | if (!strcmp(token, "noprefix")) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1178 | set_bit(ROOT_NOPREFIX, &opts->flags); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1179 | continue; |
| 1180 | } |
| 1181 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1182 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1185 | if (!strcmp(token, "xattr")) { |
| 1186 | set_bit(ROOT_XATTR, &opts->flags); |
| 1187 | continue; |
| 1188 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1189 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1190 | /* Specifying two release agents is forbidden */ |
| 1191 | if (opts->release_agent) |
| 1192 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1193 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1194 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1195 | if (!opts->release_agent) |
| 1196 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1197 | continue; |
| 1198 | } |
| 1199 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1200 | const char *name = token + 5; |
| 1201 | /* Can't specify an empty name */ |
| 1202 | if (!strlen(name)) |
| 1203 | return -EINVAL; |
| 1204 | /* Must match [\w.-]+ */ |
| 1205 | for (i = 0; i < strlen(name); i++) { |
| 1206 | char c = name[i]; |
| 1207 | if (isalnum(c)) |
| 1208 | continue; |
| 1209 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1210 | continue; |
| 1211 | return -EINVAL; |
| 1212 | } |
| 1213 | /* Specifying two names is forbidden */ |
| 1214 | if (opts->name) |
| 1215 | return -EINVAL; |
| 1216 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1217 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1218 | GFP_KERNEL); |
| 1219 | if (!opts->name) |
| 1220 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1221 | |
| 1222 | continue; |
| 1223 | } |
| 1224 | |
| 1225 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1226 | struct cgroup_subsys *ss = subsys[i]; |
| 1227 | if (ss == NULL) |
| 1228 | continue; |
| 1229 | if (strcmp(token, ss->name)) |
| 1230 | continue; |
| 1231 | if (ss->disabled) |
| 1232 | continue; |
| 1233 | |
| 1234 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1235 | if (all_ss) |
| 1236 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1237 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1238 | one_ss = true; |
| 1239 | |
| 1240 | break; |
| 1241 | } |
| 1242 | if (i == CGROUP_SUBSYS_COUNT) |
| 1243 | return -ENOENT; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1248 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1249 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1250 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1251 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1252 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1253 | struct cgroup_subsys *ss = subsys[i]; |
| 1254 | if (ss == NULL) |
| 1255 | continue; |
| 1256 | if (ss->disabled) |
| 1257 | continue; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1258 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1262 | /* Consistency checks */ |
| 1263 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1264 | /* |
| 1265 | * Option noprefix was introduced just for backward compatibility |
| 1266 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1267 | * the cpuset subsystem. |
| 1268 | */ |
| 1269 | if (test_bit(ROOT_NOPREFIX, &opts->flags) && |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1270 | (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1271 | return -EINVAL; |
| 1272 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1275 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1276 | return -EINVAL; |
| 1277 | |
| 1278 | /* |
| 1279 | * We either have to specify by name or by subsystems. (So all |
| 1280 | * empty hierarchies must have a name). |
| 1281 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1282 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1283 | return -EINVAL; |
| 1284 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | /* |
| 1286 | * Grab references on all the modules we'll need, so the subsystems |
| 1287 | * don't dance around before rebind_subsystems attaches them. This may |
| 1288 | * take duplicate reference counts on a subsystem that's already used, |
| 1289 | * but rebind_subsystems handles this case. |
| 1290 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1291 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1292 | unsigned long bit = 1UL << i; |
| 1293 | |
Aristeu Rozanski | a1a71b45 | 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 | if (!try_module_get(subsys[i]->module)) { |
| 1297 | module_pin_failed = true; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | if (module_pin_failed) { |
| 1302 | /* |
| 1303 | * oops, one of the modules was going away. this means that we |
| 1304 | * raced with a module_delete call, and to the user this is |
| 1305 | * essentially a "subsystem doesn't exist" case. |
| 1306 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1307 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1308 | /* drop refcounts only on the ones we took */ |
| 1309 | unsigned long bit = 1UL << i; |
| 1310 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1311 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | continue; |
| 1313 | module_put(subsys[i]->module); |
| 1314 | } |
| 1315 | return -ENOENT; |
| 1316 | } |
| 1317 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
| 1320 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1321 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1322 | { |
| 1323 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1324 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1325 | unsigned long bit = 1UL << i; |
| 1326 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1327 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1328 | continue; |
| 1329 | module_put(subsys[i]->module); |
| 1330 | } |
| 1331 | } |
| 1332 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1333 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1334 | { |
| 1335 | int ret = 0; |
| 1336 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1337 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1338 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1339 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1340 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1341 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1342 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1343 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | |
| 1345 | /* See what subsystems are wanted */ |
| 1346 | ret = parse_cgroupfs_options(data, &opts); |
| 1347 | if (ret) |
| 1348 | goto out_unlock; |
| 1349 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1350 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1351 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1352 | task_tgid_nr(current), current->comm); |
| 1353 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1354 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1355 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1356 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1357 | /* Don't allow flags or name to change at remount */ |
| 1358 | if (opts.flags != root->flags || |
| 1359 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1360 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1361 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1362 | goto out_unlock; |
| 1363 | } |
| 1364 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1365 | /* |
| 1366 | * Clear out the files of subsystems that should be removed, do |
| 1367 | * this before rebind_subsystems, since rebind_subsystems may |
| 1368 | * change this hierarchy's subsys_list. |
| 1369 | */ |
| 1370 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1371 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1372 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1373 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1374 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1375 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1376 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1377 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1378 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1379 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1380 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1381 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1382 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1383 | if (opts.release_agent) |
| 1384 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1385 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1386 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1387 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1388 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1389 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1390 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | return ret; |
| 1392 | } |
| 1393 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1394 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1395 | .statfs = simple_statfs, |
| 1396 | .drop_inode = generic_delete_inode, |
| 1397 | .show_options = cgroup_show_options, |
| 1398 | .remount_fs = cgroup_remount, |
| 1399 | }; |
| 1400 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1401 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1402 | { |
| 1403 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1404 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1405 | INIT_LIST_HEAD(&cgrp->files); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1406 | INIT_LIST_HEAD(&cgrp->css_sets); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1407 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1408 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1409 | INIT_LIST_HEAD(&cgrp->pidlists); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1410 | INIT_WORK(&cgrp->free_work, cgroup_free_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1411 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1412 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1413 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1414 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1415 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1416 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1417 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1418 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1419 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1420 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | INIT_LIST_HEAD(&root->subsys_list); |
| 1422 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1423 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1424 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1425 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1426 | cgrp->name = &root_cgroup_name; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1427 | cgrp->top_cgroup = cgrp; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1428 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1429 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1432 | static bool init_root_id(struct cgroupfs_root *root) |
| 1433 | { |
| 1434 | int ret = 0; |
| 1435 | |
| 1436 | do { |
| 1437 | if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL)) |
| 1438 | return false; |
| 1439 | spin_lock(&hierarchy_id_lock); |
| 1440 | /* Try to allocate the next unused ID */ |
| 1441 | ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id, |
| 1442 | &root->hierarchy_id); |
| 1443 | if (ret == -ENOSPC) |
| 1444 | /* Try again starting from 0 */ |
| 1445 | ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id); |
| 1446 | if (!ret) { |
| 1447 | next_hierarchy_id = root->hierarchy_id + 1; |
| 1448 | } else if (ret != -EAGAIN) { |
| 1449 | /* Can only get here if the 31-bit IDR is full ... */ |
| 1450 | BUG_ON(ret); |
| 1451 | } |
| 1452 | spin_unlock(&hierarchy_id_lock); |
| 1453 | } while (ret); |
| 1454 | return true; |
| 1455 | } |
| 1456 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1457 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1458 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1459 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1460 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1461 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1462 | /* If we asked for a name then it must match */ |
| 1463 | if (opts->name && strcmp(opts->name, root->name)) |
| 1464 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1465 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1466 | /* |
| 1467 | * If we asked for subsystems (or explicitly for no |
| 1468 | * subsystems) then they must match |
| 1469 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1470 | if ((opts->subsys_mask || opts->none) |
| 1471 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1472 | return 0; |
| 1473 | |
| 1474 | return 1; |
| 1475 | } |
| 1476 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1477 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1478 | { |
| 1479 | struct cgroupfs_root *root; |
| 1480 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1481 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1482 | return NULL; |
| 1483 | |
| 1484 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1485 | if (!root) |
| 1486 | return ERR_PTR(-ENOMEM); |
| 1487 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1488 | if (!init_root_id(root)) { |
| 1489 | kfree(root); |
| 1490 | return ERR_PTR(-ENOMEM); |
| 1491 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1492 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1493 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1494 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1495 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1496 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1497 | if (opts->release_agent) |
| 1498 | strcpy(root->release_agent_path, opts->release_agent); |
| 1499 | if (opts->name) |
| 1500 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1501 | if (opts->cpuset_clone_children) |
| 1502 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1503 | return root; |
| 1504 | } |
| 1505 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1506 | static void cgroup_drop_root(struct cgroupfs_root *root) |
| 1507 | { |
| 1508 | if (!root) |
| 1509 | return; |
| 1510 | |
| 1511 | BUG_ON(!root->hierarchy_id); |
| 1512 | spin_lock(&hierarchy_id_lock); |
| 1513 | ida_remove(&hierarchy_ida, root->hierarchy_id); |
| 1514 | spin_unlock(&hierarchy_id_lock); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1515 | ida_destroy(&root->cgroup_ida); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1516 | kfree(root); |
| 1517 | } |
| 1518 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1519 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1520 | { |
| 1521 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1522 | struct cgroup_sb_opts *opts = data; |
| 1523 | |
| 1524 | /* If we don't have a new root, we can't set up a new sb */ |
| 1525 | if (!opts->new_root) |
| 1526 | return -EINVAL; |
| 1527 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1528 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1529 | |
| 1530 | ret = set_anon_super(sb, NULL); |
| 1531 | if (ret) |
| 1532 | return ret; |
| 1533 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1534 | sb->s_fs_info = opts->new_root; |
| 1535 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1536 | |
| 1537 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1538 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1539 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1540 | sb->s_op = &cgroup_ops; |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1546 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1547 | static const struct dentry_operations cgroup_dops = { |
| 1548 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1549 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1550 | }; |
| 1551 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1552 | struct inode *inode = |
| 1553 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1554 | |
| 1555 | if (!inode) |
| 1556 | return -ENOMEM; |
| 1557 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1558 | inode->i_fop = &simple_dir_operations; |
| 1559 | inode->i_op = &cgroup_dir_inode_operations; |
| 1560 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1561 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1562 | sb->s_root = d_make_root(inode); |
| 1563 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1565 | /* for everything else we want ->d_op set */ |
| 1566 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1567 | return 0; |
| 1568 | } |
| 1569 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1570 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1571 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1572 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1573 | { |
| 1574 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1575 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | int ret = 0; |
| 1577 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1578 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1579 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | |
| 1581 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1582 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1583 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1584 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1585 | if (ret) |
| 1586 | goto out_err; |
| 1587 | |
| 1588 | /* |
| 1589 | * Allocate a new cgroup root. We may not need it if we're |
| 1590 | * reusing an existing hierarchy. |
| 1591 | */ |
| 1592 | new_root = cgroup_root_from_opts(&opts); |
| 1593 | if (IS_ERR(new_root)) { |
| 1594 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1595 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1596 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1598 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1599 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1600 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1602 | ret = PTR_ERR(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1603 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1604 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1607 | root = sb->s_fs_info; |
| 1608 | BUG_ON(!root); |
| 1609 | if (root == opts.new_root) { |
| 1610 | /* We used the new root structure, so this is a new hierarchy */ |
| 1611 | struct list_head tmp_cg_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1612 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1613 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1614 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1615 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1616 | struct css_set *cg; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1617 | |
| 1618 | BUG_ON(sb->s_root != NULL); |
| 1619 | |
| 1620 | ret = cgroup_get_rootdir(sb); |
| 1621 | if (ret) |
| 1622 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1623 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1624 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1625 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1626 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1627 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1628 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1629 | /* Check for name clashes with existing mounts */ |
| 1630 | ret = -EBUSY; |
| 1631 | if (strlen(root->name)) |
| 1632 | for_each_active_root(existing_root) |
| 1633 | if (!strcmp(existing_root->name, root->name)) |
| 1634 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1635 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1636 | /* |
| 1637 | * We're accessing css_set_count without locking |
| 1638 | * css_set_lock here, but that's OK - it can only be |
| 1639 | * increased by someone holding cgroup_lock, and |
| 1640 | * that's us. The worst that can happen is that we |
| 1641 | * have some link structures left over |
| 1642 | */ |
| 1643 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1644 | if (ret) |
| 1645 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1646 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1647 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | if (ret == -EBUSY) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1649 | free_cg_links(&tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1650 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1652 | /* |
| 1653 | * There must be no failure case after here, since rebinding |
| 1654 | * takes care of subsystems' refcounts, which are explicitly |
| 1655 | * dropped in the failure exit path. |
| 1656 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | |
| 1658 | /* EBUSY should be the only error here */ |
| 1659 | BUG_ON(ret); |
| 1660 | |
| 1661 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1662 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1663 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1664 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | root->top_cgroup.dentry = sb->s_root; |
| 1666 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1667 | /* Link the top cgroup in this hierarchy into all |
| 1668 | * the css_set objects */ |
| 1669 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1670 | hash_for_each(css_set_table, i, cg, hlist) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 1671 | link_css_set(&tmp_cg_links, cg, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1672 | write_unlock(&css_set_lock); |
| 1673 | |
| 1674 | free_cg_links(&tmp_cg_links); |
| 1675 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1676 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1677 | BUG_ON(root->number_of_cgroups != 1); |
| 1678 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1679 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1680 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1681 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1682 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1683 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1684 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1685 | } else { |
| 1686 | /* |
| 1687 | * We re-used an existing hierarchy - the new root (if |
| 1688 | * any) is not needed |
| 1689 | */ |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1690 | cgroup_drop_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1691 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1692 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1695 | kfree(opts.release_agent); |
| 1696 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1697 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1698 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1699 | unlock_drop: |
| 1700 | mutex_unlock(&cgroup_root_mutex); |
| 1701 | mutex_unlock(&cgroup_mutex); |
| 1702 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1703 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1704 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1705 | drop_modules: |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1706 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1707 | out_err: |
| 1708 | kfree(opts.release_agent); |
| 1709 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1710 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1714 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1715 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1716 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1717 | struct cg_cgroup_link *link; |
| 1718 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | |
| 1720 | BUG_ON(!root); |
| 1721 | |
| 1722 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1723 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1724 | |
| 1725 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1726 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1727 | |
| 1728 | /* Rebind all subsystems back to the default hierarchy */ |
| 1729 | ret = rebind_subsystems(root, 0); |
| 1730 | /* Shouldn't be able to fail ... */ |
| 1731 | BUG_ON(ret); |
| 1732 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1733 | /* |
| 1734 | * Release all the links from css_sets to this hierarchy's |
| 1735 | * root cgroup |
| 1736 | */ |
| 1737 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1738 | |
| 1739 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1740 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1741 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1742 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1743 | kfree(link); |
| 1744 | } |
| 1745 | write_unlock(&css_set_lock); |
| 1746 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1747 | if (!list_empty(&root->root_list)) { |
| 1748 | list_del(&root->root_list); |
| 1749 | root_count--; |
| 1750 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1751 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1752 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1753 | mutex_unlock(&cgroup_mutex); |
| 1754 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1755 | simple_xattrs_free(&cgrp->xattrs); |
| 1756 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | kill_litter_super(sb); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1758 | cgroup_drop_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | static struct file_system_type cgroup_fs_type = { |
| 1762 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1763 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | .kill_sb = cgroup_kill_sb, |
| 1765 | }; |
| 1766 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1767 | static struct kobject *cgroup_kobj; |
| 1768 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1769 | /** |
| 1770 | * cgroup_path - generate the path of a cgroup |
| 1771 | * @cgrp: the cgroup in question |
| 1772 | * @buf: the buffer to write the path into |
| 1773 | * @buflen: the length of the buffer |
| 1774 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1775 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1776 | * |
| 1777 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1778 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1779 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1780 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1781 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1782 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1784 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1786 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1787 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1788 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1789 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1790 | rcu_read_lock(); |
| 1791 | while (cgrp) { |
| 1792 | const char *name = cgroup_name(cgrp); |
| 1793 | int len; |
| 1794 | |
| 1795 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1796 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1797 | goto out; |
| 1798 | memcpy(start, name, len); |
| 1799 | |
| 1800 | if (!cgrp->parent) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1801 | break; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1802 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1803 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1804 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1805 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1806 | |
| 1807 | cgrp = cgrp->parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | } |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1810 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1811 | out: |
| 1812 | rcu_read_unlock(); |
| 1813 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1814 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1815 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1816 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1817 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1818 | * Control Group taskset |
| 1819 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1820 | struct task_and_cgroup { |
| 1821 | struct task_struct *task; |
| 1822 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1823 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1824 | }; |
| 1825 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1826 | struct cgroup_taskset { |
| 1827 | struct task_and_cgroup single; |
| 1828 | struct flex_array *tc_array; |
| 1829 | int tc_array_len; |
| 1830 | int idx; |
| 1831 | struct cgroup *cur_cgrp; |
| 1832 | }; |
| 1833 | |
| 1834 | /** |
| 1835 | * cgroup_taskset_first - reset taskset and return the first task |
| 1836 | * @tset: taskset of interest |
| 1837 | * |
| 1838 | * @tset iteration is initialized and the first task is returned. |
| 1839 | */ |
| 1840 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1841 | { |
| 1842 | if (tset->tc_array) { |
| 1843 | tset->idx = 0; |
| 1844 | return cgroup_taskset_next(tset); |
| 1845 | } else { |
| 1846 | tset->cur_cgrp = tset->single.cgrp; |
| 1847 | return tset->single.task; |
| 1848 | } |
| 1849 | } |
| 1850 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1851 | |
| 1852 | /** |
| 1853 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1854 | * @tset: taskset of interest |
| 1855 | * |
| 1856 | * Return the next task in @tset. Iteration must have been initialized |
| 1857 | * with cgroup_taskset_first(). |
| 1858 | */ |
| 1859 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1860 | { |
| 1861 | struct task_and_cgroup *tc; |
| 1862 | |
| 1863 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1864 | return NULL; |
| 1865 | |
| 1866 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1867 | tset->cur_cgrp = tc->cgrp; |
| 1868 | return tc->task; |
| 1869 | } |
| 1870 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1871 | |
| 1872 | /** |
| 1873 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1874 | * @tset: taskset of interest |
| 1875 | * |
| 1876 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1877 | * function must be preceded by either cgroup_taskset_first() or |
| 1878 | * cgroup_taskset_next(). |
| 1879 | */ |
| 1880 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1881 | { |
| 1882 | return tset->cur_cgrp; |
| 1883 | } |
| 1884 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1885 | |
| 1886 | /** |
| 1887 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1888 | * @tset: taskset of interest |
| 1889 | */ |
| 1890 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1891 | { |
| 1892 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1893 | } |
| 1894 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1895 | |
| 1896 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1897 | /* |
| 1898 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1899 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1900 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1901 | */ |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 1902 | static void cgroup_task_migrate(struct cgroup *oldcgrp, |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1903 | struct task_struct *tsk, struct css_set *newcg) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1904 | { |
| 1905 | struct css_set *oldcg; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1906 | |
| 1907 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1908 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1909 | * setting such that we can't race against cgroup_exit() changing the |
| 1910 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1911 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1912 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1913 | oldcg = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1914 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1915 | task_lock(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1916 | rcu_assign_pointer(tsk->cgroups, newcg); |
| 1917 | task_unlock(tsk); |
| 1918 | |
| 1919 | /* Update the css_set linked lists if we're using them */ |
| 1920 | write_lock(&css_set_lock); |
| 1921 | if (!list_empty(&tsk->cg_list)) |
| 1922 | list_move(&tsk->cg_list, &newcg->tasks); |
| 1923 | write_unlock(&css_set_lock); |
| 1924 | |
| 1925 | /* |
| 1926 | * We just gained a reference on oldcg by taking it from the task. As |
| 1927 | * trading it for newcg is protected by cgroup_mutex, we're safe to drop |
| 1928 | * it here; it will be freed under RCU. |
| 1929 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1930 | set_bit(CGRP_RELEASABLE, &oldcgrp->flags); |
Daisuke Nishimura | 1f5320d | 2012-10-04 16:37:16 +0900 | [diff] [blame] | 1931 | put_css_set(oldcg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1934 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1935 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1937 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1938 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1939 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1940 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1941 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1943 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1944 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1945 | { |
| 1946 | int retval, i, group_size; |
| 1947 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1948 | struct cgroupfs_root *root = cgrp->root; |
| 1949 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1950 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1951 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1952 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1953 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1954 | |
| 1955 | /* |
| 1956 | * step 0: in order to do expensive, possibly blocking operations for |
| 1957 | * every thread, we cannot iterate the thread group list, since it needs |
| 1958 | * 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] | 1959 | * group - group_rwsem prevents new threads from appearing, and if |
| 1960 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1962 | if (threadgroup) |
| 1963 | group_size = get_nr_threads(tsk); |
| 1964 | else |
| 1965 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1966 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1967 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1968 | if (!group) |
| 1969 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1970 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 1971 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1972 | if (retval) |
| 1973 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 1976 | /* |
| 1977 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 1978 | * already PF_EXITING could be freed from underneath us unless we |
| 1979 | * take an rcu_read_lock. |
| 1980 | */ |
| 1981 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1982 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1983 | struct task_and_cgroup ent; |
| 1984 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1985 | /* @tsk either already exited or can't exit until the end */ |
| 1986 | if (tsk->flags & PF_EXITING) |
| 1987 | continue; |
| 1988 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1989 | /* as per above, nr_threads may decrease, but not increase. */ |
| 1990 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1991 | ent.task = tsk; |
| 1992 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 1993 | /* nothing to do if this task is already in the cgroup */ |
| 1994 | if (ent.cgrp == cgrp) |
| 1995 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1996 | /* |
| 1997 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 1998 | * earlier, but it's good form to communicate our expectations. |
| 1999 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2000 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2001 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2002 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2003 | |
| 2004 | if (!threadgroup) |
| 2005 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2007 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2008 | /* remember the number of threads in the array for later. */ |
| 2009 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2010 | tset.tc_array = group; |
| 2011 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2013 | /* methods shouldn't be called if no task is actually migrating */ |
| 2014 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2015 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2016 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2017 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2018 | /* |
| 2019 | * step 1: check that we can legitimately attach to the cgroup. |
| 2020 | */ |
| 2021 | for_each_subsys(root, ss) { |
| 2022 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2023 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2024 | if (retval) { |
| 2025 | failed_ss = ss; |
| 2026 | goto out_cancel_attach; |
| 2027 | } |
| 2028 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2033 | * we use find_css_set, which allocates a new one if necessary. |
| 2034 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2035 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2036 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2037 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2038 | if (!tc->cg) { |
| 2039 | retval = -ENOMEM; |
| 2040 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2045 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2046 | * proceed to move all tasks to the new cgroup. There are no |
| 2047 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2050 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2051 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2052 | } |
| 2053 | /* nothing is sensitive to fork() after this point. */ |
| 2054 | |
| 2055 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2056 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2057 | */ |
| 2058 | for_each_subsys(root, ss) { |
| 2059 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2060 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * step 5: success! and cleanup |
| 2065 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2066 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2067 | out_put_css_set_refs: |
| 2068 | if (retval) { |
| 2069 | for (i = 0; i < group_size; i++) { |
| 2070 | tc = flex_array_get(group, i); |
| 2071 | if (!tc->cg) |
| 2072 | break; |
| 2073 | put_css_set(tc->cg); |
| 2074 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | } |
| 2076 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2077 | if (retval) { |
| 2078 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2079 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2080 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2081 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2082 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2083 | } |
| 2084 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2086 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2087 | return retval; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * 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] | 2092 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2093 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2094 | */ |
| 2095 | 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] | 2096 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2097 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2098 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2099 | int ret; |
| 2100 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | if (!cgroup_lock_live_group(cgrp)) |
| 2102 | return -ENODEV; |
| 2103 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2104 | retry_find_task: |
| 2105 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2106 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2107 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2109 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2110 | ret= -ESRCH; |
| 2111 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2112 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2113 | /* |
| 2114 | * even if we're attaching all tasks in the thread group, we |
| 2115 | * only need to check permissions on one of them. |
| 2116 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2117 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2118 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2119 | !uid_eq(cred->euid, tcred->uid) && |
| 2120 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2121 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2122 | ret = -EACCES; |
| 2123 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2124 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2125 | } else |
| 2126 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2127 | |
| 2128 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2129 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2130 | |
| 2131 | /* |
| 2132 | * Workqueue threads may acquire PF_THREAD_BOUND and become |
| 2133 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2134 | * with no rt_runtime allocated. Just say no. |
| 2135 | */ |
| 2136 | if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) { |
| 2137 | ret = -EINVAL; |
| 2138 | rcu_read_unlock(); |
| 2139 | goto out_unlock_cgroup; |
| 2140 | } |
| 2141 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2142 | get_task_struct(tsk); |
| 2143 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2144 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2145 | threadgroup_lock(tsk); |
| 2146 | if (threadgroup) { |
| 2147 | if (!thread_group_leader(tsk)) { |
| 2148 | /* |
| 2149 | * a race with de_thread from another thread's exec() |
| 2150 | * may strip us of our leadership, if this happens, |
| 2151 | * there is no choice but to throw this task away and |
| 2152 | * try again; this is |
| 2153 | * "double-double-toil-and-trouble-check locking". |
| 2154 | */ |
| 2155 | threadgroup_unlock(tsk); |
| 2156 | put_task_struct(tsk); |
| 2157 | goto retry_find_task; |
| 2158 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2162 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2163 | threadgroup_unlock(tsk); |
| 2164 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2165 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2166 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2167 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2168 | return ret; |
| 2169 | } |
| 2170 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2171 | /** |
| 2172 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2173 | * @from: attach to all cgroups of a given task |
| 2174 | * @tsk: the task to be attached |
| 2175 | */ |
| 2176 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2177 | { |
| 2178 | struct cgroupfs_root *root; |
| 2179 | int retval = 0; |
| 2180 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2181 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2182 | for_each_active_root(root) { |
| 2183 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2184 | |
| 2185 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2186 | if (retval) |
| 2187 | break; |
| 2188 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2189 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2190 | |
| 2191 | return retval; |
| 2192 | } |
| 2193 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2194 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2195 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2196 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2197 | return attach_task_by_pid(cgrp, pid, false); |
| 2198 | } |
| 2199 | |
| 2200 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2201 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2202 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2205 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2206 | const char *buffer) |
| 2207 | { |
| 2208 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2209 | if (strlen(buffer) >= PATH_MAX) |
| 2210 | return -EINVAL; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2211 | if (!cgroup_lock_live_group(cgrp)) |
| 2212 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2213 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2214 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2215 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2216 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2221 | struct seq_file *seq) |
| 2222 | { |
| 2223 | if (!cgroup_lock_live_group(cgrp)) |
| 2224 | return -ENODEV; |
| 2225 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2226 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2227 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2228 | return 0; |
| 2229 | } |
| 2230 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2231 | /* A buffer size big enough for numbers or short strings */ |
| 2232 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2233 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2234 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2235 | struct file *file, |
| 2236 | const char __user *userbuf, |
| 2237 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2238 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2239 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2240 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2241 | char *end; |
| 2242 | |
| 2243 | if (!nbytes) |
| 2244 | return -EINVAL; |
| 2245 | if (nbytes >= sizeof(buffer)) |
| 2246 | return -E2BIG; |
| 2247 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2248 | return -EFAULT; |
| 2249 | |
| 2250 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2251 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2252 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2253 | if (*end) |
| 2254 | return -EINVAL; |
| 2255 | retval = cft->write_u64(cgrp, cft, val); |
| 2256 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2257 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2258 | if (*end) |
| 2259 | return -EINVAL; |
| 2260 | retval = cft->write_s64(cgrp, cft, val); |
| 2261 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2262 | if (!retval) |
| 2263 | retval = nbytes; |
| 2264 | return retval; |
| 2265 | } |
| 2266 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2267 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2268 | struct file *file, |
| 2269 | const char __user *userbuf, |
| 2270 | size_t nbytes, loff_t *unused_ppos) |
| 2271 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2272 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2273 | int retval = 0; |
| 2274 | size_t max_bytes = cft->max_write_len; |
| 2275 | char *buffer = local_buffer; |
| 2276 | |
| 2277 | if (!max_bytes) |
| 2278 | max_bytes = sizeof(local_buffer) - 1; |
| 2279 | if (nbytes >= max_bytes) |
| 2280 | return -E2BIG; |
| 2281 | /* Allocate a dynamic buffer if we need one */ |
| 2282 | if (nbytes >= sizeof(local_buffer)) { |
| 2283 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2284 | if (buffer == NULL) |
| 2285 | return -ENOMEM; |
| 2286 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2287 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2288 | retval = -EFAULT; |
| 2289 | goto out; |
| 2290 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2291 | |
| 2292 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2293 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2294 | if (!retval) |
| 2295 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2296 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2297 | if (buffer != local_buffer) |
| 2298 | kfree(buffer); |
| 2299 | return retval; |
| 2300 | } |
| 2301 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2302 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2303 | size_t nbytes, loff_t *ppos) |
| 2304 | { |
| 2305 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2306 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2307 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2308 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2309 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2310 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2311 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2312 | if (cft->write_u64 || cft->write_s64) |
| 2313 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2314 | if (cft->write_string) |
| 2315 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2316 | if (cft->trigger) { |
| 2317 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2318 | return ret ? ret : nbytes; |
| 2319 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2320 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2323 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2324 | struct file *file, |
| 2325 | char __user *buf, size_t nbytes, |
| 2326 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2327 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2328 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2329 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2330 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2331 | |
| 2332 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2333 | } |
| 2334 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2335 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2336 | struct file *file, |
| 2337 | char __user *buf, size_t nbytes, |
| 2338 | loff_t *ppos) |
| 2339 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2340 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2341 | s64 val = cft->read_s64(cgrp, cft); |
| 2342 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2343 | |
| 2344 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2345 | } |
| 2346 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2347 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2348 | size_t nbytes, loff_t *ppos) |
| 2349 | { |
| 2350 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2351 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2352 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2353 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2354 | return -ENODEV; |
| 2355 | |
| 2356 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2357 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2358 | if (cft->read_u64) |
| 2359 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2360 | if (cft->read_s64) |
| 2361 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2362 | return -EINVAL; |
| 2363 | } |
| 2364 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2365 | /* |
| 2366 | * seqfile ops/methods for returning structured data. Currently just |
| 2367 | * supports string->u64 maps, but can be extended in future. |
| 2368 | */ |
| 2369 | |
| 2370 | struct cgroup_seqfile_state { |
| 2371 | struct cftype *cft; |
| 2372 | struct cgroup *cgroup; |
| 2373 | }; |
| 2374 | |
| 2375 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2376 | { |
| 2377 | struct seq_file *sf = cb->state; |
| 2378 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2379 | } |
| 2380 | |
| 2381 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2382 | { |
| 2383 | struct cgroup_seqfile_state *state = m->private; |
| 2384 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2385 | if (cft->read_map) { |
| 2386 | struct cgroup_map_cb cb = { |
| 2387 | .fill = cgroup_map_add, |
| 2388 | .state = m, |
| 2389 | }; |
| 2390 | return cft->read_map(state->cgroup, cft, &cb); |
| 2391 | } |
| 2392 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2395 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2396 | { |
| 2397 | struct seq_file *seq = file->private_data; |
| 2398 | kfree(seq->private); |
| 2399 | return single_release(inode, file); |
| 2400 | } |
| 2401 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2402 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2403 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2404 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2405 | .llseek = seq_lseek, |
| 2406 | .release = cgroup_seqfile_release, |
| 2407 | }; |
| 2408 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2409 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2410 | { |
| 2411 | int err; |
| 2412 | struct cftype *cft; |
| 2413 | |
| 2414 | err = generic_file_open(inode, file); |
| 2415 | if (err) |
| 2416 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2417 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2418 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2419 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2420 | struct cgroup_seqfile_state *state = |
| 2421 | kzalloc(sizeof(*state), GFP_USER); |
| 2422 | if (!state) |
| 2423 | return -ENOMEM; |
| 2424 | state->cft = cft; |
| 2425 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2426 | file->f_op = &cgroup_seqfile_operations; |
| 2427 | err = single_open(file, cgroup_seqfile_show, state); |
| 2428 | if (err < 0) |
| 2429 | kfree(state); |
| 2430 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2431 | err = cft->open(inode, file); |
| 2432 | else |
| 2433 | err = 0; |
| 2434 | |
| 2435 | return err; |
| 2436 | } |
| 2437 | |
| 2438 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2439 | { |
| 2440 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2441 | if (cft->release) |
| 2442 | return cft->release(inode, file); |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2448 | */ |
| 2449 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2450 | struct inode *new_dir, struct dentry *new_dentry) |
| 2451 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2452 | int ret; |
| 2453 | struct cgroup_name *name, *old_name; |
| 2454 | struct cgroup *cgrp; |
| 2455 | |
| 2456 | /* |
| 2457 | * It's convinient to use parent dir's i_mutex to protected |
| 2458 | * cgrp->name. |
| 2459 | */ |
| 2460 | lockdep_assert_held(&old_dir->i_mutex); |
| 2461 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2462 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2463 | return -ENOTDIR; |
| 2464 | if (new_dentry->d_inode) |
| 2465 | return -EEXIST; |
| 2466 | if (old_dir != new_dir) |
| 2467 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2468 | |
| 2469 | cgrp = __d_cgrp(old_dentry); |
| 2470 | |
| 2471 | name = cgroup_alloc_name(new_dentry); |
| 2472 | if (!name) |
| 2473 | return -ENOMEM; |
| 2474 | |
| 2475 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2476 | if (ret) { |
| 2477 | kfree(name); |
| 2478 | return ret; |
| 2479 | } |
| 2480 | |
| 2481 | old_name = cgrp->name; |
| 2482 | rcu_assign_pointer(cgrp->name, name); |
| 2483 | |
| 2484 | kfree_rcu(old_name, rcu_head); |
| 2485 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2488 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2489 | { |
| 2490 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2491 | return &__d_cgrp(dentry)->xattrs; |
| 2492 | else |
| 2493 | return &__d_cft(dentry)->xattrs; |
| 2494 | } |
| 2495 | |
| 2496 | static inline int xattr_enabled(struct dentry *dentry) |
| 2497 | { |
| 2498 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
| 2499 | return test_bit(ROOT_XATTR, &root->flags); |
| 2500 | } |
| 2501 | |
| 2502 | static bool is_valid_xattr(const char *name) |
| 2503 | { |
| 2504 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2505 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2506 | return true; |
| 2507 | return false; |
| 2508 | } |
| 2509 | |
| 2510 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2511 | const void *val, size_t size, int flags) |
| 2512 | { |
| 2513 | if (!xattr_enabled(dentry)) |
| 2514 | return -EOPNOTSUPP; |
| 2515 | if (!is_valid_xattr(name)) |
| 2516 | return -EINVAL; |
| 2517 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2518 | } |
| 2519 | |
| 2520 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2521 | { |
| 2522 | if (!xattr_enabled(dentry)) |
| 2523 | return -EOPNOTSUPP; |
| 2524 | if (!is_valid_xattr(name)) |
| 2525 | return -EINVAL; |
| 2526 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2527 | } |
| 2528 | |
| 2529 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2530 | void *buf, size_t size) |
| 2531 | { |
| 2532 | if (!xattr_enabled(dentry)) |
| 2533 | return -EOPNOTSUPP; |
| 2534 | if (!is_valid_xattr(name)) |
| 2535 | return -EINVAL; |
| 2536 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2537 | } |
| 2538 | |
| 2539 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2540 | { |
| 2541 | if (!xattr_enabled(dentry)) |
| 2542 | return -EOPNOTSUPP; |
| 2543 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2544 | } |
| 2545 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2546 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2547 | .read = cgroup_file_read, |
| 2548 | .write = cgroup_file_write, |
| 2549 | .llseek = generic_file_llseek, |
| 2550 | .open = cgroup_file_open, |
| 2551 | .release = cgroup_file_release, |
| 2552 | }; |
| 2553 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2554 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2555 | .setxattr = cgroup_setxattr, |
| 2556 | .getxattr = cgroup_getxattr, |
| 2557 | .listxattr = cgroup_listxattr, |
| 2558 | .removexattr = cgroup_removexattr, |
| 2559 | }; |
| 2560 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2561 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2562 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2563 | .mkdir = cgroup_mkdir, |
| 2564 | .rmdir = cgroup_rmdir, |
| 2565 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2566 | .setxattr = cgroup_setxattr, |
| 2567 | .getxattr = cgroup_getxattr, |
| 2568 | .listxattr = cgroup_listxattr, |
| 2569 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2570 | }; |
| 2571 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2572 | 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] | 2573 | { |
| 2574 | if (dentry->d_name.len > NAME_MAX) |
| 2575 | return ERR_PTR(-ENAMETOOLONG); |
| 2576 | d_add(dentry, NULL); |
| 2577 | return NULL; |
| 2578 | } |
| 2579 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2580 | /* |
| 2581 | * Check if a file is a control file |
| 2582 | */ |
| 2583 | static inline struct cftype *__file_cft(struct file *file) |
| 2584 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2585 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2586 | return ERR_PTR(-EINVAL); |
| 2587 | return __d_cft(file->f_dentry); |
| 2588 | } |
| 2589 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2590 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2591 | struct super_block *sb) |
| 2592 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2593 | struct inode *inode; |
| 2594 | |
| 2595 | if (!dentry) |
| 2596 | return -ENOENT; |
| 2597 | if (dentry->d_inode) |
| 2598 | return -EEXIST; |
| 2599 | |
| 2600 | inode = cgroup_new_inode(mode, sb); |
| 2601 | if (!inode) |
| 2602 | return -ENOMEM; |
| 2603 | |
| 2604 | if (S_ISDIR(mode)) { |
| 2605 | inode->i_op = &cgroup_dir_inode_operations; |
| 2606 | inode->i_fop = &simple_dir_operations; |
| 2607 | |
| 2608 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2609 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2610 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2611 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2612 | /* |
| 2613 | * Control reaches here with cgroup_mutex held. |
| 2614 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2615 | * want to populate it immediately without releasing |
| 2616 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2617 | * yet, trylock will always succeed without affecting |
| 2618 | * lockdep checks. |
| 2619 | */ |
| 2620 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2621 | } else if (S_ISREG(mode)) { |
| 2622 | inode->i_size = 0; |
| 2623 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2624 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2625 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2626 | d_instantiate(dentry, inode); |
| 2627 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2631 | /** |
| 2632 | * cgroup_file_mode - deduce file mode of a control file |
| 2633 | * @cft: the control file in question |
| 2634 | * |
| 2635 | * returns cft->mode if ->mode is not 0 |
| 2636 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2637 | * returns S_IRUGO if it has only a read handler |
| 2638 | * returns S_IWUSR if it has only a write hander |
| 2639 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2640 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2641 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2642 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2643 | |
| 2644 | if (cft->mode) |
| 2645 | return cft->mode; |
| 2646 | |
| 2647 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2648 | cft->read_map || cft->read_seq_string) |
| 2649 | mode |= S_IRUGO; |
| 2650 | |
| 2651 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2652 | cft->write_string || cft->trigger) |
| 2653 | mode |= S_IWUSR; |
| 2654 | |
| 2655 | return mode; |
| 2656 | } |
| 2657 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2658 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2659 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2660 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2661 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2662 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2663 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2664 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2665 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2666 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2667 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2668 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2669 | simple_xattrs_init(&cft->xattrs); |
| 2670 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2671 | if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2672 | strcpy(name, subsys->name); |
| 2673 | strcat(name, "."); |
| 2674 | } |
| 2675 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2676 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2677 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2678 | |
| 2679 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2680 | if (!cfe) |
| 2681 | return -ENOMEM; |
| 2682 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2683 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2684 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2685 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2686 | goto out; |
| 2687 | } |
| 2688 | |
| 2689 | mode = cgroup_file_mode(cft); |
| 2690 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2691 | if (!error) { |
| 2692 | cfe->type = (void *)cft; |
| 2693 | cfe->dentry = dentry; |
| 2694 | dentry->d_fsdata = cfe; |
| 2695 | list_add_tail(&cfe->node, &parent->files); |
| 2696 | cfe = NULL; |
| 2697 | } |
| 2698 | dput(dentry); |
| 2699 | out: |
| 2700 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2701 | return error; |
| 2702 | } |
| 2703 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2704 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2705 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2706 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2707 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2708 | int err, ret = 0; |
| 2709 | |
| 2710 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2711 | /* does cft->flags tell us to skip this file on @cgrp? */ |
| 2712 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2713 | continue; |
| 2714 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2715 | continue; |
| 2716 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2717 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2718 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2719 | if (err) |
| 2720 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2721 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2723 | } else { |
| 2724 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2725 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2726 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2727 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2730 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2731 | |
| 2732 | static void cgroup_cfts_prepare(void) |
| 2733 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2734 | { |
| 2735 | /* |
| 2736 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2737 | * the existing cgroups under cgroup_mutex and create files. |
| 2738 | * Instead, we increment reference on all cgroups and build list of |
| 2739 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2740 | * exclusive access to the field. |
| 2741 | */ |
| 2742 | mutex_lock(&cgroup_cft_mutex); |
| 2743 | mutex_lock(&cgroup_mutex); |
| 2744 | } |
| 2745 | |
| 2746 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2747 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2748 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2749 | { |
| 2750 | LIST_HEAD(pending); |
| 2751 | struct cgroup *cgrp, *n; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2752 | |
| 2753 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
| 2754 | if (cfts && ss->root != &rootnode) { |
| 2755 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2756 | dget(cgrp->dentry); |
| 2757 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | mutex_unlock(&cgroup_mutex); |
| 2762 | |
| 2763 | /* |
| 2764 | * All new cgroups will see @cfts update on @ss->cftsets. Add/rm |
| 2765 | * files for all cgroups which were created before. |
| 2766 | */ |
| 2767 | list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { |
| 2768 | struct inode *inode = cgrp->dentry->d_inode; |
| 2769 | |
| 2770 | mutex_lock(&inode->i_mutex); |
| 2771 | mutex_lock(&cgroup_mutex); |
| 2772 | if (!cgroup_is_removed(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2773 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2774 | mutex_unlock(&cgroup_mutex); |
| 2775 | mutex_unlock(&inode->i_mutex); |
| 2776 | |
| 2777 | list_del_init(&cgrp->cft_q_node); |
| 2778 | dput(cgrp->dentry); |
| 2779 | } |
| 2780 | |
| 2781 | mutex_unlock(&cgroup_cft_mutex); |
| 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2786 | * @ss: target cgroup subsystem |
| 2787 | * @cfts: zero-length name terminated array of cftypes |
| 2788 | * |
| 2789 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2790 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2791 | * have them too. This function can be called anytime whether @ss is |
| 2792 | * attached or not. |
| 2793 | * |
| 2794 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2795 | * function currently returns 0 as long as @cfts registration is successful |
| 2796 | * even if some file creation attempts on existing cgroups fail. |
| 2797 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2798 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2799 | { |
| 2800 | struct cftype_set *set; |
| 2801 | |
| 2802 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2803 | if (!set) |
| 2804 | return -ENOMEM; |
| 2805 | |
| 2806 | cgroup_cfts_prepare(); |
| 2807 | set->cfts = cfts; |
| 2808 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2809 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2810 | |
| 2811 | return 0; |
| 2812 | } |
| 2813 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2814 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2815 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2816 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2817 | * @ss: target cgroup subsystem |
| 2818 | * @cfts: zero-length name terminated array of cftypes |
| 2819 | * |
| 2820 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2821 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2822 | * won't have them either. This function can be called anytime whether @ss |
| 2823 | * is attached or not. |
| 2824 | * |
| 2825 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2826 | * registered with @ss. |
| 2827 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2828 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2829 | { |
| 2830 | struct cftype_set *set; |
| 2831 | |
| 2832 | cgroup_cfts_prepare(); |
| 2833 | |
| 2834 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2835 | if (set->cfts == cfts) { |
| 2836 | list_del_init(&set->node); |
| 2837 | cgroup_cfts_commit(ss, cfts, false); |
| 2838 | return 0; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | cgroup_cfts_commit(ss, NULL, false); |
| 2843 | return -ENOENT; |
| 2844 | } |
| 2845 | |
| 2846 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2847 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2848 | * @cgrp: the cgroup in question |
| 2849 | * |
| 2850 | * Return the number of tasks in the cgroup. |
| 2851 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2852 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2853 | { |
| 2854 | int count = 0; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2855 | struct cg_cgroup_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2856 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2857 | read_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2858 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2859 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2860 | } |
| 2861 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2862 | return count; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2866 | * Advance a list_head iterator. The iterator should be positioned at |
| 2867 | * the start of a css_set |
| 2868 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2869 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2870 | struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2871 | { |
| 2872 | struct list_head *l = it->cg_link; |
| 2873 | struct cg_cgroup_link *link; |
| 2874 | struct css_set *cg; |
| 2875 | |
| 2876 | /* Advance to the next non-empty css_set */ |
| 2877 | do { |
| 2878 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2879 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2880 | it->cg_link = NULL; |
| 2881 | return; |
| 2882 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2883 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2884 | cg = link->cg; |
| 2885 | } while (list_empty(&cg->tasks)); |
| 2886 | it->cg_link = l; |
| 2887 | it->task = cg->tasks.next; |
| 2888 | } |
| 2889 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2890 | /* |
| 2891 | * To reduce the fork() overhead for systems that are not actually |
| 2892 | * using their cgroups capability, we don't maintain the lists running |
| 2893 | * through each css_set to its tasks until we see the list actually |
| 2894 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2895 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2896 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2897 | { |
| 2898 | struct task_struct *p, *g; |
| 2899 | write_lock(&css_set_lock); |
| 2900 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2901 | /* |
| 2902 | * We need tasklist_lock because RCU is not safe against |
| 2903 | * while_each_thread(). Besides, a forking task that has passed |
| 2904 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2905 | * is not guaranteed to have its child immediately visible in the |
| 2906 | * tasklist if we walk through it with RCU. |
| 2907 | */ |
| 2908 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2909 | do_each_thread(g, p) { |
| 2910 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2911 | /* |
| 2912 | * We should check if the process is exiting, otherwise |
| 2913 | * it will race with cgroup_exit() in that the list |
| 2914 | * entry won't be deleted though the process has exited. |
| 2915 | */ |
| 2916 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2917 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2918 | task_unlock(p); |
| 2919 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2920 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2921 | write_unlock(&css_set_lock); |
| 2922 | } |
| 2923 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2924 | /** |
| 2925 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 2926 | * @pos: the current position (%NULL to initiate traversal) |
| 2927 | * @cgroup: cgroup whose descendants to walk |
| 2928 | * |
| 2929 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 2930 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
| 2931 | */ |
| 2932 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 2933 | struct cgroup *cgroup) |
| 2934 | { |
| 2935 | struct cgroup *next; |
| 2936 | |
| 2937 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2938 | |
| 2939 | /* if first iteration, pretend we just visited @cgroup */ |
| 2940 | if (!pos) { |
| 2941 | if (list_empty(&cgroup->children)) |
| 2942 | return NULL; |
| 2943 | pos = cgroup; |
| 2944 | } |
| 2945 | |
| 2946 | /* visit the first child if exists */ |
| 2947 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 2948 | if (next) |
| 2949 | return next; |
| 2950 | |
| 2951 | /* no child, visit my or the closest ancestor's next sibling */ |
| 2952 | do { |
| 2953 | next = list_entry_rcu(pos->sibling.next, struct cgroup, |
| 2954 | sibling); |
| 2955 | if (&next->sibling != &pos->parent->children) |
| 2956 | return next; |
| 2957 | |
| 2958 | pos = pos->parent; |
| 2959 | } while (pos != cgroup); |
| 2960 | |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 2964 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 2965 | /** |
| 2966 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 2967 | * @pos: cgroup of interest |
| 2968 | * |
| 2969 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 2970 | * @pos is returned. This can be used during pre-order traversal to skip |
| 2971 | * subtree of @pos. |
| 2972 | */ |
| 2973 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 2974 | { |
| 2975 | struct cgroup *last, *tmp; |
| 2976 | |
| 2977 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2978 | |
| 2979 | do { |
| 2980 | last = pos; |
| 2981 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 2982 | pos = NULL; |
| 2983 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 2984 | pos = tmp; |
| 2985 | } while (pos); |
| 2986 | |
| 2987 | return last; |
| 2988 | } |
| 2989 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 2990 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2991 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 2992 | { |
| 2993 | struct cgroup *last; |
| 2994 | |
| 2995 | do { |
| 2996 | last = pos; |
| 2997 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 2998 | sibling); |
| 2999 | } while (pos); |
| 3000 | |
| 3001 | return last; |
| 3002 | } |
| 3003 | |
| 3004 | /** |
| 3005 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3006 | * @pos: the current position (%NULL to initiate traversal) |
| 3007 | * @cgroup: cgroup whose descendants to walk |
| 3008 | * |
| 3009 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3010 | * descendant to visit for post-order traversal of @cgroup's descendants. |
| 3011 | */ |
| 3012 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3013 | struct cgroup *cgroup) |
| 3014 | { |
| 3015 | struct cgroup *next; |
| 3016 | |
| 3017 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3018 | |
| 3019 | /* if first iteration, visit the leftmost descendant */ |
| 3020 | if (!pos) { |
| 3021 | next = cgroup_leftmost_descendant(cgroup); |
| 3022 | return next != cgroup ? next : NULL; |
| 3023 | } |
| 3024 | |
| 3025 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 3026 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3027 | if (&next->sibling != &pos->parent->children) |
| 3028 | return cgroup_leftmost_descendant(next); |
| 3029 | |
| 3030 | /* no sibling left, visit parent */ |
| 3031 | next = pos->parent; |
| 3032 | return next != cgroup ? next : NULL; |
| 3033 | } |
| 3034 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3035 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3036 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3037 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3038 | { |
| 3039 | /* |
| 3040 | * The first time anyone tries to iterate across a cgroup, |
| 3041 | * we need to enable the list linking each css_set to its |
| 3042 | * tasks, and fix up all existing tasks. |
| 3043 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3044 | if (!use_task_css_set_links) |
| 3045 | cgroup_enable_task_cg_lists(); |
| 3046 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3047 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3048 | it->cg_link = &cgrp->css_sets; |
| 3049 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3050 | } |
| 3051 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3052 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3053 | struct cgroup_iter *it) |
| 3054 | { |
| 3055 | struct task_struct *res; |
| 3056 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3057 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3058 | |
| 3059 | /* If the iterator cg is NULL, we have no tasks */ |
| 3060 | if (!it->cg_link) |
| 3061 | return NULL; |
| 3062 | res = list_entry(l, struct task_struct, cg_list); |
| 3063 | /* Advance iterator to find next entry */ |
| 3064 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3065 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 3066 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3067 | /* We reached the end of this task list - move on to |
| 3068 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3069 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3070 | } else { |
| 3071 | it->task = l; |
| 3072 | } |
| 3073 | return res; |
| 3074 | } |
| 3075 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3076 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3077 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3078 | { |
| 3079 | read_unlock(&css_set_lock); |
| 3080 | } |
| 3081 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3082 | static inline int started_after_time(struct task_struct *t1, |
| 3083 | struct timespec *time, |
| 3084 | struct task_struct *t2) |
| 3085 | { |
| 3086 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3087 | if (start_diff > 0) { |
| 3088 | return 1; |
| 3089 | } else if (start_diff < 0) { |
| 3090 | return 0; |
| 3091 | } else { |
| 3092 | /* |
| 3093 | * Arbitrarily, if two processes started at the same |
| 3094 | * time, we'll say that the lower pointer value |
| 3095 | * started first. Note that t2 may have exited by now |
| 3096 | * so this may not be a valid pointer any longer, but |
| 3097 | * that's fine - it still serves to distinguish |
| 3098 | * between two tasks started (effectively) simultaneously. |
| 3099 | */ |
| 3100 | return t1 > t2; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /* |
| 3105 | * This function is a callback from heap_insert() and is used to order |
| 3106 | * the heap. |
| 3107 | * In this case we order the heap in descending task start time. |
| 3108 | */ |
| 3109 | static inline int started_after(void *p1, void *p2) |
| 3110 | { |
| 3111 | struct task_struct *t1 = p1; |
| 3112 | struct task_struct *t2 = p2; |
| 3113 | return started_after_time(t1, &t2->start_time, t2); |
| 3114 | } |
| 3115 | |
| 3116 | /** |
| 3117 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3118 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3119 | * |
| 3120 | * Arguments include pointers to callback functions test_task() and |
| 3121 | * process_task(). |
| 3122 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3123 | * and if it returns true, call process_task() for it also. |
| 3124 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3125 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3126 | * but does not lock css_set_lock for the call to process_task(). |
| 3127 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3128 | * creation. |
| 3129 | * It is guaranteed that process_task() will act on every task that |
| 3130 | * is a member of the cgroup for the duration of this call. This |
| 3131 | * function may or may not call process_task() for tasks that exit |
| 3132 | * or move to a different cgroup during the call, or are forked or |
| 3133 | * move into the cgroup during the call. |
| 3134 | * |
| 3135 | * Note that test_task() may be called with locks held, and may in some |
| 3136 | * situations be called multiple times for the same task, so it should |
| 3137 | * be cheap. |
| 3138 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3139 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3140 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3141 | * may cause this function to fail). |
| 3142 | */ |
| 3143 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3144 | { |
| 3145 | int retval, i; |
| 3146 | struct cgroup_iter it; |
| 3147 | struct task_struct *p, *dropped; |
| 3148 | /* Never dereference latest_task, since it's not refcounted */ |
| 3149 | struct task_struct *latest_task = NULL; |
| 3150 | struct ptr_heap tmp_heap; |
| 3151 | struct ptr_heap *heap; |
| 3152 | struct timespec latest_time = { 0, 0 }; |
| 3153 | |
| 3154 | if (scan->heap) { |
| 3155 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3156 | heap = scan->heap; |
| 3157 | heap->gt = &started_after; |
| 3158 | } else { |
| 3159 | /* We need to allocate our own heap memory */ |
| 3160 | heap = &tmp_heap; |
| 3161 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3162 | if (retval) |
| 3163 | /* cannot allocate the heap */ |
| 3164 | return retval; |
| 3165 | } |
| 3166 | |
| 3167 | again: |
| 3168 | /* |
| 3169 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3170 | * to determine which are of interest, and using the scanner's |
| 3171 | * "process_task" callback to process any of them that need an update. |
| 3172 | * Since we don't want to hold any locks during the task updates, |
| 3173 | * gather tasks to be processed in a heap structure. |
| 3174 | * The heap is sorted by descending task start time. |
| 3175 | * If the statically-sized heap fills up, we overflow tasks that |
| 3176 | * started later, and in future iterations only consider tasks that |
| 3177 | * started after the latest task in the previous pass. This |
| 3178 | * guarantees forward progress and that we don't miss any tasks. |
| 3179 | */ |
| 3180 | heap->size = 0; |
| 3181 | cgroup_iter_start(scan->cg, &it); |
| 3182 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3183 | /* |
| 3184 | * Only affect tasks that qualify per the caller's callback, |
| 3185 | * if he provided one |
| 3186 | */ |
| 3187 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3188 | continue; |
| 3189 | /* |
| 3190 | * Only process tasks that started after the last task |
| 3191 | * we processed |
| 3192 | */ |
| 3193 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3194 | continue; |
| 3195 | dropped = heap_insert(heap, p); |
| 3196 | if (dropped == NULL) { |
| 3197 | /* |
| 3198 | * The new task was inserted; the heap wasn't |
| 3199 | * previously full |
| 3200 | */ |
| 3201 | get_task_struct(p); |
| 3202 | } else if (dropped != p) { |
| 3203 | /* |
| 3204 | * The new task was inserted, and pushed out a |
| 3205 | * different task |
| 3206 | */ |
| 3207 | get_task_struct(p); |
| 3208 | put_task_struct(dropped); |
| 3209 | } |
| 3210 | /* |
| 3211 | * Else the new task was newer than anything already in |
| 3212 | * the heap and wasn't inserted |
| 3213 | */ |
| 3214 | } |
| 3215 | cgroup_iter_end(scan->cg, &it); |
| 3216 | |
| 3217 | if (heap->size) { |
| 3218 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3219 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3220 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3221 | latest_time = q->start_time; |
| 3222 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3223 | } |
| 3224 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3225 | scan->process_task(q, scan); |
| 3226 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3227 | } |
| 3228 | /* |
| 3229 | * If we had to process any tasks at all, scan again |
| 3230 | * in case some of them were in the middle of forking |
| 3231 | * children that didn't get processed. |
| 3232 | * Not the most efficient way to do it, but it avoids |
| 3233 | * having to take callback_mutex in the fork path |
| 3234 | */ |
| 3235 | goto again; |
| 3236 | } |
| 3237 | if (heap == &tmp_heap) |
| 3238 | heap_free(&tmp_heap); |
| 3239 | return 0; |
| 3240 | } |
| 3241 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3242 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3243 | struct cgroup_scanner *scan) |
| 3244 | { |
| 3245 | struct cgroup *new_cgroup = scan->data; |
| 3246 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3247 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3248 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3249 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | /** |
| 3253 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3254 | * @to: cgroup to which the tasks will be moved |
| 3255 | * @from: cgroup in which the tasks currently reside |
| 3256 | */ |
| 3257 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3258 | { |
| 3259 | struct cgroup_scanner scan; |
| 3260 | |
| 3261 | scan.cg = from; |
| 3262 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3263 | scan.process_task = cgroup_transfer_one_task; |
| 3264 | scan.heap = NULL; |
| 3265 | scan.data = to; |
| 3266 | |
| 3267 | return cgroup_scan_tasks(&scan); |
| 3268 | } |
| 3269 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3270 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3271 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3272 | * |
| 3273 | * Reading this file can return large amounts of data if a cgroup has |
| 3274 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3275 | * but we cannot guarantee that the information we produce is correct |
| 3276 | * unless we produce it entirely atomically. |
| 3277 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3278 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3279 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3280 | /* which pidlist file are we talking about? */ |
| 3281 | enum cgroup_filetype { |
| 3282 | CGROUP_FILE_PROCS, |
| 3283 | CGROUP_FILE_TASKS, |
| 3284 | }; |
| 3285 | |
| 3286 | /* |
| 3287 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3288 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3289 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3290 | * to the cgroup. |
| 3291 | */ |
| 3292 | struct cgroup_pidlist { |
| 3293 | /* |
| 3294 | * used to find which pidlist is wanted. doesn't change as long as |
| 3295 | * this particular list stays in the list. |
| 3296 | */ |
| 3297 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3298 | /* array of xids */ |
| 3299 | pid_t *list; |
| 3300 | /* how many elements the above list has */ |
| 3301 | int length; |
| 3302 | /* how many files are using the current array */ |
| 3303 | int use_count; |
| 3304 | /* each of these stored in a list by its cgroup */ |
| 3305 | struct list_head links; |
| 3306 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3307 | struct cgroup *owner; |
| 3308 | /* protects the other fields */ |
| 3309 | struct rw_semaphore mutex; |
| 3310 | }; |
| 3311 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3312 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3313 | * The following two functions "fix" the issue where there are more pids |
| 3314 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3315 | * TODO: replace with a kernel-wide solution to this problem |
| 3316 | */ |
| 3317 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3318 | static void *pidlist_allocate(int count) |
| 3319 | { |
| 3320 | if (PIDLIST_TOO_LARGE(count)) |
| 3321 | return vmalloc(count * sizeof(pid_t)); |
| 3322 | else |
| 3323 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3324 | } |
| 3325 | static void pidlist_free(void *p) |
| 3326 | { |
| 3327 | if (is_vmalloc_addr(p)) |
| 3328 | vfree(p); |
| 3329 | else |
| 3330 | kfree(p); |
| 3331 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3332 | |
| 3333 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3334 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3335 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3336 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3337 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3338 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3339 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3340 | |
| 3341 | /* |
| 3342 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3343 | * edge cases first; no work needs to be done for either |
| 3344 | */ |
| 3345 | if (length == 0 || length == 1) |
| 3346 | return length; |
| 3347 | /* src and dest walk down the list; dest counts unique elements */ |
| 3348 | for (src = 1; src < length; src++) { |
| 3349 | /* find next unique element */ |
| 3350 | while (list[src] == list[src-1]) { |
| 3351 | src++; |
| 3352 | if (src == length) |
| 3353 | goto after; |
| 3354 | } |
| 3355 | /* dest always points to where the next unique element goes */ |
| 3356 | list[dest] = list[src]; |
| 3357 | dest++; |
| 3358 | } |
| 3359 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3360 | return dest; |
| 3361 | } |
| 3362 | |
| 3363 | static int cmppid(const void *a, const void *b) |
| 3364 | { |
| 3365 | return *(pid_t *)a - *(pid_t *)b; |
| 3366 | } |
| 3367 | |
| 3368 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3369 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3370 | * returns with the lock on that pidlist already held, and takes care |
| 3371 | * of the use count, or returns NULL with no locks held if we're out of |
| 3372 | * memory. |
| 3373 | */ |
| 3374 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3375 | enum cgroup_filetype type) |
| 3376 | { |
| 3377 | struct cgroup_pidlist *l; |
| 3378 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3379 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3380 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3381 | /* |
| 3382 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3383 | * the last ref-holder is trying to remove l from the list at the same |
| 3384 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3385 | * list we find out from under us - compare release_pid_array(). |
| 3386 | */ |
| 3387 | mutex_lock(&cgrp->pidlist_mutex); |
| 3388 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3389 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3390 | /* make sure l doesn't vanish out from under us */ |
| 3391 | down_write(&l->mutex); |
| 3392 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3393 | return l; |
| 3394 | } |
| 3395 | } |
| 3396 | /* entry not found; create a new one */ |
| 3397 | l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 3398 | if (!l) { |
| 3399 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3400 | return l; |
| 3401 | } |
| 3402 | init_rwsem(&l->mutex); |
| 3403 | down_write(&l->mutex); |
| 3404 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3405 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3406 | l->use_count = 0; /* don't increment here */ |
| 3407 | l->list = NULL; |
| 3408 | l->owner = cgrp; |
| 3409 | list_add(&l->links, &cgrp->pidlists); |
| 3410 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3411 | return l; |
| 3412 | } |
| 3413 | |
| 3414 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3415 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3416 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3417 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3418 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3419 | { |
| 3420 | pid_t *array; |
| 3421 | int length; |
| 3422 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3423 | struct cgroup_iter it; |
| 3424 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3425 | struct cgroup_pidlist *l; |
| 3426 | |
| 3427 | /* |
| 3428 | * If cgroup gets more users after we read count, we won't have |
| 3429 | * enough space - tough. This race is indistinguishable to the |
| 3430 | * caller from the case that the additional cgroup users didn't |
| 3431 | * show up until sometime later on. |
| 3432 | */ |
| 3433 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3434 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3435 | if (!array) |
| 3436 | return -ENOMEM; |
| 3437 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3438 | cgroup_iter_start(cgrp, &it); |
| 3439 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3440 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3441 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3442 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3443 | if (type == CGROUP_FILE_PROCS) |
| 3444 | pid = task_tgid_vnr(tsk); |
| 3445 | else |
| 3446 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3447 | if (pid > 0) /* make sure to only use valid results */ |
| 3448 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3449 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3450 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3451 | length = n; |
| 3452 | /* now sort & (if procs) strip out duplicates */ |
| 3453 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3454 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3455 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3456 | l = cgroup_pidlist_find(cgrp, type); |
| 3457 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3458 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3459 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3460 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3461 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3462 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3463 | l->list = array; |
| 3464 | l->length = length; |
| 3465 | l->use_count++; |
| 3466 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3467 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3468 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3471 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3472 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3473 | * @stats: cgroupstats to fill information into |
| 3474 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3475 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3476 | * |
| 3477 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3478 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3479 | */ |
| 3480 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3481 | { |
| 3482 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3483 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3484 | struct cgroup_iter it; |
| 3485 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3486 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3487 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3488 | * Validate dentry by checking the superblock operations, |
| 3489 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3490 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3491 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3492 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3493 | goto err; |
| 3494 | |
| 3495 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3496 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3497 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3498 | cgroup_iter_start(cgrp, &it); |
| 3499 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3500 | switch (tsk->state) { |
| 3501 | case TASK_RUNNING: |
| 3502 | stats->nr_running++; |
| 3503 | break; |
| 3504 | case TASK_INTERRUPTIBLE: |
| 3505 | stats->nr_sleeping++; |
| 3506 | break; |
| 3507 | case TASK_UNINTERRUPTIBLE: |
| 3508 | stats->nr_uninterruptible++; |
| 3509 | break; |
| 3510 | case TASK_STOPPED: |
| 3511 | stats->nr_stopped++; |
| 3512 | break; |
| 3513 | default: |
| 3514 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3515 | stats->nr_io_wait++; |
| 3516 | break; |
| 3517 | } |
| 3518 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3519 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3520 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3521 | err: |
| 3522 | return ret; |
| 3523 | } |
| 3524 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3525 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3526 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3527 | * 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] | 3528 | * 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] | 3529 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3530 | */ |
| 3531 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3532 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3533 | { |
| 3534 | /* |
| 3535 | * Initially we receive a position value that corresponds to |
| 3536 | * one more than the last pid shown (or 0 on the first call or |
| 3537 | * after a seek to the start). Use a binary-search to find the |
| 3538 | * next pid to display, if any |
| 3539 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3540 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3541 | int index = 0, pid = *pos; |
| 3542 | int *iter; |
| 3543 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3544 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3545 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3546 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3547 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3548 | while (index < end) { |
| 3549 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3550 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3551 | index = mid; |
| 3552 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3553 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3554 | index = mid + 1; |
| 3555 | else |
| 3556 | end = mid; |
| 3557 | } |
| 3558 | } |
| 3559 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3560 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3561 | return NULL; |
| 3562 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3563 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3564 | *pos = *iter; |
| 3565 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3566 | } |
| 3567 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3568 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3569 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | struct cgroup_pidlist *l = s->private; |
| 3571 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3572 | } |
| 3573 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3574 | 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] | 3575 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3576 | struct cgroup_pidlist *l = s->private; |
| 3577 | pid_t *p = v; |
| 3578 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3579 | /* |
| 3580 | * Advance to the next pid in the array. If this goes off the |
| 3581 | * end, we're done |
| 3582 | */ |
| 3583 | p++; |
| 3584 | if (p >= end) { |
| 3585 | return NULL; |
| 3586 | } else { |
| 3587 | *pos = *p; |
| 3588 | return p; |
| 3589 | } |
| 3590 | } |
| 3591 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3592 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3593 | { |
| 3594 | return seq_printf(s, "%d\n", *(int *)v); |
| 3595 | } |
| 3596 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3597 | /* |
| 3598 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3599 | * independent of whether it's tasks or procs |
| 3600 | */ |
| 3601 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3602 | .start = cgroup_pidlist_start, |
| 3603 | .stop = cgroup_pidlist_stop, |
| 3604 | .next = cgroup_pidlist_next, |
| 3605 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3606 | }; |
| 3607 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3608 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3609 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3610 | /* |
| 3611 | * the case where we're the last user of this particular pidlist will |
| 3612 | * have us remove it from the cgroup's list, which entails taking the |
| 3613 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3614 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3615 | */ |
| 3616 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3617 | down_write(&l->mutex); |
| 3618 | BUG_ON(!l->use_count); |
| 3619 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3620 | /* we're the last user if refcount is 0; remove and free */ |
| 3621 | list_del(&l->links); |
| 3622 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3623 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3624 | put_pid_ns(l->key.ns); |
| 3625 | up_write(&l->mutex); |
| 3626 | kfree(l); |
| 3627 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3628 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3629 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3630 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3633 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3634 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3635 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3636 | if (!(file->f_mode & FMODE_READ)) |
| 3637 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3638 | /* |
| 3639 | * the seq_file will only be initialized if the file was opened for |
| 3640 | * reading; hence we check if it's not null only in that case. |
| 3641 | */ |
| 3642 | l = ((struct seq_file *)file->private_data)->private; |
| 3643 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3644 | return seq_release(inode, file); |
| 3645 | } |
| 3646 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3648 | .read = seq_read, |
| 3649 | .llseek = seq_lseek, |
| 3650 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3651 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3652 | }; |
| 3653 | |
| 3654 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3655 | * The following functions handle opens on a file that displays a pidlist |
| 3656 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3657 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3658 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3660 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3661 | { |
| 3662 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3663 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3664 | int retval; |
| 3665 | |
| 3666 | /* Nothing to do for write-only files */ |
| 3667 | if (!(file->f_mode & FMODE_READ)) |
| 3668 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3669 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3670 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3671 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3672 | if (retval) |
| 3673 | return retval; |
| 3674 | /* configure file information */ |
| 3675 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3676 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3677 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3678 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3679 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3680 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3681 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3682 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3683 | return 0; |
| 3684 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3685 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3686 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3687 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3688 | } |
| 3689 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3690 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3691 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3692 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3693 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3694 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3695 | struct cftype *cft) |
| 3696 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3697 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3700 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3701 | struct cftype *cft, |
| 3702 | u64 val) |
| 3703 | { |
| 3704 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3705 | if (val) |
| 3706 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3707 | else |
| 3708 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3709 | return 0; |
| 3710 | } |
| 3711 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3712 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3713 | * Unregister event and free resources. |
| 3714 | * |
| 3715 | * Gets called from workqueue. |
| 3716 | */ |
| 3717 | static void cgroup_event_remove(struct work_struct *work) |
| 3718 | { |
| 3719 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3720 | remove); |
| 3721 | struct cgroup *cgrp = event->cgrp; |
| 3722 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3723 | remove_wait_queue(event->wqh, &event->wait); |
| 3724 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3725 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3726 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3727 | /* Notify userspace the event is going away. */ |
| 3728 | eventfd_signal(event->eventfd, 1); |
| 3729 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3730 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3731 | kfree(event); |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3732 | dput(cgrp->dentry); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /* |
| 3736 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3737 | * |
| 3738 | * Called with wqh->lock held and interrupts disabled. |
| 3739 | */ |
| 3740 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3741 | int sync, void *key) |
| 3742 | { |
| 3743 | struct cgroup_event *event = container_of(wait, |
| 3744 | struct cgroup_event, wait); |
| 3745 | struct cgroup *cgrp = event->cgrp; |
| 3746 | unsigned long flags = (unsigned long)key; |
| 3747 | |
| 3748 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3749 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3750 | * If the event has been detached at cgroup removal, we |
| 3751 | * can simply return knowing the other side will cleanup |
| 3752 | * for us. |
| 3753 | * |
| 3754 | * We can't race against event freeing since the other |
| 3755 | * side will require wqh->lock via remove_wait_queue(), |
| 3756 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3757 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3758 | spin_lock(&cgrp->event_list_lock); |
| 3759 | if (!list_empty(&event->list)) { |
| 3760 | list_del_init(&event->list); |
| 3761 | /* |
| 3762 | * We are in atomic context, but cgroup_event_remove() |
| 3763 | * may sleep, so we have to call it in workqueue. |
| 3764 | */ |
| 3765 | schedule_work(&event->remove); |
| 3766 | } |
| 3767 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | return 0; |
| 3771 | } |
| 3772 | |
| 3773 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3774 | wait_queue_head_t *wqh, poll_table *pt) |
| 3775 | { |
| 3776 | struct cgroup_event *event = container_of(pt, |
| 3777 | struct cgroup_event, pt); |
| 3778 | |
| 3779 | event->wqh = wqh; |
| 3780 | add_wait_queue(wqh, &event->wait); |
| 3781 | } |
| 3782 | |
| 3783 | /* |
| 3784 | * Parse input and register new cgroup event handler. |
| 3785 | * |
| 3786 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3787 | * Interpretation of args is defined by control file implementation. |
| 3788 | */ |
| 3789 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3790 | const char *buffer) |
| 3791 | { |
| 3792 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3793 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3794 | unsigned int efd, cfd; |
| 3795 | struct file *efile = NULL; |
| 3796 | struct file *cfile = NULL; |
| 3797 | char *endp; |
| 3798 | int ret; |
| 3799 | |
| 3800 | efd = simple_strtoul(buffer, &endp, 10); |
| 3801 | if (*endp != ' ') |
| 3802 | return -EINVAL; |
| 3803 | buffer = endp + 1; |
| 3804 | |
| 3805 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3806 | if ((*endp != ' ') && (*endp != '\0')) |
| 3807 | return -EINVAL; |
| 3808 | buffer = endp + 1; |
| 3809 | |
| 3810 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3811 | if (!event) |
| 3812 | return -ENOMEM; |
| 3813 | event->cgrp = cgrp; |
| 3814 | INIT_LIST_HEAD(&event->list); |
| 3815 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3816 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3817 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3818 | |
| 3819 | efile = eventfd_fget(efd); |
| 3820 | if (IS_ERR(efile)) { |
| 3821 | ret = PTR_ERR(efile); |
| 3822 | goto fail; |
| 3823 | } |
| 3824 | |
| 3825 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3826 | if (IS_ERR(event->eventfd)) { |
| 3827 | ret = PTR_ERR(event->eventfd); |
| 3828 | goto fail; |
| 3829 | } |
| 3830 | |
| 3831 | cfile = fget(cfd); |
| 3832 | if (!cfile) { |
| 3833 | ret = -EBADF; |
| 3834 | goto fail; |
| 3835 | } |
| 3836 | |
| 3837 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3838 | /* AV: shouldn't we check that it's been opened for read instead? */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3839 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3840 | if (ret < 0) |
| 3841 | goto fail; |
| 3842 | |
| 3843 | event->cft = __file_cft(cfile); |
| 3844 | if (IS_ERR(event->cft)) { |
| 3845 | ret = PTR_ERR(event->cft); |
| 3846 | goto fail; |
| 3847 | } |
| 3848 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3849 | /* |
| 3850 | * The file to be monitored must be in the same cgroup as |
| 3851 | * cgroup.event_control is. |
| 3852 | */ |
| 3853 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3854 | if (cgrp_cfile != cgrp) { |
| 3855 | ret = -EINVAL; |
| 3856 | goto fail; |
| 3857 | } |
| 3858 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3859 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3860 | ret = -EINVAL; |
| 3861 | goto fail; |
| 3862 | } |
| 3863 | |
| 3864 | ret = event->cft->register_event(cgrp, event->cft, |
| 3865 | event->eventfd, buffer); |
| 3866 | if (ret) |
| 3867 | goto fail; |
| 3868 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3869 | /* |
| 3870 | * Events should be removed after rmdir of cgroup directory, but before |
| 3871 | * destroying subsystem state objects. Let's take reference to cgroup |
| 3872 | * directory dentry to do that. |
| 3873 | */ |
| 3874 | dget(cgrp->dentry); |
| 3875 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3876 | spin_lock(&cgrp->event_list_lock); |
| 3877 | list_add(&event->list, &cgrp->event_list); |
| 3878 | spin_unlock(&cgrp->event_list_lock); |
| 3879 | |
| 3880 | fput(cfile); |
| 3881 | fput(efile); |
| 3882 | |
| 3883 | return 0; |
| 3884 | |
| 3885 | fail: |
| 3886 | if (cfile) |
| 3887 | fput(cfile); |
| 3888 | |
| 3889 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 3890 | eventfd_ctx_put(event->eventfd); |
| 3891 | |
| 3892 | if (!IS_ERR_OR_NULL(efile)) |
| 3893 | fput(efile); |
| 3894 | |
| 3895 | kfree(event); |
| 3896 | |
| 3897 | return ret; |
| 3898 | } |
| 3899 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3900 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 3901 | struct cftype *cft) |
| 3902 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3903 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 3907 | struct cftype *cft, |
| 3908 | u64 val) |
| 3909 | { |
| 3910 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3911 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3912 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 3913 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3914 | return 0; |
| 3915 | } |
| 3916 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3917 | /* |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3918 | * for the common functions, 'private' gives the type of file |
| 3919 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3920 | /* for hysterical raisins, we can't put this on the older files */ |
| 3921 | #define CGROUP_FILE_GENERIC_PREFIX "cgroup." |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3922 | static struct cftype files[] = { |
| 3923 | { |
| 3924 | .name = "tasks", |
| 3925 | .open = cgroup_tasks_open, |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3926 | .write_u64 = cgroup_tasks_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3927 | .release = cgroup_pidlist_release, |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 3928 | .mode = S_IRUGO | S_IWUSR, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3929 | }, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3930 | { |
| 3931 | .name = CGROUP_FILE_GENERIC_PREFIX "procs", |
| 3932 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3933 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3934 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 3935 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3936 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3937 | { |
| 3938 | .name = "notify_on_release", |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 3939 | .read_u64 = cgroup_read_notify_on_release, |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3940 | .write_u64 = cgroup_write_notify_on_release, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3941 | }, |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3942 | { |
| 3943 | .name = CGROUP_FILE_GENERIC_PREFIX "event_control", |
| 3944 | .write_string = cgroup_write_event_control, |
| 3945 | .mode = S_IWUGO, |
| 3946 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 3947 | { |
| 3948 | .name = "cgroup.clone_children", |
| 3949 | .read_u64 = cgroup_clone_children_read, |
| 3950 | .write_u64 = cgroup_clone_children_write, |
| 3951 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3952 | { |
| 3953 | .name = "release_agent", |
| 3954 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 3955 | .read_seq_string = cgroup_release_agent_show, |
| 3956 | .write_string = cgroup_release_agent_write, |
| 3957 | .max_write_len = PATH_MAX, |
| 3958 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3959 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3960 | }; |
| 3961 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3962 | /** |
| 3963 | * cgroup_populate_dir - selectively creation of files in a directory |
| 3964 | * @cgrp: target cgroup |
| 3965 | * @base_files: true if the base files should be added |
| 3966 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 3967 | */ |
| 3968 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 3969 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3970 | { |
| 3971 | int err; |
| 3972 | struct cgroup_subsys *ss; |
| 3973 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3974 | if (base_files) { |
| 3975 | err = cgroup_addrm_files(cgrp, NULL, files, true); |
| 3976 | if (err < 0) |
| 3977 | return err; |
| 3978 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3979 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3980 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3981 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3982 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 3983 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 3984 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3985 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3986 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 3987 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 3988 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3989 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 3990 | /* This cgroup is ready now */ |
| 3991 | for_each_subsys(cgrp->root, ss) { |
| 3992 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 3993 | /* |
| 3994 | * Update id->css pointer and make this css visible from |
| 3995 | * CSS ID functions. This pointer will be dereferened |
| 3996 | * from RCU-read-side without locks. |
| 3997 | */ |
| 3998 | if (css->id) |
| 3999 | rcu_assign_pointer(css->id->css, css); |
| 4000 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4001 | |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4005 | static void css_dput_fn(struct work_struct *work) |
| 4006 | { |
| 4007 | struct cgroup_subsys_state *css = |
| 4008 | container_of(work, struct cgroup_subsys_state, dput_work); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4009 | struct dentry *dentry = css->cgroup->dentry; |
| 4010 | struct super_block *sb = dentry->d_sb; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4011 | |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4012 | atomic_inc(&sb->s_active); |
| 4013 | dput(dentry); |
| 4014 | deactivate_super(sb); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4017 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4018 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4019 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4020 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4021 | css->cgroup = cgrp; |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 4022 | atomic_set(&css->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4023 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4024 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4025 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4026 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4027 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4028 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4029 | |
| 4030 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4031 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4032 | * css_put(). dput() requires process context, which css_put() may |
| 4033 | * be called without. @css->dput_work will be used to invoke |
| 4034 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4035 | */ |
| 4036 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4037 | } |
| 4038 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4039 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4040 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4041 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4042 | int ret = 0; |
| 4043 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4044 | lockdep_assert_held(&cgroup_mutex); |
| 4045 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4046 | if (ss->css_online) |
| 4047 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4048 | if (!ret) |
| 4049 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4050 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4054 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4055 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4056 | { |
| 4057 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4058 | |
| 4059 | lockdep_assert_held(&cgroup_mutex); |
| 4060 | |
| 4061 | if (!(css->flags & CSS_ONLINE)) |
| 4062 | return; |
| 4063 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4064 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4065 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4066 | |
| 4067 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4068 | } |
| 4069 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4070 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4071 | * cgroup_create - create a cgroup |
| 4072 | * @parent: cgroup that will be parent of the new cgroup |
| 4073 | * @dentry: dentry of the new cgroup |
| 4074 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4075 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4076 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4077 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4078 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4079 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4080 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4081 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4082 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4083 | struct cgroupfs_root *root = parent->root; |
| 4084 | int err = 0; |
| 4085 | struct cgroup_subsys *ss; |
| 4086 | struct super_block *sb = root->sb; |
| 4087 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4088 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4089 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4090 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4091 | return -ENOMEM; |
| 4092 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4093 | name = cgroup_alloc_name(dentry); |
| 4094 | if (!name) |
| 4095 | goto err_free_cgrp; |
| 4096 | rcu_assign_pointer(cgrp->name, name); |
| 4097 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4098 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4099 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4100 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4101 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4102 | /* |
| 4103 | * Only live parents can have children. Note that the liveliness |
| 4104 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4105 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4106 | * anyway so that locking is contained inside cgroup proper and we |
| 4107 | * don't get nasty surprises if we ever grow another caller. |
| 4108 | */ |
| 4109 | if (!cgroup_lock_live_group(parent)) { |
| 4110 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4111 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4112 | } |
| 4113 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4114 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4115 | * get deleted on unmount if there are child cgroups. This |
| 4116 | * can be done outside cgroup_mutex, since the sb can't |
| 4117 | * disappear while someone has an open control file on the |
| 4118 | * fs */ |
| 4119 | atomic_inc(&sb->s_active); |
| 4120 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4121 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4122 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4123 | dentry->d_fsdata = cgrp; |
| 4124 | cgrp->dentry = dentry; |
| 4125 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4126 | cgrp->parent = parent; |
| 4127 | cgrp->root = parent->root; |
| 4128 | cgrp->top_cgroup = parent->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4129 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4130 | if (notify_on_release(parent)) |
| 4131 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4132 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4133 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4134 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4135 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4136 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4137 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4138 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4139 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4140 | if (IS_ERR(css)) { |
| 4141 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4142 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4143 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4144 | init_cgroup_css(css, ss, cgrp); |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4145 | if (ss->use_id) { |
| 4146 | err = alloc_css_id(ss, parent, cgrp); |
| 4147 | if (err) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4148 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4149 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4152 | /* |
| 4153 | * Create directory. cgroup_create_file() returns with the new |
| 4154 | * directory locked on success so that it can be populated without |
| 4155 | * dropping cgroup_mutex. |
| 4156 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4157 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4158 | if (err < 0) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4159 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4160 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4161 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4162 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4163 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4164 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4165 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4166 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4167 | /* each css holds a ref to the cgroup's dentry */ |
| 4168 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4169 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4170 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4171 | /* creation succeeded, notify subsystems */ |
| 4172 | for_each_subsys(root, ss) { |
| 4173 | err = online_css(ss, cgrp); |
| 4174 | if (err) |
| 4175 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4176 | |
| 4177 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4178 | parent->parent) { |
| 4179 | 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", |
| 4180 | current->comm, current->pid, ss->name); |
| 4181 | if (!strcmp(ss->name, "memory")) |
| 4182 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4183 | ss->warned_broken_hierarchy = true; |
| 4184 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4185 | } |
| 4186 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4187 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4188 | if (err) |
| 4189 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4190 | |
| 4191 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4192 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4193 | |
| 4194 | return 0; |
| 4195 | |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4196 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4197 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4198 | if (cgrp->subsys[ss->subsys_id]) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4199 | ss->css_free(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4201 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4202 | /* Release the reference count that we took on the superblock */ |
| 4203 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4204 | err_free_id: |
| 4205 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4206 | err_free_name: |
| 4207 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4208 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4209 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4210 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4211 | |
| 4212 | err_destroy: |
| 4213 | cgroup_destroy_locked(cgrp); |
| 4214 | mutex_unlock(&cgroup_mutex); |
| 4215 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4216 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4219 | 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] | 4220 | { |
| 4221 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4222 | |
| 4223 | /* the vfs holds inode->i_mutex already */ |
| 4224 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4225 | } |
| 4226 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4227 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4228 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4229 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4230 | struct dentry *d = cgrp->dentry; |
| 4231 | struct cgroup *parent = cgrp->parent; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4232 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4233 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4234 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4235 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4236 | lockdep_assert_held(&cgroup_mutex); |
| 4237 | |
| 4238 | if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4239 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4240 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4241 | /* |
| 4242 | * Block new css_tryget() by deactivating refcnt and mark @cgrp |
| 4243 | * removed. This makes future css_tryget() and child creation |
| 4244 | * attempts fail thus maintaining the removal conditions verified |
| 4245 | * above. |
| 4246 | */ |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4247 | for_each_subsys(cgrp->root, ss) { |
| 4248 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4249 | |
| 4250 | WARN_ON(atomic_read(&css->refcnt) < 0); |
| 4251 | atomic_add(CSS_DEACT_BIAS, &css->refcnt); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4252 | } |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4253 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 4254 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4255 | /* tell subsystems to initate destruction */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4256 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4257 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4258 | |
| 4259 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4260 | * Put all the base refs. Each css holds an extra reference to the |
| 4261 | * cgroup's dentry and cgroup removal proceeds regardless of css |
| 4262 | * refs. On the last put of each css, whenever that may be, the |
| 4263 | * extra dentry ref is put so that dentry destruction happens only |
| 4264 | * after all css's are released. |
| 4265 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4266 | for_each_subsys(cgrp->root, ss) |
| 4267 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4268 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4269 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4270 | if (!list_empty(&cgrp->release_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4271 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4272 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4273 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4274 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4275 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4276 | list_del_init(&cgrp->allcg_node); |
| 4277 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4278 | dget(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4279 | cgroup_d_remove_dir(d); |
| 4280 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4281 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4282 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4283 | check_for_release(parent); |
| 4284 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4285 | /* |
| 4286 | * Unregister events and notify userspace. |
| 4287 | * Notify userspace about cgroup removing only after rmdir of cgroup |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4288 | * directory to avoid race between userspace and kernelspace. |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4289 | */ |
| 4290 | spin_lock(&cgrp->event_list_lock); |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4291 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 4292 | list_del_init(&event->list); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4293 | schedule_work(&event->remove); |
| 4294 | } |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4295 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4296 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4297 | return 0; |
| 4298 | } |
| 4299 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4300 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4301 | { |
| 4302 | int ret; |
| 4303 | |
| 4304 | mutex_lock(&cgroup_mutex); |
| 4305 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4306 | mutex_unlock(&cgroup_mutex); |
| 4307 | |
| 4308 | return ret; |
| 4309 | } |
| 4310 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4311 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4312 | { |
| 4313 | INIT_LIST_HEAD(&ss->cftsets); |
| 4314 | |
| 4315 | /* |
| 4316 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4317 | * deregistration. |
| 4318 | */ |
| 4319 | if (ss->base_cftypes) { |
| 4320 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4321 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4322 | } |
| 4323 | } |
| 4324 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4325 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4326 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4327 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4328 | |
| 4329 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4330 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4331 | mutex_lock(&cgroup_mutex); |
| 4332 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4333 | /* init base cftset */ |
| 4334 | cgroup_init_cftsets(ss); |
| 4335 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4336 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4337 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4338 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4339 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4340 | /* We don't handle early failures gracefully */ |
| 4341 | BUG_ON(IS_ERR(css)); |
| 4342 | init_cgroup_css(css, ss, dummytop); |
| 4343 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4344 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4345 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4346 | * newly registered, all tasks and hence the |
| 4347 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4348 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4349 | |
| 4350 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4351 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4352 | /* At system boot, before all subsystems have been |
| 4353 | * registered, no tasks have been forked, so we don't |
| 4354 | * need to invoke fork callbacks here. */ |
| 4355 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4356 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4357 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4358 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4359 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4360 | mutex_unlock(&cgroup_mutex); |
| 4361 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4362 | /* this function shouldn't be used with modular subsystems, since they |
| 4363 | * need to register a subsys_id, among other things */ |
| 4364 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4368 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4369 | * @ss: the subsystem to load |
| 4370 | * |
| 4371 | * 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] | 4372 | * 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] | 4373 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4374 | * simpler cgroup_init_subsys. |
| 4375 | */ |
| 4376 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4377 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4378 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4379 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4380 | struct hlist_node *tmp; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4381 | struct css_set *cg; |
| 4382 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4383 | |
| 4384 | /* check name and function validity */ |
| 4385 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4386 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4387 | return -EINVAL; |
| 4388 | |
| 4389 | /* |
| 4390 | * we don't support callbacks in modular subsystems. this check is |
| 4391 | * before the ss->module check for consistency; a subsystem that could |
| 4392 | * be a module should still have no callbacks even if the user isn't |
| 4393 | * compiling it as one. |
| 4394 | */ |
| 4395 | if (ss->fork || ss->exit) |
| 4396 | return -EINVAL; |
| 4397 | |
| 4398 | /* |
| 4399 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4400 | * since cgroup_init_subsys will have already taken care of it. |
| 4401 | */ |
| 4402 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4403 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4404 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4405 | return 0; |
| 4406 | } |
| 4407 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4408 | /* init base cftset */ |
| 4409 | cgroup_init_cftsets(ss); |
| 4410 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4411 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4412 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4413 | |
| 4414 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4415 | * no ss->css_alloc seems to need anything important in the ss |
| 4416 | * struct, so this can happen first (i.e. before the rootnode |
| 4417 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4418 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4419 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4420 | if (IS_ERR(css)) { |
| 4421 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4422 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4423 | mutex_unlock(&cgroup_mutex); |
| 4424 | return PTR_ERR(css); |
| 4425 | } |
| 4426 | |
| 4427 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4428 | ss->root = &rootnode; |
| 4429 | |
| 4430 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4431 | init_cgroup_css(css, ss, dummytop); |
| 4432 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4433 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4434 | ret = cgroup_init_idr(ss, css); |
| 4435 | if (ret) |
| 4436 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | /* |
| 4440 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4441 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4442 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4443 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4444 | * table state, so each changed css_set will need its hash recomputed. |
| 4445 | * this is all done under the css_set_lock. |
| 4446 | */ |
| 4447 | write_lock(&css_set_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4448 | hash_for_each_safe(css_set_table, i, tmp, cg, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4449 | /* skip entries that we already rehashed */ |
| 4450 | if (cg->subsys[ss->subsys_id]) |
| 4451 | continue; |
| 4452 | /* remove existing entry */ |
| 4453 | hash_del(&cg->hlist); |
| 4454 | /* set new value */ |
| 4455 | cg->subsys[ss->subsys_id] = css; |
| 4456 | /* recompute hash and restore entry */ |
| 4457 | key = css_set_hash(cg->subsys); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4458 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4459 | } |
| 4460 | write_unlock(&css_set_lock); |
| 4461 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4462 | ss->active = 1; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4463 | ret = online_css(ss, dummytop); |
| 4464 | if (ret) |
| 4465 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4466 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4467 | /* success! */ |
| 4468 | mutex_unlock(&cgroup_mutex); |
| 4469 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4470 | |
| 4471 | err_unload: |
| 4472 | mutex_unlock(&cgroup_mutex); |
| 4473 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4474 | cgroup_unload_subsys(ss); |
| 4475 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4476 | } |
| 4477 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4478 | |
| 4479 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4480 | * cgroup_unload_subsys: unload a modular subsystem |
| 4481 | * @ss: the subsystem to unload |
| 4482 | * |
| 4483 | * This function should be called in a modular subsystem's exitcall. When this |
| 4484 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4485 | * the subsystem will not be attached to any hierarchy. |
| 4486 | */ |
| 4487 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4488 | { |
| 4489 | struct cg_cgroup_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4490 | |
| 4491 | BUG_ON(ss->module == NULL); |
| 4492 | |
| 4493 | /* |
| 4494 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4495 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4496 | * doesn't start being used while we're killing it off. |
| 4497 | */ |
| 4498 | BUG_ON(ss->root != &rootnode); |
| 4499 | |
| 4500 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4501 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4502 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4503 | ss->active = 0; |
| 4504 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4505 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4506 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4507 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4508 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4509 | subsys[ss->subsys_id] = NULL; |
| 4510 | |
| 4511 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4512 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4513 | |
| 4514 | /* |
| 4515 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4516 | * in loading, we need to pay our respects to the hashtable gods. |
| 4517 | */ |
| 4518 | write_lock(&css_set_lock); |
| 4519 | list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { |
| 4520 | struct css_set *cg = link->cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4521 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4522 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4523 | hash_del(&cg->hlist); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4524 | cg->subsys[ss->subsys_id] = NULL; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4525 | key = css_set_hash(cg->subsys); |
| 4526 | hash_add(css_set_table, &cg->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4527 | } |
| 4528 | write_unlock(&css_set_lock); |
| 4529 | |
| 4530 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4531 | * remove subsystem's css from the dummytop and free it - need to |
| 4532 | * free before marking as null because ss->css_free needs the |
| 4533 | * cgrp->subsys pointer to find their state. note that this also |
| 4534 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4535 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4536 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4537 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4538 | |
| 4539 | mutex_unlock(&cgroup_mutex); |
| 4540 | } |
| 4541 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4542 | |
| 4543 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4544 | * cgroup_init_early - cgroup initialization at system boot |
| 4545 | * |
| 4546 | * Initialize cgroups at system boot, and initialize any |
| 4547 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4548 | */ |
| 4549 | int __init cgroup_init_early(void) |
| 4550 | { |
| 4551 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4552 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4553 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 4554 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4555 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4556 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4557 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4558 | root_count = 1; |
| 4559 | init_task.cgroups = &init_css_set; |
| 4560 | |
| 4561 | init_css_set_link.cg = &init_css_set; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4562 | init_css_set_link.cgrp = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4563 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4564 | &rootnode.top_cgroup.css_sets); |
| 4565 | list_add(&init_css_set_link.cg_link_list, |
| 4566 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4567 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4568 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4569 | struct cgroup_subsys *ss = subsys[i]; |
| 4570 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4571 | /* at bootup time, we don't worry about modular subsystems */ |
| 4572 | if (!ss || ss->module) |
| 4573 | continue; |
| 4574 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4575 | BUG_ON(!ss->name); |
| 4576 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4577 | BUG_ON(!ss->css_alloc); |
| 4578 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4579 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4580 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4581 | ss->name, ss->subsys_id); |
| 4582 | BUG(); |
| 4583 | } |
| 4584 | |
| 4585 | if (ss->early_init) |
| 4586 | cgroup_init_subsys(ss); |
| 4587 | } |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
| 4591 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4592 | * cgroup_init - cgroup initialization |
| 4593 | * |
| 4594 | * Register cgroup filesystem and /proc file, and initialize |
| 4595 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4596 | */ |
| 4597 | int __init cgroup_init(void) |
| 4598 | { |
| 4599 | int err; |
| 4600 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4601 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4602 | |
| 4603 | err = bdi_init(&cgroup_backing_dev_info); |
| 4604 | if (err) |
| 4605 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4606 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4607 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4608 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4609 | |
| 4610 | /* at bootup time, we don't worry about modular subsystems */ |
| 4611 | if (!ss || ss->module) |
| 4612 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4613 | if (!ss->early_init) |
| 4614 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4615 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4616 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4619 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4620 | key = css_set_hash(init_css_set.subsys); |
| 4621 | hash_add(css_set_table, &init_css_set.hlist, key); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4622 | BUG_ON(!init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4623 | |
| 4624 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4625 | if (!cgroup_kobj) { |
| 4626 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4627 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | err = register_filesystem(&cgroup_fs_type); |
| 4631 | if (err < 0) { |
| 4632 | kobject_put(cgroup_kobj); |
| 4633 | goto out; |
| 4634 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4635 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4636 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4637 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4638 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4639 | if (err) |
| 4640 | bdi_destroy(&cgroup_backing_dev_info); |
| 4641 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4642 | return err; |
| 4643 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4644 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4645 | /* |
| 4646 | * proc_cgroup_show() |
| 4647 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4648 | * - Used for /proc/<pid>/cgroup. |
| 4649 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4650 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4651 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4652 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4653 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4654 | * cgroup to top_cgroup. |
| 4655 | */ |
| 4656 | |
| 4657 | /* TODO: Use a proper seq_file iterator */ |
| 4658 | static int proc_cgroup_show(struct seq_file *m, void *v) |
| 4659 | { |
| 4660 | struct pid *pid; |
| 4661 | struct task_struct *tsk; |
| 4662 | char *buf; |
| 4663 | int retval; |
| 4664 | struct cgroupfs_root *root; |
| 4665 | |
| 4666 | retval = -ENOMEM; |
| 4667 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4668 | if (!buf) |
| 4669 | goto out; |
| 4670 | |
| 4671 | retval = -ESRCH; |
| 4672 | pid = m->private; |
| 4673 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4674 | if (!tsk) |
| 4675 | goto out_free; |
| 4676 | |
| 4677 | retval = 0; |
| 4678 | |
| 4679 | mutex_lock(&cgroup_mutex); |
| 4680 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4681 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4682 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4683 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4684 | int count = 0; |
| 4685 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4686 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4687 | for_each_subsys(root, ss) |
| 4688 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4689 | if (strlen(root->name)) |
| 4690 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4691 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4692 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4693 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4694 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4695 | if (retval < 0) |
| 4696 | goto out_unlock; |
| 4697 | seq_puts(m, buf); |
| 4698 | seq_putc(m, '\n'); |
| 4699 | } |
| 4700 | |
| 4701 | out_unlock: |
| 4702 | mutex_unlock(&cgroup_mutex); |
| 4703 | put_task_struct(tsk); |
| 4704 | out_free: |
| 4705 | kfree(buf); |
| 4706 | out: |
| 4707 | return retval; |
| 4708 | } |
| 4709 | |
| 4710 | static int cgroup_open(struct inode *inode, struct file *file) |
| 4711 | { |
| 4712 | struct pid *pid = PROC_I(inode)->pid; |
| 4713 | return single_open(file, proc_cgroup_show, pid); |
| 4714 | } |
| 4715 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4716 | const struct file_operations proc_cgroup_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4717 | .open = cgroup_open, |
| 4718 | .read = seq_read, |
| 4719 | .llseek = seq_lseek, |
| 4720 | .release = single_release, |
| 4721 | }; |
| 4722 | |
| 4723 | /* Display information about each subsystem and each hierarchy */ |
| 4724 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4725 | { |
| 4726 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4727 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4728 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4729 | /* |
| 4730 | * ideally we don't want subsystems moving around while we do this. |
| 4731 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4732 | * subsys/hierarchy state. |
| 4733 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4734 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4735 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4736 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4737 | if (ss == NULL) |
| 4738 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4739 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4740 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4741 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4742 | } |
| 4743 | mutex_unlock(&cgroup_mutex); |
| 4744 | return 0; |
| 4745 | } |
| 4746 | |
| 4747 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4748 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4749 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4752 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4753 | .open = cgroupstats_open, |
| 4754 | .read = seq_read, |
| 4755 | .llseek = seq_lseek, |
| 4756 | .release = single_release, |
| 4757 | }; |
| 4758 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4759 | /** |
| 4760 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4761 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4762 | * |
| 4763 | * Description: A task inherits its parent's cgroup at fork(). |
| 4764 | * |
| 4765 | * A pointer to the shared css_set was automatically copied in |
| 4766 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4767 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4768 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4769 | * have already changed current->cgroups, allowing the previously |
| 4770 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4771 | * |
| 4772 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4773 | * task, and the passed argument 'child' points to the child task. |
| 4774 | */ |
| 4775 | void cgroup_fork(struct task_struct *child) |
| 4776 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4777 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4778 | child->cgroups = current->cgroups; |
| 4779 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4780 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4781 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4785 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4786 | * @child: the task in question |
| 4787 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4788 | * Adds the task to the list running through its css_set if necessary and |
| 4789 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4790 | * visible on the task list in case we race with the first call to |
| 4791 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 4792 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4793 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4794 | void cgroup_post_fork(struct task_struct *child) |
| 4795 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4796 | int i; |
| 4797 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4798 | /* |
| 4799 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4800 | * under the tasklist_lock and we read it here after we added the child |
| 4801 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4802 | * yet in the tasklist when we walked through it from |
| 4803 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4804 | * should be visible now due to the paired locking and barriers implied |
| 4805 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4806 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4807 | * lock on fork. |
| 4808 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4809 | if (use_task_css_set_links) { |
| 4810 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4811 | task_lock(child); |
| 4812 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4813 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4814 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4815 | write_unlock(&css_set_lock); |
| 4816 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4817 | |
| 4818 | /* |
| 4819 | * Call ss->fork(). This must happen after @child is linked on |
| 4820 | * css_set; otherwise, @child might change state between ->fork() |
| 4821 | * and addition to css_set. |
| 4822 | */ |
| 4823 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 4824 | /* |
| 4825 | * fork/exit callbacks are supported only for builtin |
| 4826 | * subsystems, and the builtin section of the subsys |
| 4827 | * array is immutable, so we don't need to lock the |
| 4828 | * subsys array here. On the other hand, modular section |
| 4829 | * of the array can be freed at module unload, so we |
| 4830 | * can't touch that. |
| 4831 | */ |
| 4832 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4833 | struct cgroup_subsys *ss = subsys[i]; |
| 4834 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4835 | if (ss->fork) |
| 4836 | ss->fork(child); |
| 4837 | } |
| 4838 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4839 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4840 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4841 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4842 | * cgroup_exit - detach cgroup from exiting task |
| 4843 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4844 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4845 | * |
| 4846 | * Description: Detach cgroup from @tsk and release it. |
| 4847 | * |
| 4848 | * Note that cgroups marked notify_on_release force every task in |
| 4849 | * them to take the global cgroup_mutex mutex when exiting. |
| 4850 | * This could impact scaling on very large systems. Be reluctant to |
| 4851 | * use notify_on_release cgroups where very high task exit scaling |
| 4852 | * is required on large systems. |
| 4853 | * |
| 4854 | * the_top_cgroup_hack: |
| 4855 | * |
| 4856 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4857 | * |
| 4858 | * We call cgroup_exit() while the task is still competent to |
| 4859 | * handle notify_on_release(), then leave the task attached to the |
| 4860 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4861 | * |
| 4862 | * To do this properly, we would increment the reference count on |
| 4863 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4864 | * code we would add a second cgroup function call, to drop that |
| 4865 | * reference. This would just create an unnecessary hot spot on |
| 4866 | * the top_cgroup reference count, to no avail. |
| 4867 | * |
| 4868 | * Normally, holding a reference to a cgroup without bumping its |
| 4869 | * count is unsafe. The cgroup could go away, or someone could |
| 4870 | * attach us to a different cgroup, decrementing the count on |
| 4871 | * the first cgroup that we never incremented. But in this case, |
| 4872 | * 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] | 4873 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 4874 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4875 | */ |
| 4876 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 4877 | { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4878 | struct css_set *cg; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4879 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4880 | |
| 4881 | /* |
| 4882 | * Unlink from the css_set task list if necessary. |
| 4883 | * Optimistically check cg_list before taking |
| 4884 | * css_set_lock |
| 4885 | */ |
| 4886 | if (!list_empty(&tsk->cg_list)) { |
| 4887 | write_lock(&css_set_lock); |
| 4888 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4889 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4890 | write_unlock(&css_set_lock); |
| 4891 | } |
| 4892 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4893 | /* Reassign the task to the init_css_set. */ |
| 4894 | task_lock(tsk); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4895 | cg = tsk->cgroups; |
| 4896 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4897 | |
| 4898 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 4899 | /* |
| 4900 | * fork/exit callbacks are supported only for builtin |
| 4901 | * subsystems, see cgroup_post_fork() for details. |
| 4902 | */ |
| 4903 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4904 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4905 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4906 | if (ss->exit) { |
| 4907 | struct cgroup *old_cgrp = |
| 4908 | rcu_dereference_raw(cg->subsys[i])->cgroup; |
| 4909 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 4910 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4911 | } |
| 4912 | } |
| 4913 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4914 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 4915 | |
Li Zefan | b5d646f | 2013-01-24 14:43:51 +0800 | [diff] [blame] | 4916 | put_css_set_taskexit(cg); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4917 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 4918 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4919 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4920 | { |
| 4921 | /* All of these checks rely on RCU to keep the cgroup |
| 4922 | * structure alive */ |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4923 | if (cgroup_is_releasable(cgrp) && |
| 4924 | !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) { |
| 4925 | /* |
| 4926 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4927 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4928 | * it now |
| 4929 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4930 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4931 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4932 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4933 | if (!cgroup_is_removed(cgrp) && |
| 4934 | list_empty(&cgrp->release_list)) { |
| 4935 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4936 | need_schedule_work = 1; |
| 4937 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4938 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4939 | if (need_schedule_work) |
| 4940 | schedule_work(&release_agent_work); |
| 4941 | } |
| 4942 | } |
| 4943 | |
Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 4944 | /* Caller must verify that the css is not for root cgroup */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4945 | bool __css_tryget(struct cgroup_subsys_state *css) |
| 4946 | { |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4947 | while (true) { |
| 4948 | int t, v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4949 | |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4950 | v = css_refcnt(css); |
| 4951 | t = atomic_cmpxchg(&css->refcnt, v, v + 1); |
| 4952 | if (likely(t == v)) |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4953 | return true; |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4954 | else if (t < 0) |
| 4955 | return false; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4956 | cpu_relax(); |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4957 | } |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4958 | } |
| 4959 | EXPORT_SYMBOL_GPL(__css_tryget); |
| 4960 | |
| 4961 | /* Caller must verify that the css is not for root cgroup */ |
| 4962 | void __css_put(struct cgroup_subsys_state *css) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4963 | { |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 4964 | int v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4965 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 4966 | v = css_unbias_refcnt(atomic_dec_return(&css->refcnt)); |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 4967 | if (v == 0) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4968 | schedule_work(&css->dput_work); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4969 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 4970 | EXPORT_SYMBOL_GPL(__css_put); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4971 | |
| 4972 | /* |
| 4973 | * Notify userspace when a cgroup is released, by running the |
| 4974 | * configured release agent with the name of the cgroup (path |
| 4975 | * relative to the root of cgroup file system) as the argument. |
| 4976 | * |
| 4977 | * Most likely, this user command will try to rmdir this cgroup. |
| 4978 | * |
| 4979 | * This races with the possibility that some other task will be |
| 4980 | * attached to this cgroup before it is removed, or that some other |
| 4981 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 4982 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 4983 | * unused, and this cgroup will be reprieved from its death sentence, |
| 4984 | * to continue to serve a useful existence. Next time it's released, |
| 4985 | * we will get notified again, if it still has 'notify_on_release' set. |
| 4986 | * |
| 4987 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 4988 | * means only wait until the task is successfully execve()'d. The |
| 4989 | * separate release agent task is forked by call_usermodehelper(), |
| 4990 | * then control in this thread returns here, without waiting for the |
| 4991 | * release agent task. We don't bother to wait because the caller of |
| 4992 | * this routine has no use for the exit status of the release agent |
| 4993 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4994 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4995 | static void cgroup_release_agent(struct work_struct *work) |
| 4996 | { |
| 4997 | BUG_ON(work != &release_agent_work); |
| 4998 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4999 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5000 | while (!list_empty(&release_list)) { |
| 5001 | char *argv[3], *envp[3]; |
| 5002 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5003 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5004 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5005 | struct cgroup, |
| 5006 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5007 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5008 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5009 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5010 | if (!pathbuf) |
| 5011 | goto continue_free; |
| 5012 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5013 | goto continue_free; |
| 5014 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5015 | if (!agentbuf) |
| 5016 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5017 | |
| 5018 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5019 | argv[i++] = agentbuf; |
| 5020 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5021 | argv[i] = NULL; |
| 5022 | |
| 5023 | i = 0; |
| 5024 | /* minimal command environment */ |
| 5025 | envp[i++] = "HOME=/"; |
| 5026 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5027 | envp[i] = NULL; |
| 5028 | |
| 5029 | /* Drop the lock while we invoke the usermode helper, |
| 5030 | * since the exec could involve hitting disk and hence |
| 5031 | * be a slow process */ |
| 5032 | mutex_unlock(&cgroup_mutex); |
| 5033 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5034 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5035 | continue_free: |
| 5036 | kfree(pathbuf); |
| 5037 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5038 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5039 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5040 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5041 | mutex_unlock(&cgroup_mutex); |
| 5042 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5043 | |
| 5044 | static int __init cgroup_disable(char *str) |
| 5045 | { |
| 5046 | int i; |
| 5047 | char *token; |
| 5048 | |
| 5049 | while ((token = strsep(&str, ",")) != NULL) { |
| 5050 | if (!*token) |
| 5051 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5052 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5053 | struct cgroup_subsys *ss = subsys[i]; |
| 5054 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5055 | /* |
| 5056 | * cgroup_disable, being at boot time, can't |
| 5057 | * know about module subsystems, so we don't |
| 5058 | * worry about them. |
| 5059 | */ |
| 5060 | if (!ss || ss->module) |
| 5061 | continue; |
| 5062 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5063 | if (!strcmp(token, ss->name)) { |
| 5064 | ss->disabled = 1; |
| 5065 | printk(KERN_INFO "Disabling %s control group" |
| 5066 | " subsystem\n", ss->name); |
| 5067 | break; |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | return 1; |
| 5072 | } |
| 5073 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5074 | |
| 5075 | /* |
| 5076 | * Functons for CSS ID. |
| 5077 | */ |
| 5078 | |
| 5079 | /* |
| 5080 | *To get ID other than 0, this should be called when !cgroup_is_removed(). |
| 5081 | */ |
| 5082 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5083 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5084 | struct css_id *cssid; |
| 5085 | |
| 5086 | /* |
| 5087 | * This css_id() can return correct value when somone has refcnt |
| 5088 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5089 | * it's unchanged until freed. |
| 5090 | */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5091 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5092 | |
| 5093 | if (cssid) |
| 5094 | return cssid->id; |
| 5095 | return 0; |
| 5096 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5097 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5098 | |
| 5099 | unsigned short css_depth(struct cgroup_subsys_state *css) |
| 5100 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5101 | struct css_id *cssid; |
| 5102 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5103 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5104 | |
| 5105 | if (cssid) |
| 5106 | return cssid->depth; |
| 5107 | return 0; |
| 5108 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5109 | EXPORT_SYMBOL_GPL(css_depth); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5110 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5111 | /** |
| 5112 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5113 | * @child: the css to be tested. |
| 5114 | * @root: the css supporsed to be an ancestor of the child. |
| 5115 | * |
| 5116 | * 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] | 5117 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5118 | * But, considering usual usage, the csses should be valid objects after test. |
| 5119 | * Assuming that the caller will do some action to the child if this returns |
| 5120 | * returns true, the caller must take "child";s reference count. |
| 5121 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5122 | */ |
| 5123 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5124 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5125 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5126 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5127 | struct css_id *child_id; |
| 5128 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5129 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5130 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5131 | if (!child_id) |
| 5132 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5133 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5134 | if (!root_id) |
| 5135 | return false; |
| 5136 | if (child_id->depth < root_id->depth) |
| 5137 | return false; |
| 5138 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5139 | return false; |
| 5140 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5141 | } |
| 5142 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5143 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5144 | { |
| 5145 | struct css_id *id = css->id; |
| 5146 | /* When this is called before css_id initialization, id can be NULL */ |
| 5147 | if (!id) |
| 5148 | return; |
| 5149 | |
| 5150 | BUG_ON(!ss->use_id); |
| 5151 | |
| 5152 | rcu_assign_pointer(id->css, NULL); |
| 5153 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5154 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5155 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5156 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5157 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5158 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5159 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5160 | |
| 5161 | /* |
| 5162 | * This is called by init or create(). Then, calls to this function are |
| 5163 | * always serialized (By cgroup_mutex() at create()). |
| 5164 | */ |
| 5165 | |
| 5166 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5167 | { |
| 5168 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5169 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5170 | |
| 5171 | BUG_ON(!ss->use_id); |
| 5172 | |
| 5173 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5174 | newid = kzalloc(size, GFP_KERNEL); |
| 5175 | if (!newid) |
| 5176 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5177 | |
| 5178 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5179 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5180 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5181 | ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5182 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5183 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5184 | |
| 5185 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5186 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5187 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5188 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5189 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5190 | newid->depth = depth; |
| 5191 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5192 | err_out: |
| 5193 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5194 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5195 | |
| 5196 | } |
| 5197 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5198 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5199 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5200 | { |
| 5201 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5202 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5203 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5204 | idr_init(&ss->idr); |
| 5205 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5206 | newid = get_new_cssid(ss, 0); |
| 5207 | if (IS_ERR(newid)) |
| 5208 | return PTR_ERR(newid); |
| 5209 | |
| 5210 | newid->stack[0] = newid->id; |
| 5211 | newid->css = rootcss; |
| 5212 | rootcss->id = newid; |
| 5213 | return 0; |
| 5214 | } |
| 5215 | |
| 5216 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5217 | struct cgroup *child) |
| 5218 | { |
| 5219 | int subsys_id, i, depth = 0; |
| 5220 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5221 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5222 | |
| 5223 | subsys_id = ss->subsys_id; |
| 5224 | parent_css = parent->subsys[subsys_id]; |
| 5225 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5226 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5227 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5228 | |
| 5229 | child_id = get_new_cssid(ss, depth); |
| 5230 | if (IS_ERR(child_id)) |
| 5231 | return PTR_ERR(child_id); |
| 5232 | |
| 5233 | for (i = 0; i < depth; i++) |
| 5234 | child_id->stack[i] = parent_id->stack[i]; |
| 5235 | child_id->stack[depth] = child_id->id; |
| 5236 | /* |
| 5237 | * child_id->css pointer will be set after this cgroup is available |
| 5238 | * see cgroup_populate_dir() |
| 5239 | */ |
| 5240 | rcu_assign_pointer(child_css->id, child_id); |
| 5241 | |
| 5242 | return 0; |
| 5243 | } |
| 5244 | |
| 5245 | /** |
| 5246 | * css_lookup - lookup css by id |
| 5247 | * @ss: cgroup subsys to be looked into. |
| 5248 | * @id: the id |
| 5249 | * |
| 5250 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5251 | * NULL if not. Should be called under rcu_read_lock() |
| 5252 | */ |
| 5253 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5254 | { |
| 5255 | struct css_id *cssid = NULL; |
| 5256 | |
| 5257 | BUG_ON(!ss->use_id); |
| 5258 | cssid = idr_find(&ss->idr, id); |
| 5259 | |
| 5260 | if (unlikely(!cssid)) |
| 5261 | return NULL; |
| 5262 | |
| 5263 | return rcu_dereference(cssid->css); |
| 5264 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5265 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5266 | |
| 5267 | /** |
| 5268 | * css_get_next - lookup next cgroup under specified hierarchy. |
| 5269 | * @ss: pointer to subsystem |
| 5270 | * @id: current position of iteration. |
| 5271 | * @root: pointer to css. search tree under this. |
| 5272 | * @foundid: position of found object. |
| 5273 | * |
| 5274 | * Search next css under the specified hierarchy of rootid. Calling under |
| 5275 | * rcu_read_lock() is necessary. Returns NULL if it reaches the end. |
| 5276 | */ |
| 5277 | struct cgroup_subsys_state * |
| 5278 | css_get_next(struct cgroup_subsys *ss, int id, |
| 5279 | struct cgroup_subsys_state *root, int *foundid) |
| 5280 | { |
| 5281 | struct cgroup_subsys_state *ret = NULL; |
| 5282 | struct css_id *tmp; |
| 5283 | int tmpid; |
| 5284 | int rootid = css_id(root); |
| 5285 | int depth = css_depth(root); |
| 5286 | |
| 5287 | if (!rootid) |
| 5288 | return NULL; |
| 5289 | |
| 5290 | BUG_ON(!ss->use_id); |
Hugh Dickins | ca464d6 | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5291 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 5292 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5293 | /* fill start point for scan */ |
| 5294 | tmpid = id; |
| 5295 | while (1) { |
| 5296 | /* |
| 5297 | * scan next entry from bitmap(tree), tmpid is updated after |
| 5298 | * idr_get_next(). |
| 5299 | */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5300 | tmp = idr_get_next(&ss->idr, &tmpid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5301 | if (!tmp) |
| 5302 | break; |
| 5303 | if (tmp->depth >= depth && tmp->stack[depth] == rootid) { |
| 5304 | ret = rcu_dereference(tmp->css); |
| 5305 | if (ret) { |
| 5306 | *foundid = tmpid; |
| 5307 | break; |
| 5308 | } |
| 5309 | } |
| 5310 | /* continue to scan from next id */ |
| 5311 | tmpid = tmpid + 1; |
| 5312 | } |
| 5313 | return ret; |
| 5314 | } |
| 5315 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5316 | /* |
| 5317 | * get corresponding css from file open on cgroupfs directory |
| 5318 | */ |
| 5319 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5320 | { |
| 5321 | struct cgroup *cgrp; |
| 5322 | struct inode *inode; |
| 5323 | struct cgroup_subsys_state *css; |
| 5324 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5325 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5326 | /* check in cgroup filesystem dir */ |
| 5327 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5328 | return ERR_PTR(-EBADF); |
| 5329 | |
| 5330 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5331 | return ERR_PTR(-EINVAL); |
| 5332 | |
| 5333 | /* get cgroup */ |
| 5334 | cgrp = __d_cgrp(f->f_dentry); |
| 5335 | css = cgrp->subsys[id]; |
| 5336 | return css ? css : ERR_PTR(-ENOENT); |
| 5337 | } |
| 5338 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5339 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5340 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5341 | { |
| 5342 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5343 | |
| 5344 | if (!css) |
| 5345 | return ERR_PTR(-ENOMEM); |
| 5346 | |
| 5347 | return css; |
| 5348 | } |
| 5349 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5350 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5351 | { |
| 5352 | kfree(cont->subsys[debug_subsys_id]); |
| 5353 | } |
| 5354 | |
| 5355 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) |
| 5356 | { |
| 5357 | return atomic_read(&cont->count); |
| 5358 | } |
| 5359 | |
| 5360 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5361 | { |
| 5362 | return cgroup_task_count(cont); |
| 5363 | } |
| 5364 | |
| 5365 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5366 | { |
| 5367 | return (u64)(unsigned long)current->cgroups; |
| 5368 | } |
| 5369 | |
| 5370 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5371 | struct cftype *cft) |
| 5372 | { |
| 5373 | u64 count; |
| 5374 | |
| 5375 | rcu_read_lock(); |
| 5376 | count = atomic_read(¤t->cgroups->refcount); |
| 5377 | rcu_read_unlock(); |
| 5378 | return count; |
| 5379 | } |
| 5380 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5381 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5382 | struct cftype *cft, |
| 5383 | struct seq_file *seq) |
| 5384 | { |
| 5385 | struct cg_cgroup_link *link; |
| 5386 | struct css_set *cg; |
| 5387 | |
| 5388 | read_lock(&css_set_lock); |
| 5389 | rcu_read_lock(); |
| 5390 | cg = rcu_dereference(current->cgroups); |
| 5391 | list_for_each_entry(link, &cg->cg_links, cg_link_list) { |
| 5392 | struct cgroup *c = link->cgrp; |
| 5393 | const char *name; |
| 5394 | |
| 5395 | if (c->dentry) |
| 5396 | name = c->dentry->d_name.name; |
| 5397 | else |
| 5398 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5399 | seq_printf(seq, "Root %d group %s\n", |
| 5400 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5401 | } |
| 5402 | rcu_read_unlock(); |
| 5403 | read_unlock(&css_set_lock); |
| 5404 | return 0; |
| 5405 | } |
| 5406 | |
| 5407 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5408 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5409 | struct cftype *cft, |
| 5410 | struct seq_file *seq) |
| 5411 | { |
| 5412 | struct cg_cgroup_link *link; |
| 5413 | |
| 5414 | read_lock(&css_set_lock); |
| 5415 | list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { |
| 5416 | struct css_set *cg = link->cg; |
| 5417 | struct task_struct *task; |
| 5418 | int count = 0; |
| 5419 | seq_printf(seq, "css_set %p\n", cg); |
| 5420 | list_for_each_entry(task, &cg->tasks, cg_list) { |
| 5421 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5422 | seq_puts(seq, " ...\n"); |
| 5423 | break; |
| 5424 | } else { |
| 5425 | seq_printf(seq, " task %d\n", |
| 5426 | task_pid_vnr(task)); |
| 5427 | } |
| 5428 | } |
| 5429 | } |
| 5430 | read_unlock(&css_set_lock); |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5434 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5435 | { |
| 5436 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5437 | } |
| 5438 | |
| 5439 | static struct cftype debug_files[] = { |
| 5440 | { |
| 5441 | .name = "cgroup_refcount", |
| 5442 | .read_u64 = cgroup_refcount_read, |
| 5443 | }, |
| 5444 | { |
| 5445 | .name = "taskcount", |
| 5446 | .read_u64 = debug_taskcount_read, |
| 5447 | }, |
| 5448 | |
| 5449 | { |
| 5450 | .name = "current_css_set", |
| 5451 | .read_u64 = current_css_set_read, |
| 5452 | }, |
| 5453 | |
| 5454 | { |
| 5455 | .name = "current_css_set_refcount", |
| 5456 | .read_u64 = current_css_set_refcount_read, |
| 5457 | }, |
| 5458 | |
| 5459 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5460 | .name = "current_css_set_cg_links", |
| 5461 | .read_seq_string = current_css_set_cg_links_read, |
| 5462 | }, |
| 5463 | |
| 5464 | { |
| 5465 | .name = "cgroup_css_links", |
| 5466 | .read_seq_string = cgroup_css_links_read, |
| 5467 | }, |
| 5468 | |
| 5469 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5470 | .name = "releasable", |
| 5471 | .read_u64 = releasable_read, |
| 5472 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5473 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5474 | { } /* terminate */ |
| 5475 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5476 | |
| 5477 | struct cgroup_subsys debug_subsys = { |
| 5478 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5479 | .css_alloc = debug_css_alloc, |
| 5480 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5481 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5482 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5483 | }; |
| 5484 | #endif /* CONFIG_CGROUP_DEBUG */ |