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