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