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