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