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