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 | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* |
| 67 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 68 | * hierarchy must be performed while holding it. |
| 69 | * |
| 70 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 71 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 72 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 73 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 74 | * break the following locking order cycle. |
| 75 | * |
| 76 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 77 | * B. namespace_sem -> cgroup_mutex |
| 78 | * |
| 79 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 80 | * breaks it. |
| 81 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_RCU |
| 83 | DEFINE_MUTEX(cgroup_mutex); |
| 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 85 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 93 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | * registered after that. The mutable section of this array is protected by |
| 95 | * cgroup_mutex. |
| 96 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 97 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 98 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 99 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
| 104 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 105 | * subsystems that are otherwise unattached - it never has more than a |
| 106 | * single cgroup, and all tasks are part of that cgroup. |
| 107 | */ |
| 108 | static struct cgroupfs_root rootnode; |
| 109 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 110 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 111 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 112 | */ |
| 113 | struct cfent { |
| 114 | struct list_head node; |
| 115 | struct dentry *dentry; |
| 116 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 117 | |
| 118 | /* file xattrs */ |
| 119 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 123 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 124 | * cgroup_subsys->use_id != 0. |
| 125 | */ |
| 126 | #define CSS_ID_MAX (65535) |
| 127 | struct css_id { |
| 128 | /* |
| 129 | * The css to which this ID points. This pointer is set to valid value |
| 130 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 131 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 132 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 133 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 134 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 135 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 136 | /* |
| 137 | * ID of this css. |
| 138 | */ |
| 139 | unsigned short id; |
| 140 | /* |
| 141 | * Depth in hierarchy which this ID belongs to. |
| 142 | */ |
| 143 | unsigned short depth; |
| 144 | /* |
| 145 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 146 | */ |
| 147 | struct rcu_head rcu_head; |
| 148 | /* |
| 149 | * Hierarchy of CSS ID belongs to. |
| 150 | */ |
| 151 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 152 | }; |
| 153 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 154 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 155 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 156 | */ |
| 157 | struct cgroup_event { |
| 158 | /* |
| 159 | * Cgroup which the event belongs to. |
| 160 | */ |
| 161 | struct cgroup *cgrp; |
| 162 | /* |
| 163 | * Control file which the event associated. |
| 164 | */ |
| 165 | struct cftype *cft; |
| 166 | /* |
| 167 | * eventfd to signal userspace about the event. |
| 168 | */ |
| 169 | struct eventfd_ctx *eventfd; |
| 170 | /* |
| 171 | * Each of these stored in a list by the cgroup. |
| 172 | */ |
| 173 | struct list_head list; |
| 174 | /* |
| 175 | * All fields below needed to unregister event when |
| 176 | * userspace closes eventfd. |
| 177 | */ |
| 178 | poll_table pt; |
| 179 | wait_queue_head_t *wqh; |
| 180 | wait_queue_t wait; |
| 181 | struct work_struct remove; |
| 182 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 183 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 184 | /* The list of hierarchy roots */ |
| 185 | |
| 186 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 187 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 188 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 191 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 192 | * writes, either for reads. |
| 193 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 194 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 195 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 196 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 197 | #define dummytop (&rootnode.top_cgroup) |
| 198 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 199 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 200 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 201 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 202 | * check for fork/exit handlers to call. This avoids us having to do |
| 203 | * extra work in the fork/exit path if none of the subsystems need to |
| 204 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 205 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 206 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 207 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 208 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 209 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 210 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 211 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 212 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 213 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 214 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 215 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 216 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 219 | /** |
| 220 | * cgroup_is_descendant - test ancestry |
| 221 | * @cgrp: the cgroup to be tested |
| 222 | * @ancestor: possible ancestor of @cgrp |
| 223 | * |
| 224 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 225 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 226 | * and @ancestor are accessible. |
| 227 | */ |
| 228 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 229 | { |
| 230 | while (cgrp) { |
| 231 | if (cgrp == ancestor) |
| 232 | return true; |
| 233 | cgrp = cgrp->parent; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 238 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 239 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 240 | { |
| 241 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 242 | (1 << CGRP_RELEASABLE) | |
| 243 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 244 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 247 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 248 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 249 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 252 | /* |
| 253 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 254 | * an active hierarchy |
| 255 | */ |
| 256 | #define for_each_subsys(_root, _ss) \ |
| 257 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 258 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 259 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 260 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 261 | list_for_each_entry(_root, &roots, root_list) |
| 262 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 263 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 264 | { |
| 265 | return dentry->d_fsdata; |
| 266 | } |
| 267 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 268 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 269 | { |
| 270 | return dentry->d_fsdata; |
| 271 | } |
| 272 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 273 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 274 | { |
| 275 | return __d_cfe(dentry)->type; |
| 276 | } |
| 277 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 278 | /** |
| 279 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 280 | * @cgrp: the cgroup to be checked for liveness |
| 281 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 282 | * On success, returns true; the mutex should be later unlocked. On |
| 283 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 284 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 285 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 286 | { |
| 287 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 288 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 289 | mutex_unlock(&cgroup_mutex); |
| 290 | return false; |
| 291 | } |
| 292 | return true; |
| 293 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 295 | /* the list of cgroups eligible for automatic release. Protected by |
| 296 | * release_list_lock */ |
| 297 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 298 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 299 | static void cgroup_release_agent(struct work_struct *work); |
| 300 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 301 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 302 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 303 | /* |
| 304 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 305 | * belong to different cgroups on different hierarchies. In the other |
| 306 | * direction, a css_set is naturally associated with multiple cgroups. |
| 307 | * This M:N relationship is represented by the following link structure |
| 308 | * which exists for each association and allows traversing the associations |
| 309 | * from both sides. |
| 310 | */ |
| 311 | struct cgrp_cset_link { |
| 312 | /* the cgroup and css_set this link associates */ |
| 313 | struct cgroup *cgrp; |
| 314 | struct css_set *cset; |
| 315 | |
| 316 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 317 | struct list_head cset_link; |
| 318 | |
| 319 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 320 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | /* The default css_set - used by init and its children prior to any |
| 324 | * hierarchies being mounted. It contains a pointer to the root state |
| 325 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 326 | * reference-counted, to improve performance when child cgroups |
| 327 | * haven't been created. |
| 328 | */ |
| 329 | |
| 330 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 331 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 332 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 333 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 334 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 335 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 336 | /* css_set_lock protects the list of css_set objects, and the |
| 337 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 338 | * due to cgroup_iter_start() */ |
| 339 | static DEFINE_RWLOCK(css_set_lock); |
| 340 | static int css_set_count; |
| 341 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 342 | /* |
| 343 | * hash table for cgroup groups. This improves the performance to find |
| 344 | * an existing css_set. This hash doesn't (currently) take into |
| 345 | * account cgroups in empty hierarchies. |
| 346 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 347 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 348 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 349 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 350 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 351 | { |
| 352 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 353 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 354 | |
| 355 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 356 | key += (unsigned long)css[i]; |
| 357 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 358 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 359 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 362 | /* We don't maintain the lists running through each css_set to its |
| 363 | * task until after the first call to cgroup_iter_start(). This |
| 364 | * reduces the fork()/exit() overhead for people who have cgroups |
| 365 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 366 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 367 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 368 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 369 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 370 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 371 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 372 | /* |
| 373 | * Ensure that the refcount doesn't hit zero while any readers |
| 374 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 375 | * rwlock |
| 376 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 377 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 378 | return; |
| 379 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 380 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 381 | write_unlock(&css_set_lock); |
| 382 | return; |
| 383 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 384 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 385 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 386 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 387 | css_set_count--; |
| 388 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 389 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 390 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 391 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 392 | list_del(&link->cset_link); |
| 393 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 394 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 395 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 396 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 397 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 398 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 399 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 400 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 401 | |
| 402 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 403 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 404 | |
| 405 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 406 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
| 410 | * refcounted get/put for css_set objects |
| 411 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 412 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 413 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 414 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 417 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 418 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 419 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 423 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 427 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 428 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 429 | * @cset: candidate css_set being tested |
| 430 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 431 | * @new_cgrp: cgroup that's being entered by the task |
| 432 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 433 | * |
| 434 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 435 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 436 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 437 | static bool compare_css_sets(struct css_set *cset, |
| 438 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 439 | struct cgroup *new_cgrp, |
| 440 | struct cgroup_subsys_state *template[]) |
| 441 | { |
| 442 | struct list_head *l1, *l2; |
| 443 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 444 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 445 | /* Not all subsystems matched */ |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Compare cgroup pointers in order to distinguish between |
| 451 | * different cgroups in heirarchies with no subsystems. We |
| 452 | * could get by with just this check alone (and skip the |
| 453 | * memcmp above) but on most setups the memcmp check will |
| 454 | * avoid the need for this more expensive check on almost all |
| 455 | * candidates. |
| 456 | */ |
| 457 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 458 | l1 = &cset->cgrp_links; |
| 459 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 460 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 461 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 462 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 463 | |
| 464 | l1 = l1->next; |
| 465 | l2 = l2->next; |
| 466 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 467 | if (l1 == &cset->cgrp_links) { |
| 468 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 469 | break; |
| 470 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 471 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 472 | } |
| 473 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 474 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 475 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 476 | cgrp1 = link1->cgrp; |
| 477 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 479 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * If this hierarchy is the hierarchy of the cgroup |
| 483 | * that's changing, then we need to check that this |
| 484 | * css_set points to the new cgroup; if it's any other |
| 485 | * hierarchy, then this css_set should point to the |
| 486 | * same cgroup as the old css_set. |
| 487 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 488 | if (cgrp1->root == new_cgrp->root) { |
| 489 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 490 | return false; |
| 491 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 492 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 500 | * find_existing_css_set() is a helper for |
| 501 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 502 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 503 | * |
| 504 | * oldcg: the cgroup group that we're using before the cgroup |
| 505 | * transition |
| 506 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 507 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 508 | * |
| 509 | * template: location in which to build the desired set of subsystem |
| 510 | * state objects for the new cgroup group |
| 511 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 512 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 513 | struct cgroup *cgrp, |
| 514 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 515 | { |
| 516 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 517 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 518 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 519 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 520 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 521 | /* |
| 522 | * Build the set of subsystem state objects that we want to see in the |
| 523 | * new css_set. while subsystems can change globally, the entries here |
| 524 | * won't change, so no need for locking. |
| 525 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 526 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 527 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 528 | /* Subsystem is in this hierarchy. So we want |
| 529 | * the subsystem state from the new |
| 530 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 531 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 532 | } else { |
| 533 | /* Subsystem is not in this hierarchy, so we |
| 534 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 535 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 539 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 541 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 542 | continue; |
| 543 | |
| 544 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 545 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 546 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 547 | |
| 548 | /* No existing cgroup group matched */ |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 552 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 553 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 554 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 555 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 556 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 557 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 558 | kfree(link); |
| 559 | } |
| 560 | } |
| 561 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 562 | /** |
| 563 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 564 | * @count: the number of links to allocate |
| 565 | * @tmp_links: list_head the allocated links are put on |
| 566 | * |
| 567 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 568 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 569 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 570 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 571 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 572 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 573 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 574 | |
| 575 | INIT_LIST_HEAD(tmp_links); |
| 576 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 577 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 578 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 579 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 580 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 581 | return -ENOMEM; |
| 582 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 584 | } |
| 585 | return 0; |
| 586 | } |
| 587 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 588 | /** |
| 589 | * link_css_set - a helper function to link a css_set to a cgroup |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 590 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 591 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 592 | * @cgrp: the destination cgroup |
| 593 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 594 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 595 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 596 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 597 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 598 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | BUG_ON(list_empty(tmp_links)); |
| 600 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 601 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 602 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 604 | /* |
| 605 | * Always add links to the tail of the list so that the list |
| 606 | * is sorted by order of hierarchy creation |
| 607 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 608 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 611 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 612 | * find_css_set() takes an existing cgroup group and a |
| 613 | * cgroup object, and returns a css_set object that's |
| 614 | * equivalent to the old group, but with the given cgroup |
| 615 | * substituted into the appropriate hierarchy. Must be called with |
| 616 | * cgroup_mutex held |
| 617 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 618 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 619 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 620 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 621 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 622 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 623 | struct list_head tmp_links; |
| 624 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 625 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 626 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 627 | /* First see if we already have a cgroup group that matches |
| 628 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 629 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 630 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 631 | if (cset) |
| 632 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 633 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 634 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 635 | if (cset) |
| 636 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 637 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 638 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 639 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 640 | return NULL; |
| 641 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 642 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
| 643 | if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 644 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 645 | return NULL; |
| 646 | } |
| 647 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 648 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 649 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | INIT_LIST_HEAD(&cset->tasks); |
| 651 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 652 | |
| 653 | /* Copy the set of subsystem state objects generated in |
| 654 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 655 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 656 | |
| 657 | write_lock(&css_set_lock); |
| 658 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 659 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 660 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 661 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 662 | if (c->root == cgrp->root) |
| 663 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 664 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 665 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 667 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 668 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 669 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 670 | |
| 671 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 672 | key = css_set_hash(cset->subsys); |
| 673 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 674 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | write_unlock(&css_set_lock); |
| 676 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 677 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 680 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 681 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 682 | * called with cgroup_mutex held. |
| 683 | */ |
| 684 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 685 | struct cgroupfs_root *root) |
| 686 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 688 | struct cgroup *res = NULL; |
| 689 | |
| 690 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 691 | read_lock(&css_set_lock); |
| 692 | /* |
| 693 | * No need to lock the task - since we hold cgroup_mutex the |
| 694 | * task can't change groups, so the only thing that can happen |
| 695 | * is that it exits and its css is set back to init_css_set. |
| 696 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 697 | cset = task->cgroups; |
| 698 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 699 | res = &root->top_cgroup; |
| 700 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 701 | struct cgrp_cset_link *link; |
| 702 | |
| 703 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 704 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 705 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 706 | if (c->root == root) { |
| 707 | res = c; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | read_unlock(&css_set_lock); |
| 713 | BUG_ON(!res); |
| 714 | return res; |
| 715 | } |
| 716 | |
| 717 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 718 | * There is one global cgroup mutex. We also require taking |
| 719 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 720 | * See "The task_lock() exception", at the end of this comment. |
| 721 | * |
| 722 | * A task must hold cgroup_mutex to modify cgroups. |
| 723 | * |
| 724 | * Any task can increment and decrement the count field without lock. |
| 725 | * So in general, code holding cgroup_mutex can't rely on the count |
| 726 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 727 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 728 | * means that no tasks are currently attached, therefore there is no |
| 729 | * way a task attached to that cgroup can fork (the other way to |
| 730 | * increment the count). So code holding cgroup_mutex can safely |
| 731 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 732 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 733 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 734 | * needs that mutex. |
| 735 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 736 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 737 | * (usually) take cgroup_mutex. These are the two most performance |
| 738 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 739 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 740 | * 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] | 741 | * to the release agent with the name of the cgroup (path relative to |
| 742 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 743 | * |
| 744 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 745 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 746 | * tasks in the system use _some_ cgroup, and since there is always at |
| 747 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 748 | * always has either children cgroups and/or using tasks. So we don't |
| 749 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 750 | * |
| 751 | * The task_lock() exception |
| 752 | * |
| 753 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 754 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 755 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 756 | * several performance critical places that need to reference |
| 757 | * task->cgroup without the expense of grabbing a system global |
| 758 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 759 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 760 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 761 | * the task_struct routinely used for such matters. |
| 762 | * |
| 763 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 764 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 765 | */ |
| 766 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 767 | /* |
| 768 | * A couple of forward declarations required, due to cyclic reference loop: |
| 769 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 770 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 771 | * -> cgroup_mkdir. |
| 772 | */ |
| 773 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 774 | 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] | 775 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 776 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 777 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 778 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 779 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 780 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 781 | |
| 782 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 783 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 784 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 785 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 787 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 788 | struct cgroup *parent, struct cgroup *child); |
| 789 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 790 | 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] | 791 | { |
| 792 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | |
| 794 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 795 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 796 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 797 | inode->i_uid = current_fsuid(); |
| 798 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 800 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 801 | } |
| 802 | return inode; |
| 803 | } |
| 804 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 805 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 806 | { |
| 807 | struct cgroup_name *name; |
| 808 | |
| 809 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 810 | if (!name) |
| 811 | return NULL; |
| 812 | strcpy(name->name, dentry->d_name.name); |
| 813 | return name; |
| 814 | } |
| 815 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 816 | static void cgroup_free_fn(struct work_struct *work) |
| 817 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 818 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 819 | struct cgroup_subsys *ss; |
| 820 | |
| 821 | mutex_lock(&cgroup_mutex); |
| 822 | /* |
| 823 | * Release the subsystem state objects. |
| 824 | */ |
| 825 | for_each_subsys(cgrp->root, ss) |
| 826 | ss->css_free(cgrp); |
| 827 | |
| 828 | cgrp->root->number_of_cgroups--; |
| 829 | mutex_unlock(&cgroup_mutex); |
| 830 | |
| 831 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 832 | * We get a ref to the parent's dentry, and put the ref when |
| 833 | * this cgroup is being freed, so it's guaranteed that the |
| 834 | * parent won't be destroyed before its children. |
| 835 | */ |
| 836 | dput(cgrp->parent->dentry); |
| 837 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 838 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 839 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 840 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 841 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 842 | * created the cgroup. This will free cgrp->root, if we are |
| 843 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 844 | */ |
| 845 | deactivate_super(cgrp->root->sb); |
| 846 | |
| 847 | /* |
| 848 | * if we're getting rid of the cgroup, refcount should ensure |
| 849 | * that there are no pidlists left. |
| 850 | */ |
| 851 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 852 | |
| 853 | simple_xattrs_free(&cgrp->xattrs); |
| 854 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 855 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 856 | kfree(cgrp); |
| 857 | } |
| 858 | |
| 859 | static void cgroup_free_rcu(struct rcu_head *head) |
| 860 | { |
| 861 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 862 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 863 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 864 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 865 | } |
| 866 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 867 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 868 | { |
| 869 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 870 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 871 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 872 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 873 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 874 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 875 | } else { |
| 876 | struct cfent *cfe = __d_cfe(dentry); |
| 877 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 878 | |
| 879 | WARN_ONCE(!list_empty(&cfe->node) && |
| 880 | cgrp != &cgrp->root->top_cgroup, |
| 881 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 882 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 883 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 884 | } |
| 885 | iput(inode); |
| 886 | } |
| 887 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 888 | static int cgroup_delete(const struct dentry *d) |
| 889 | { |
| 890 | return 1; |
| 891 | } |
| 892 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 893 | static void remove_dir(struct dentry *d) |
| 894 | { |
| 895 | struct dentry *parent = dget(d->d_parent); |
| 896 | |
| 897 | d_delete(d); |
| 898 | simple_rmdir(parent->d_inode, d); |
| 899 | dput(parent); |
| 900 | } |
| 901 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 902 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 903 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 904 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 905 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 906 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 907 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 908 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 909 | /* |
| 910 | * If we're doing cleanup due to failure of cgroup_create(), |
| 911 | * the corresponding @cfe may not exist. |
| 912 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 913 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 914 | struct dentry *d = cfe->dentry; |
| 915 | |
| 916 | if (cft && cfe->type != cft) |
| 917 | continue; |
| 918 | |
| 919 | dget(d); |
| 920 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 921 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 922 | list_del_init(&cfe->node); |
| 923 | dput(d); |
| 924 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 925 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 926 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 929 | /** |
| 930 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 931 | * @dir: directory containing the files |
| 932 | * @base_files: true if the base files should be removed |
| 933 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 934 | */ |
| 935 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 936 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 937 | { |
| 938 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 939 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 940 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 941 | for_each_subsys(cgrp->root, ss) { |
| 942 | struct cftype_set *set; |
| 943 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 944 | continue; |
| 945 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 946 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 947 | } |
| 948 | if (base_files) { |
| 949 | while (!list_empty(&cgrp->files)) |
| 950 | cgroup_rm_file(cgrp, NULL); |
| 951 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* |
| 955 | * NOTE : the dentry must have been dget()'ed |
| 956 | */ |
| 957 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 958 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 959 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 960 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 961 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 962 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 963 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 964 | parent = dentry->d_parent; |
| 965 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 966 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 967 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 968 | spin_unlock(&dentry->d_lock); |
| 969 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 970 | remove_dir(dentry); |
| 971 | } |
| 972 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 973 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 974 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 975 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 976 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 977 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 978 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 979 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 980 | { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 981 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 982 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | int i; |
| 984 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 985 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 986 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 987 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 988 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 989 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | /* Check that any added subsystems are currently free */ |
| 991 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 992 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 994 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 995 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 996 | /* |
| 997 | * Nobody should tell us to do a subsys that doesn't exist: |
| 998 | * parse_cgroupfs_options should catch that case and refcounts |
| 999 | * ensure that subsystems won't disappear once selected. |
| 1000 | */ |
| 1001 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | if (ss->root != &rootnode) { |
| 1003 | /* Subsystem isn't free */ |
| 1004 | return -EBUSY; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* Currently we don't handle adding/removing subsystems when |
| 1009 | * any child cgroups exist. This is theoretically supportable |
| 1010 | * but involves complex error handling, so it's being left until |
| 1011 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1012 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | return -EBUSY; |
| 1014 | |
| 1015 | /* Process each subsystem */ |
| 1016 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1017 | struct cgroup_subsys *ss = subsys[i]; |
| 1018 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1019 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1020 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1021 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1022 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1023 | BUG_ON(!dummytop->subsys[i]); |
| 1024 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1025 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1026 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1027 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1028 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1029 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1030 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1031 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1032 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1034 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1035 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1036 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1037 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1038 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1040 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1041 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1042 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1043 | /* subsystem is now free - drop reference on module */ |
| 1044 | module_put(ss->module); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1045 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1046 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1047 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1048 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1049 | /* |
| 1050 | * a refcount was taken, but we already had one, so |
| 1051 | * drop the extra reference. |
| 1052 | */ |
| 1053 | module_put(ss->module); |
| 1054 | #ifdef CONFIG_MODULE_UNLOAD |
| 1055 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1056 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1057 | } else { |
| 1058 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1059 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1062 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1067 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1068 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1069 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | struct cgroup_subsys *ss; |
| 1071 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1072 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | for_each_subsys(root, ss) |
| 1074 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1075 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1076 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1077 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1079 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1080 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1081 | if (strlen(root->release_agent_path)) |
| 1082 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1083 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1084 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1085 | if (strlen(root->name)) |
| 1086 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1087 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1092 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1093 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1094 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1095 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1096 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1097 | /* User explicitly requested empty subsystem */ |
| 1098 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1099 | |
| 1100 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1101 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | }; |
| 1103 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1104 | /* |
| 1105 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1106 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1107 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1108 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1109 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1110 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1111 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1112 | char *token, *o = data; |
| 1113 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1114 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1115 | int i; |
| 1116 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1117 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1118 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1119 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1120 | #ifdef CONFIG_CPUSETS |
| 1121 | mask = ~(1UL << cpuset_subsys_id); |
| 1122 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1123 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1124 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1125 | |
| 1126 | while ((token = strsep(&o, ",")) != NULL) { |
| 1127 | if (!*token) |
| 1128 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1129 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1130 | /* Explicitly have no subsystems */ |
| 1131 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1132 | continue; |
| 1133 | } |
| 1134 | if (!strcmp(token, "all")) { |
| 1135 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1136 | if (one_ss) |
| 1137 | return -EINVAL; |
| 1138 | all_ss = true; |
| 1139 | continue; |
| 1140 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1141 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1142 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1143 | continue; |
| 1144 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1145 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1146 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
| 1149 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1150 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1151 | continue; |
| 1152 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1153 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1154 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1155 | continue; |
| 1156 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1157 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1158 | /* Specifying two release agents is forbidden */ |
| 1159 | if (opts->release_agent) |
| 1160 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1161 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1162 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1163 | if (!opts->release_agent) |
| 1164 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1165 | continue; |
| 1166 | } |
| 1167 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1168 | const char *name = token + 5; |
| 1169 | /* Can't specify an empty name */ |
| 1170 | if (!strlen(name)) |
| 1171 | return -EINVAL; |
| 1172 | /* Must match [\w.-]+ */ |
| 1173 | for (i = 0; i < strlen(name); i++) { |
| 1174 | char c = name[i]; |
| 1175 | if (isalnum(c)) |
| 1176 | continue; |
| 1177 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1178 | continue; |
| 1179 | return -EINVAL; |
| 1180 | } |
| 1181 | /* Specifying two names is forbidden */ |
| 1182 | if (opts->name) |
| 1183 | return -EINVAL; |
| 1184 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1185 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1186 | GFP_KERNEL); |
| 1187 | if (!opts->name) |
| 1188 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1189 | |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
| 1193 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1194 | struct cgroup_subsys *ss = subsys[i]; |
| 1195 | if (ss == NULL) |
| 1196 | continue; |
| 1197 | if (strcmp(token, ss->name)) |
| 1198 | continue; |
| 1199 | if (ss->disabled) |
| 1200 | continue; |
| 1201 | |
| 1202 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1203 | if (all_ss) |
| 1204 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1205 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1206 | one_ss = true; |
| 1207 | |
| 1208 | break; |
| 1209 | } |
| 1210 | if (i == CGROUP_SUBSYS_COUNT) |
| 1211 | return -ENOENT; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1216 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1217 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1218 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1219 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1220 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1221 | struct cgroup_subsys *ss = subsys[i]; |
| 1222 | if (ss == NULL) |
| 1223 | continue; |
| 1224 | if (ss->disabled) |
| 1225 | continue; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1226 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1230 | /* Consistency checks */ |
| 1231 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1232 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1233 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1234 | |
| 1235 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1236 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1237 | return -EINVAL; |
| 1238 | } |
| 1239 | |
| 1240 | if (opts->cpuset_clone_children) { |
| 1241 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1242 | return -EINVAL; |
| 1243 | } |
| 1244 | } |
| 1245 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * Option noprefix was introduced just for backward compatibility |
| 1248 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1249 | * the cpuset subsystem. |
| 1250 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1251 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1252 | return -EINVAL; |
| 1253 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1254 | |
| 1255 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1256 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1257 | return -EINVAL; |
| 1258 | |
| 1259 | /* |
| 1260 | * We either have to specify by name or by subsystems. (So all |
| 1261 | * empty hierarchies must have a name). |
| 1262 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1263 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1264 | return -EINVAL; |
| 1265 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1266 | /* |
| 1267 | * Grab references on all the modules we'll need, so the subsystems |
| 1268 | * don't dance around before rebind_subsystems attaches them. This may |
| 1269 | * take duplicate reference counts on a subsystem that's already used, |
| 1270 | * but rebind_subsystems handles this case. |
| 1271 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1272 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1273 | unsigned long bit = 1UL << i; |
| 1274 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1275 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1276 | continue; |
| 1277 | if (!try_module_get(subsys[i]->module)) { |
| 1278 | module_pin_failed = true; |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | if (module_pin_failed) { |
| 1283 | /* |
| 1284 | * oops, one of the modules was going away. this means that we |
| 1285 | * raced with a module_delete call, and to the user this is |
| 1286 | * essentially a "subsystem doesn't exist" case. |
| 1287 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1288 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1289 | /* drop refcounts only on the ones we took */ |
| 1290 | unsigned long bit = 1UL << i; |
| 1291 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1292 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1293 | continue; |
| 1294 | module_put(subsys[i]->module); |
| 1295 | } |
| 1296 | return -ENOENT; |
| 1297 | } |
| 1298 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1299 | return 0; |
| 1300 | } |
| 1301 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1302 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1303 | { |
| 1304 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1305 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1306 | unsigned long bit = 1UL << i; |
| 1307 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1308 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1309 | continue; |
| 1310 | module_put(subsys[i]->module); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1314 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1315 | { |
| 1316 | int ret = 0; |
| 1317 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1318 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1319 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1320 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1321 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1322 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1323 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1324 | return -EINVAL; |
| 1325 | } |
| 1326 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1327 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1328 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1329 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | |
| 1331 | /* See what subsystems are wanted */ |
| 1332 | ret = parse_cgroupfs_options(data, &opts); |
| 1333 | if (ret) |
| 1334 | goto out_unlock; |
| 1335 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1336 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1337 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1338 | task_tgid_nr(current), current->comm); |
| 1339 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1340 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1341 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1342 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1343 | /* Don't allow flags or name to change at remount */ |
| 1344 | if (opts.flags != root->flags || |
| 1345 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1346 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1347 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1348 | goto out_unlock; |
| 1349 | } |
| 1350 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1351 | /* |
| 1352 | * Clear out the files of subsystems that should be removed, do |
| 1353 | * this before rebind_subsystems, since rebind_subsystems may |
| 1354 | * change this hierarchy's subsys_list. |
| 1355 | */ |
| 1356 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1357 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1358 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1359 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1360 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1361 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1362 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1363 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1364 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1365 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1366 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1367 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1368 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1369 | if (opts.release_agent) |
| 1370 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1371 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1372 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1373 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1374 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1375 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1376 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1377 | return ret; |
| 1378 | } |
| 1379 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1380 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | .statfs = simple_statfs, |
| 1382 | .drop_inode = generic_delete_inode, |
| 1383 | .show_options = cgroup_show_options, |
| 1384 | .remount_fs = cgroup_remount, |
| 1385 | }; |
| 1386 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1387 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1388 | { |
| 1389 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1390 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1391 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1392 | INIT_LIST_HEAD(&cgrp->cset_links); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1393 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1394 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1395 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1396 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1397 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1398 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1399 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1400 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1401 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1402 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1403 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1404 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1405 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1406 | INIT_LIST_HEAD(&root->subsys_list); |
| 1407 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1408 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1409 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1410 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1411 | cgrp->name = &root_cgroup_name; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1412 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1413 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1416 | static int cgroup_init_root_id(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1417 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1418 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1419 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1420 | lockdep_assert_held(&cgroup_mutex); |
| 1421 | lockdep_assert_held(&cgroup_root_mutex); |
| 1422 | |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1423 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL); |
| 1424 | if (id < 0) |
| 1425 | return id; |
| 1426 | |
| 1427 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1432 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1433 | lockdep_assert_held(&cgroup_mutex); |
| 1434 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1435 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1436 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1437 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1438 | root->hierarchy_id = 0; |
| 1439 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1442 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1443 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1444 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1445 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1446 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1447 | /* If we asked for a name then it must match */ |
| 1448 | if (opts->name && strcmp(opts->name, root->name)) |
| 1449 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1450 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * If we asked for subsystems (or explicitly for no |
| 1453 | * subsystems) then they must match |
| 1454 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1455 | if ((opts->subsys_mask || opts->none) |
| 1456 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1457 | return 0; |
| 1458 | |
| 1459 | return 1; |
| 1460 | } |
| 1461 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1462 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1463 | { |
| 1464 | struct cgroupfs_root *root; |
| 1465 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1466 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1467 | return NULL; |
| 1468 | |
| 1469 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1470 | if (!root) |
| 1471 | return ERR_PTR(-ENOMEM); |
| 1472 | |
| 1473 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1474 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1475 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1476 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1477 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1478 | if (opts->release_agent) |
| 1479 | strcpy(root->release_agent_path, opts->release_agent); |
| 1480 | if (opts->name) |
| 1481 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1482 | if (opts->cpuset_clone_children) |
| 1483 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1484 | return root; |
| 1485 | } |
| 1486 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1487 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1488 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1489 | if (root) { |
| 1490 | /* hierarhcy ID shoulid already have been released */ |
| 1491 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1492 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1493 | ida_destroy(&root->cgroup_ida); |
| 1494 | kfree(root); |
| 1495 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1498 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1499 | { |
| 1500 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1501 | struct cgroup_sb_opts *opts = data; |
| 1502 | |
| 1503 | /* If we don't have a new root, we can't set up a new sb */ |
| 1504 | if (!opts->new_root) |
| 1505 | return -EINVAL; |
| 1506 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1507 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1508 | |
| 1509 | ret = set_anon_super(sb, NULL); |
| 1510 | if (ret) |
| 1511 | return ret; |
| 1512 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1513 | sb->s_fs_info = opts->new_root; |
| 1514 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1515 | |
| 1516 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1517 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1518 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1519 | sb->s_op = &cgroup_ops; |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1525 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1526 | static const struct dentry_operations cgroup_dops = { |
| 1527 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1528 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1529 | }; |
| 1530 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1531 | struct inode *inode = |
| 1532 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1533 | |
| 1534 | if (!inode) |
| 1535 | return -ENOMEM; |
| 1536 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1537 | inode->i_fop = &simple_dir_operations; |
| 1538 | inode->i_op = &cgroup_dir_inode_operations; |
| 1539 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1540 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1541 | sb->s_root = d_make_root(inode); |
| 1542 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1543 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1544 | /* for everything else we want ->d_op set */ |
| 1545 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1549 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1551 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1552 | { |
| 1553 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1554 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1555 | int ret = 0; |
| 1556 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1557 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1558 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | |
| 1560 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1561 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1562 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1563 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1564 | if (ret) |
| 1565 | goto out_err; |
| 1566 | |
| 1567 | /* |
| 1568 | * Allocate a new cgroup root. We may not need it if we're |
| 1569 | * reusing an existing hierarchy. |
| 1570 | */ |
| 1571 | new_root = cgroup_root_from_opts(&opts); |
| 1572 | if (IS_ERR(new_root)) { |
| 1573 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1574 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1575 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1576 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1577 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1578 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1579 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1581 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1582 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1583 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1586 | root = sb->s_fs_info; |
| 1587 | BUG_ON(!root); |
| 1588 | if (root == opts.new_root) { |
| 1589 | /* We used the new root structure, so this is a new hierarchy */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1590 | struct list_head tmp_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1591 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1592 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1593 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1594 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1595 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1596 | |
| 1597 | BUG_ON(sb->s_root != NULL); |
| 1598 | |
| 1599 | ret = cgroup_get_rootdir(sb); |
| 1600 | if (ret) |
| 1601 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1602 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1603 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1604 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1606 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1607 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1608 | /* Check for name clashes with existing mounts */ |
| 1609 | ret = -EBUSY; |
| 1610 | if (strlen(root->name)) |
| 1611 | for_each_active_root(existing_root) |
| 1612 | if (!strcmp(existing_root->name, root->name)) |
| 1613 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1614 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1615 | /* |
| 1616 | * We're accessing css_set_count without locking |
| 1617 | * css_set_lock here, but that's OK - it can only be |
| 1618 | * increased by someone holding cgroup_lock, and |
| 1619 | * that's us. The worst that can happen is that we |
| 1620 | * have some link structures left over |
| 1621 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1622 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1623 | if (ret) |
| 1624 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1625 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1626 | ret = cgroup_init_root_id(root); |
| 1627 | if (ret) |
| 1628 | goto unlock_drop; |
| 1629 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1630 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1631 | if (ret == -EBUSY) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1632 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1633 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1634 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1635 | /* |
| 1636 | * There must be no failure case after here, since rebinding |
| 1637 | * takes care of subsystems' refcounts, which are explicitly |
| 1638 | * dropped in the failure exit path. |
| 1639 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1640 | |
| 1641 | /* EBUSY should be the only error here */ |
| 1642 | BUG_ON(ret); |
| 1643 | |
| 1644 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1645 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1647 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | root->top_cgroup.dentry = sb->s_root; |
| 1649 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1650 | /* Link the top cgroup in this hierarchy into all |
| 1651 | * the css_set objects */ |
| 1652 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1653 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1654 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1655 | write_unlock(&css_set_lock); |
| 1656 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1657 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1658 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1659 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1660 | BUG_ON(root->number_of_cgroups != 1); |
| 1661 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1662 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1663 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1664 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1665 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1666 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1667 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1668 | } else { |
| 1669 | /* |
| 1670 | * We re-used an existing hierarchy - the new root (if |
| 1671 | * any) is not needed |
| 1672 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1673 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1674 | |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1675 | if (root->flags != opts.flags) { |
| 1676 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1677 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1678 | ret = -EINVAL; |
| 1679 | goto drop_new_super; |
| 1680 | } else { |
| 1681 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1682 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1685 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1686 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1689 | kfree(opts.release_agent); |
| 1690 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1691 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1693 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1694 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1695 | mutex_unlock(&cgroup_root_mutex); |
| 1696 | mutex_unlock(&cgroup_mutex); |
| 1697 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1698 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1699 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1700 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1701 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1702 | out_err: |
| 1703 | kfree(opts.release_agent); |
| 1704 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1705 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1709 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1710 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1711 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1712 | int ret; |
| 1713 | |
| 1714 | BUG_ON(!root); |
| 1715 | |
| 1716 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1717 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1718 | |
| 1719 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1720 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1721 | |
| 1722 | /* Rebind all subsystems back to the default hierarchy */ |
| 1723 | ret = rebind_subsystems(root, 0); |
| 1724 | /* Shouldn't be able to fail ... */ |
| 1725 | BUG_ON(ret); |
| 1726 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1727 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1728 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1729 | * root cgroup |
| 1730 | */ |
| 1731 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1732 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1733 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1734 | list_del(&link->cset_link); |
| 1735 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1736 | kfree(link); |
| 1737 | } |
| 1738 | write_unlock(&css_set_lock); |
| 1739 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1740 | if (!list_empty(&root->root_list)) { |
| 1741 | list_del(&root->root_list); |
| 1742 | root_count--; |
| 1743 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1744 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1745 | cgroup_exit_root_id(root); |
| 1746 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1747 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1748 | mutex_unlock(&cgroup_mutex); |
| 1749 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1750 | simple_xattrs_free(&cgrp->xattrs); |
| 1751 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1752 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1753 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static struct file_system_type cgroup_fs_type = { |
| 1757 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1758 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | .kill_sb = cgroup_kill_sb, |
| 1760 | }; |
| 1761 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1762 | static struct kobject *cgroup_kobj; |
| 1763 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1764 | /** |
| 1765 | * cgroup_path - generate the path of a cgroup |
| 1766 | * @cgrp: the cgroup in question |
| 1767 | * @buf: the buffer to write the path into |
| 1768 | * @buflen: the length of the buffer |
| 1769 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1770 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1771 | * |
| 1772 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1773 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1774 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1775 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1776 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1777 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1778 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1779 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1780 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1781 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1782 | if (!cgrp->parent) { |
| 1783 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1784 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
| 1787 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1788 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1789 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1790 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1791 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1792 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1793 | const char *name = cgroup_name(cgrp); |
| 1794 | int len; |
| 1795 | |
| 1796 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1797 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1798 | goto out; |
| 1799 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1800 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1801 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1802 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1803 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1804 | |
| 1805 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1806 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1807 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | out: |
| 1810 | rcu_read_unlock(); |
| 1811 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1813 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1814 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1815 | /** |
| 1816 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1817 | * @task: target task |
| 1818 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1819 | * @buf: the buffer to write the path into |
| 1820 | * @buflen: the length of the buffer |
| 1821 | * |
| 1822 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1823 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1824 | * be used inside locks used by cgroup controller callbacks. |
| 1825 | */ |
| 1826 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1827 | char *buf, size_t buflen) |
| 1828 | { |
| 1829 | struct cgroupfs_root *root; |
| 1830 | struct cgroup *cgrp = NULL; |
| 1831 | int ret = -ENOENT; |
| 1832 | |
| 1833 | mutex_lock(&cgroup_mutex); |
| 1834 | |
| 1835 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1836 | if (root) { |
| 1837 | cgrp = task_cgroup_from_root(task, root); |
| 1838 | ret = cgroup_path(cgrp, buf, buflen); |
| 1839 | } |
| 1840 | |
| 1841 | mutex_unlock(&cgroup_mutex); |
| 1842 | |
| 1843 | return ret; |
| 1844 | } |
| 1845 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1846 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1847 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1848 | * Control Group taskset |
| 1849 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1850 | struct task_and_cgroup { |
| 1851 | struct task_struct *task; |
| 1852 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1853 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1854 | }; |
| 1855 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1856 | struct cgroup_taskset { |
| 1857 | struct task_and_cgroup single; |
| 1858 | struct flex_array *tc_array; |
| 1859 | int tc_array_len; |
| 1860 | int idx; |
| 1861 | struct cgroup *cur_cgrp; |
| 1862 | }; |
| 1863 | |
| 1864 | /** |
| 1865 | * cgroup_taskset_first - reset taskset and return the first task |
| 1866 | * @tset: taskset of interest |
| 1867 | * |
| 1868 | * @tset iteration is initialized and the first task is returned. |
| 1869 | */ |
| 1870 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1871 | { |
| 1872 | if (tset->tc_array) { |
| 1873 | tset->idx = 0; |
| 1874 | return cgroup_taskset_next(tset); |
| 1875 | } else { |
| 1876 | tset->cur_cgrp = tset->single.cgrp; |
| 1877 | return tset->single.task; |
| 1878 | } |
| 1879 | } |
| 1880 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1881 | |
| 1882 | /** |
| 1883 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1884 | * @tset: taskset of interest |
| 1885 | * |
| 1886 | * Return the next task in @tset. Iteration must have been initialized |
| 1887 | * with cgroup_taskset_first(). |
| 1888 | */ |
| 1889 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1890 | { |
| 1891 | struct task_and_cgroup *tc; |
| 1892 | |
| 1893 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1894 | return NULL; |
| 1895 | |
| 1896 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1897 | tset->cur_cgrp = tc->cgrp; |
| 1898 | return tc->task; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1901 | |
| 1902 | /** |
| 1903 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1904 | * @tset: taskset of interest |
| 1905 | * |
| 1906 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1907 | * function must be preceded by either cgroup_taskset_first() or |
| 1908 | * cgroup_taskset_next(). |
| 1909 | */ |
| 1910 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1911 | { |
| 1912 | return tset->cur_cgrp; |
| 1913 | } |
| 1914 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1915 | |
| 1916 | /** |
| 1917 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1918 | * @tset: taskset of interest |
| 1919 | */ |
| 1920 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1921 | { |
| 1922 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1923 | } |
| 1924 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1925 | |
| 1926 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1927 | /* |
| 1928 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1929 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1930 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1931 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1932 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1933 | struct task_struct *tsk, |
| 1934 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1935 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1936 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1937 | |
| 1938 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1939 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1940 | * setting such that we can't race against cgroup_exit() changing the |
| 1941 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1943 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1944 | old_cset = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1945 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1946 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1947 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1948 | task_unlock(tsk); |
| 1949 | |
| 1950 | /* Update the css_set linked lists if we're using them */ |
| 1951 | write_lock(&css_set_lock); |
| 1952 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1953 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1954 | write_unlock(&css_set_lock); |
| 1955 | |
| 1956 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1957 | * We just gained a reference on old_cset by taking it from the |
| 1958 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1959 | * we're safe to drop it here; it will be freed under RCU. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1960 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1961 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1962 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1965 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1966 | * 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] | 1967 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1968 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1969 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1971 | * 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] | 1972 | * 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] | 1973 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1974 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1975 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1976 | { |
| 1977 | int retval, i, group_size; |
| 1978 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1979 | struct cgroupfs_root *root = cgrp->root; |
| 1980 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1981 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1982 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1983 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1984 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1985 | |
| 1986 | /* |
| 1987 | * step 0: in order to do expensive, possibly blocking operations for |
| 1988 | * every thread, we cannot iterate the thread group list, since it needs |
| 1989 | * 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] | 1990 | * group - group_rwsem prevents new threads from appearing, and if |
| 1991 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1992 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1993 | if (threadgroup) |
| 1994 | group_size = get_nr_threads(tsk); |
| 1995 | else |
| 1996 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1997 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1999 | if (!group) |
| 2000 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2001 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2002 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2003 | if (retval) |
| 2004 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2005 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2007 | /* |
| 2008 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2009 | * already PF_EXITING could be freed from underneath us unless we |
| 2010 | * take an rcu_read_lock. |
| 2011 | */ |
| 2012 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2014 | struct task_and_cgroup ent; |
| 2015 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2016 | /* @tsk either already exited or can't exit until the end */ |
| 2017 | if (tsk->flags & PF_EXITING) |
| 2018 | continue; |
| 2019 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2020 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2021 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2022 | ent.task = tsk; |
| 2023 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2024 | /* nothing to do if this task is already in the cgroup */ |
| 2025 | if (ent.cgrp == cgrp) |
| 2026 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2027 | /* |
| 2028 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2029 | * earlier, but it's good form to communicate our expectations. |
| 2030 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2031 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2032 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2033 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2034 | |
| 2035 | if (!threadgroup) |
| 2036 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2037 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2038 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2039 | /* remember the number of threads in the array for later. */ |
| 2040 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2041 | tset.tc_array = group; |
| 2042 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2043 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2044 | /* methods shouldn't be called if no task is actually migrating */ |
| 2045 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2046 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2047 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2048 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | /* |
| 2050 | * step 1: check that we can legitimately attach to the cgroup. |
| 2051 | */ |
| 2052 | for_each_subsys(root, ss) { |
| 2053 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2054 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2055 | if (retval) { |
| 2056 | failed_ss = ss; |
| 2057 | goto out_cancel_attach; |
| 2058 | } |
| 2059 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | /* |
| 2063 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2064 | * we use find_css_set, which allocates a new one if necessary. |
| 2065 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2066 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2067 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2068 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2069 | if (!tc->cg) { |
| 2070 | retval = -ENOMEM; |
| 2071 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2076 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2077 | * proceed to move all tasks to the new cgroup. There are no |
| 2078 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 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); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2082 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2083 | } |
| 2084 | /* nothing is sensitive to fork() after this point. */ |
| 2085 | |
| 2086 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2087 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | */ |
| 2089 | for_each_subsys(root, ss) { |
| 2090 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2091 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * step 5: success! and cleanup |
| 2096 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2097 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2098 | out_put_css_set_refs: |
| 2099 | if (retval) { |
| 2100 | for (i = 0; i < group_size; i++) { |
| 2101 | tc = flex_array_get(group, i); |
| 2102 | if (!tc->cg) |
| 2103 | break; |
| 2104 | put_css_set(tc->cg); |
| 2105 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2106 | } |
| 2107 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | if (retval) { |
| 2109 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2110 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2112 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2113 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2116 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2117 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2118 | return retval; |
| 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * 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] | 2123 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2124 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2125 | */ |
| 2126 | 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] | 2127 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2128 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2129 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2130 | int ret; |
| 2131 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2132 | if (!cgroup_lock_live_group(cgrp)) |
| 2133 | return -ENODEV; |
| 2134 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2135 | retry_find_task: |
| 2136 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2137 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2138 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2139 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2140 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2141 | ret= -ESRCH; |
| 2142 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2143 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2144 | /* |
| 2145 | * even if we're attaching all tasks in the thread group, we |
| 2146 | * only need to check permissions on one of them. |
| 2147 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2148 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2149 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2150 | !uid_eq(cred->euid, tcred->uid) && |
| 2151 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2152 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2153 | ret = -EACCES; |
| 2154 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2155 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2156 | } else |
| 2157 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2158 | |
| 2159 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2160 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2161 | |
| 2162 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2163 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2164 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2165 | * with no rt_runtime allocated. Just say no. |
| 2166 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2167 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2168 | ret = -EINVAL; |
| 2169 | rcu_read_unlock(); |
| 2170 | goto out_unlock_cgroup; |
| 2171 | } |
| 2172 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2173 | get_task_struct(tsk); |
| 2174 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2175 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2176 | threadgroup_lock(tsk); |
| 2177 | if (threadgroup) { |
| 2178 | if (!thread_group_leader(tsk)) { |
| 2179 | /* |
| 2180 | * a race with de_thread from another thread's exec() |
| 2181 | * may strip us of our leadership, if this happens, |
| 2182 | * there is no choice but to throw this task away and |
| 2183 | * try again; this is |
| 2184 | * "double-double-toil-and-trouble-check locking". |
| 2185 | */ |
| 2186 | threadgroup_unlock(tsk); |
| 2187 | put_task_struct(tsk); |
| 2188 | goto retry_find_task; |
| 2189 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2193 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2194 | threadgroup_unlock(tsk); |
| 2195 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2196 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2197 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2198 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2199 | return ret; |
| 2200 | } |
| 2201 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2202 | /** |
| 2203 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2204 | * @from: attach to all cgroups of a given task |
| 2205 | * @tsk: the task to be attached |
| 2206 | */ |
| 2207 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2208 | { |
| 2209 | struct cgroupfs_root *root; |
| 2210 | int retval = 0; |
| 2211 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2212 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2213 | for_each_active_root(root) { |
| 2214 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2215 | |
| 2216 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2217 | if (retval) |
| 2218 | break; |
| 2219 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2220 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2221 | |
| 2222 | return retval; |
| 2223 | } |
| 2224 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2225 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2226 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2227 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2228 | return attach_task_by_pid(cgrp, pid, false); |
| 2229 | } |
| 2230 | |
| 2231 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2232 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2233 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2234 | } |
| 2235 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2236 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2237 | const char *buffer) |
| 2238 | { |
| 2239 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2240 | if (strlen(buffer) >= PATH_MAX) |
| 2241 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2242 | if (!cgroup_lock_live_group(cgrp)) |
| 2243 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2244 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2245 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2246 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2247 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2248 | return 0; |
| 2249 | } |
| 2250 | |
| 2251 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2252 | struct seq_file *seq) |
| 2253 | { |
| 2254 | if (!cgroup_lock_live_group(cgrp)) |
| 2255 | return -ENODEV; |
| 2256 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2257 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2258 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2259 | return 0; |
| 2260 | } |
| 2261 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2262 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2263 | struct seq_file *seq) |
| 2264 | { |
| 2265 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2266 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2269 | /* A buffer size big enough for numbers or short strings */ |
| 2270 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2271 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2272 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2273 | struct file *file, |
| 2274 | const char __user *userbuf, |
| 2275 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2276 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2277 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2278 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2279 | char *end; |
| 2280 | |
| 2281 | if (!nbytes) |
| 2282 | return -EINVAL; |
| 2283 | if (nbytes >= sizeof(buffer)) |
| 2284 | return -E2BIG; |
| 2285 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2286 | return -EFAULT; |
| 2287 | |
| 2288 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2289 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2290 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2291 | if (*end) |
| 2292 | return -EINVAL; |
| 2293 | retval = cft->write_u64(cgrp, cft, val); |
| 2294 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2295 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2296 | if (*end) |
| 2297 | return -EINVAL; |
| 2298 | retval = cft->write_s64(cgrp, cft, val); |
| 2299 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2300 | if (!retval) |
| 2301 | retval = nbytes; |
| 2302 | return retval; |
| 2303 | } |
| 2304 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2305 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2306 | struct file *file, |
| 2307 | const char __user *userbuf, |
| 2308 | size_t nbytes, loff_t *unused_ppos) |
| 2309 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2310 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2311 | int retval = 0; |
| 2312 | size_t max_bytes = cft->max_write_len; |
| 2313 | char *buffer = local_buffer; |
| 2314 | |
| 2315 | if (!max_bytes) |
| 2316 | max_bytes = sizeof(local_buffer) - 1; |
| 2317 | if (nbytes >= max_bytes) |
| 2318 | return -E2BIG; |
| 2319 | /* Allocate a dynamic buffer if we need one */ |
| 2320 | if (nbytes >= sizeof(local_buffer)) { |
| 2321 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2322 | if (buffer == NULL) |
| 2323 | return -ENOMEM; |
| 2324 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2325 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2326 | retval = -EFAULT; |
| 2327 | goto out; |
| 2328 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2329 | |
| 2330 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2331 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2332 | if (!retval) |
| 2333 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2334 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2335 | if (buffer != local_buffer) |
| 2336 | kfree(buffer); |
| 2337 | return retval; |
| 2338 | } |
| 2339 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2340 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2341 | size_t nbytes, loff_t *ppos) |
| 2342 | { |
| 2343 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2344 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2345 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2346 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2347 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2348 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2349 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2350 | if (cft->write_u64 || cft->write_s64) |
| 2351 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2352 | if (cft->write_string) |
| 2353 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2354 | if (cft->trigger) { |
| 2355 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2356 | return ret ? ret : nbytes; |
| 2357 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2358 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2361 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2362 | struct file *file, |
| 2363 | char __user *buf, size_t nbytes, |
| 2364 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2365 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2366 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2367 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2368 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2369 | |
| 2370 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2371 | } |
| 2372 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2373 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2374 | struct file *file, |
| 2375 | char __user *buf, size_t nbytes, |
| 2376 | loff_t *ppos) |
| 2377 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2378 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2379 | s64 val = cft->read_s64(cgrp, cft); |
| 2380 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2381 | |
| 2382 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2383 | } |
| 2384 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2385 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2386 | size_t nbytes, loff_t *ppos) |
| 2387 | { |
| 2388 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2389 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2390 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2391 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2392 | return -ENODEV; |
| 2393 | |
| 2394 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2395 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2396 | if (cft->read_u64) |
| 2397 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2398 | if (cft->read_s64) |
| 2399 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2400 | return -EINVAL; |
| 2401 | } |
| 2402 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2403 | /* |
| 2404 | * seqfile ops/methods for returning structured data. Currently just |
| 2405 | * supports string->u64 maps, but can be extended in future. |
| 2406 | */ |
| 2407 | |
| 2408 | struct cgroup_seqfile_state { |
| 2409 | struct cftype *cft; |
| 2410 | struct cgroup *cgroup; |
| 2411 | }; |
| 2412 | |
| 2413 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2414 | { |
| 2415 | struct seq_file *sf = cb->state; |
| 2416 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2417 | } |
| 2418 | |
| 2419 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2420 | { |
| 2421 | struct cgroup_seqfile_state *state = m->private; |
| 2422 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2423 | if (cft->read_map) { |
| 2424 | struct cgroup_map_cb cb = { |
| 2425 | .fill = cgroup_map_add, |
| 2426 | .state = m, |
| 2427 | }; |
| 2428 | return cft->read_map(state->cgroup, cft, &cb); |
| 2429 | } |
| 2430 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2433 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2434 | { |
| 2435 | struct seq_file *seq = file->private_data; |
| 2436 | kfree(seq->private); |
| 2437 | return single_release(inode, file); |
| 2438 | } |
| 2439 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2440 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2441 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2442 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2443 | .llseek = seq_lseek, |
| 2444 | .release = cgroup_seqfile_release, |
| 2445 | }; |
| 2446 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2447 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2448 | { |
| 2449 | int err; |
| 2450 | struct cftype *cft; |
| 2451 | |
| 2452 | err = generic_file_open(inode, file); |
| 2453 | if (err) |
| 2454 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2455 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2456 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2457 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2458 | struct cgroup_seqfile_state *state; |
| 2459 | |
| 2460 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2461 | if (!state) |
| 2462 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2463 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2464 | state->cft = cft; |
| 2465 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2466 | file->f_op = &cgroup_seqfile_operations; |
| 2467 | err = single_open(file, cgroup_seqfile_show, state); |
| 2468 | if (err < 0) |
| 2469 | kfree(state); |
| 2470 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2471 | err = cft->open(inode, file); |
| 2472 | else |
| 2473 | err = 0; |
| 2474 | |
| 2475 | return err; |
| 2476 | } |
| 2477 | |
| 2478 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2479 | { |
| 2480 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2481 | if (cft->release) |
| 2482 | return cft->release(inode, file); |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2488 | */ |
| 2489 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2490 | struct inode *new_dir, struct dentry *new_dentry) |
| 2491 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2492 | int ret; |
| 2493 | struct cgroup_name *name, *old_name; |
| 2494 | struct cgroup *cgrp; |
| 2495 | |
| 2496 | /* |
| 2497 | * It's convinient to use parent dir's i_mutex to protected |
| 2498 | * cgrp->name. |
| 2499 | */ |
| 2500 | lockdep_assert_held(&old_dir->i_mutex); |
| 2501 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2502 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2503 | return -ENOTDIR; |
| 2504 | if (new_dentry->d_inode) |
| 2505 | return -EEXIST; |
| 2506 | if (old_dir != new_dir) |
| 2507 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2508 | |
| 2509 | cgrp = __d_cgrp(old_dentry); |
| 2510 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2511 | /* |
| 2512 | * This isn't a proper migration and its usefulness is very |
| 2513 | * limited. Disallow if sane_behavior. |
| 2514 | */ |
| 2515 | if (cgroup_sane_behavior(cgrp)) |
| 2516 | return -EPERM; |
| 2517 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2518 | name = cgroup_alloc_name(new_dentry); |
| 2519 | if (!name) |
| 2520 | return -ENOMEM; |
| 2521 | |
| 2522 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2523 | if (ret) { |
| 2524 | kfree(name); |
| 2525 | return ret; |
| 2526 | } |
| 2527 | |
| 2528 | old_name = cgrp->name; |
| 2529 | rcu_assign_pointer(cgrp->name, name); |
| 2530 | |
| 2531 | kfree_rcu(old_name, rcu_head); |
| 2532 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2535 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2536 | { |
| 2537 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2538 | return &__d_cgrp(dentry)->xattrs; |
| 2539 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2540 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | static inline int xattr_enabled(struct dentry *dentry) |
| 2544 | { |
| 2545 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2546 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | static bool is_valid_xattr(const char *name) |
| 2550 | { |
| 2551 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2552 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2553 | return true; |
| 2554 | return false; |
| 2555 | } |
| 2556 | |
| 2557 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2558 | const void *val, size_t size, int flags) |
| 2559 | { |
| 2560 | if (!xattr_enabled(dentry)) |
| 2561 | return -EOPNOTSUPP; |
| 2562 | if (!is_valid_xattr(name)) |
| 2563 | return -EINVAL; |
| 2564 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2565 | } |
| 2566 | |
| 2567 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2568 | { |
| 2569 | if (!xattr_enabled(dentry)) |
| 2570 | return -EOPNOTSUPP; |
| 2571 | if (!is_valid_xattr(name)) |
| 2572 | return -EINVAL; |
| 2573 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2574 | } |
| 2575 | |
| 2576 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2577 | void *buf, size_t size) |
| 2578 | { |
| 2579 | if (!xattr_enabled(dentry)) |
| 2580 | return -EOPNOTSUPP; |
| 2581 | if (!is_valid_xattr(name)) |
| 2582 | return -EINVAL; |
| 2583 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2584 | } |
| 2585 | |
| 2586 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2587 | { |
| 2588 | if (!xattr_enabled(dentry)) |
| 2589 | return -EOPNOTSUPP; |
| 2590 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2591 | } |
| 2592 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2593 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2594 | .read = cgroup_file_read, |
| 2595 | .write = cgroup_file_write, |
| 2596 | .llseek = generic_file_llseek, |
| 2597 | .open = cgroup_file_open, |
| 2598 | .release = cgroup_file_release, |
| 2599 | }; |
| 2600 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2601 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2602 | .setxattr = cgroup_setxattr, |
| 2603 | .getxattr = cgroup_getxattr, |
| 2604 | .listxattr = cgroup_listxattr, |
| 2605 | .removexattr = cgroup_removexattr, |
| 2606 | }; |
| 2607 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2608 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2609 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2610 | .mkdir = cgroup_mkdir, |
| 2611 | .rmdir = cgroup_rmdir, |
| 2612 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2613 | .setxattr = cgroup_setxattr, |
| 2614 | .getxattr = cgroup_getxattr, |
| 2615 | .listxattr = cgroup_listxattr, |
| 2616 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2617 | }; |
| 2618 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2619 | 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] | 2620 | { |
| 2621 | if (dentry->d_name.len > NAME_MAX) |
| 2622 | return ERR_PTR(-ENAMETOOLONG); |
| 2623 | d_add(dentry, NULL); |
| 2624 | return NULL; |
| 2625 | } |
| 2626 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2627 | /* |
| 2628 | * Check if a file is a control file |
| 2629 | */ |
| 2630 | static inline struct cftype *__file_cft(struct file *file) |
| 2631 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2632 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2633 | return ERR_PTR(-EINVAL); |
| 2634 | return __d_cft(file->f_dentry); |
| 2635 | } |
| 2636 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2637 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2638 | struct super_block *sb) |
| 2639 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2640 | struct inode *inode; |
| 2641 | |
| 2642 | if (!dentry) |
| 2643 | return -ENOENT; |
| 2644 | if (dentry->d_inode) |
| 2645 | return -EEXIST; |
| 2646 | |
| 2647 | inode = cgroup_new_inode(mode, sb); |
| 2648 | if (!inode) |
| 2649 | return -ENOMEM; |
| 2650 | |
| 2651 | if (S_ISDIR(mode)) { |
| 2652 | inode->i_op = &cgroup_dir_inode_operations; |
| 2653 | inode->i_fop = &simple_dir_operations; |
| 2654 | |
| 2655 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2656 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2657 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2658 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2659 | /* |
| 2660 | * Control reaches here with cgroup_mutex held. |
| 2661 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2662 | * want to populate it immediately without releasing |
| 2663 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2664 | * yet, trylock will always succeed without affecting |
| 2665 | * lockdep checks. |
| 2666 | */ |
| 2667 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2668 | } else if (S_ISREG(mode)) { |
| 2669 | inode->i_size = 0; |
| 2670 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2671 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2672 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2673 | d_instantiate(dentry, inode); |
| 2674 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2678 | /** |
| 2679 | * cgroup_file_mode - deduce file mode of a control file |
| 2680 | * @cft: the control file in question |
| 2681 | * |
| 2682 | * returns cft->mode if ->mode is not 0 |
| 2683 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2684 | * returns S_IRUGO if it has only a read handler |
| 2685 | * returns S_IWUSR if it has only a write hander |
| 2686 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2687 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2688 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2689 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2690 | |
| 2691 | if (cft->mode) |
| 2692 | return cft->mode; |
| 2693 | |
| 2694 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2695 | cft->read_map || cft->read_seq_string) |
| 2696 | mode |= S_IRUGO; |
| 2697 | |
| 2698 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2699 | cft->write_string || cft->trigger) |
| 2700 | mode |= S_IWUSR; |
| 2701 | |
| 2702 | return mode; |
| 2703 | } |
| 2704 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2705 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2706 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2707 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2708 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2709 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2710 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2711 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2712 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2713 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2714 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2715 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2716 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2717 | strcpy(name, subsys->name); |
| 2718 | strcat(name, "."); |
| 2719 | } |
| 2720 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2721 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2722 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2723 | |
| 2724 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2725 | if (!cfe) |
| 2726 | return -ENOMEM; |
| 2727 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2728 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2729 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2730 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2731 | goto out; |
| 2732 | } |
| 2733 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2734 | cfe->type = (void *)cft; |
| 2735 | cfe->dentry = dentry; |
| 2736 | dentry->d_fsdata = cfe; |
| 2737 | simple_xattrs_init(&cfe->xattrs); |
| 2738 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2739 | mode = cgroup_file_mode(cft); |
| 2740 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2741 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2742 | list_add_tail(&cfe->node, &parent->files); |
| 2743 | cfe = NULL; |
| 2744 | } |
| 2745 | dput(dentry); |
| 2746 | out: |
| 2747 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2748 | return error; |
| 2749 | } |
| 2750 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2751 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2752 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2753 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2754 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2755 | int err, ret = 0; |
| 2756 | |
| 2757 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2758 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2759 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2760 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2761 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2762 | continue; |
| 2763 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2764 | continue; |
| 2765 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2766 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2767 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2768 | if (err) |
| 2769 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2770 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2771 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2772 | } else { |
| 2773 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2774 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2775 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2776 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2779 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2780 | |
| 2781 | static void cgroup_cfts_prepare(void) |
| 2782 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2783 | { |
| 2784 | /* |
| 2785 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2786 | * the existing cgroups under cgroup_mutex and create files. |
| 2787 | * Instead, we increment reference on all cgroups and build list of |
| 2788 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2789 | * exclusive access to the field. |
| 2790 | */ |
| 2791 | mutex_lock(&cgroup_cft_mutex); |
| 2792 | mutex_lock(&cgroup_mutex); |
| 2793 | } |
| 2794 | |
| 2795 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2796 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2797 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2798 | { |
| 2799 | LIST_HEAD(pending); |
| 2800 | struct cgroup *cgrp, *n; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame^] | 2801 | struct super_block *sb = ss->root->sb; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2802 | |
| 2803 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame^] | 2804 | if (cfts && ss->root != &rootnode && |
| 2805 | atomic_inc_not_zero(sb->s_active)) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2806 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2807 | dget(cgrp->dentry); |
| 2808 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2809 | } |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame^] | 2810 | } else { |
| 2811 | sb = NULL; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 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); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2825 | if (!cgroup_is_dead(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 | |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame^] | 2834 | if (sb) |
| 2835 | deactivate_super(sb); |
| 2836 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2837 | mutex_unlock(&cgroup_cft_mutex); |
| 2838 | } |
| 2839 | |
| 2840 | /** |
| 2841 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2842 | * @ss: target cgroup subsystem |
| 2843 | * @cfts: zero-length name terminated array of cftypes |
| 2844 | * |
| 2845 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2846 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2847 | * have them too. This function can be called anytime whether @ss is |
| 2848 | * attached or not. |
| 2849 | * |
| 2850 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2851 | * function currently returns 0 as long as @cfts registration is successful |
| 2852 | * even if some file creation attempts on existing cgroups fail. |
| 2853 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2854 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2855 | { |
| 2856 | struct cftype_set *set; |
| 2857 | |
| 2858 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2859 | if (!set) |
| 2860 | return -ENOMEM; |
| 2861 | |
| 2862 | cgroup_cfts_prepare(); |
| 2863 | set->cfts = cfts; |
| 2864 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2865 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2866 | |
| 2867 | return 0; |
| 2868 | } |
| 2869 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2870 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2871 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2872 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2873 | * @ss: target cgroup subsystem |
| 2874 | * @cfts: zero-length name terminated array of cftypes |
| 2875 | * |
| 2876 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2877 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2878 | * won't have them either. This function can be called anytime whether @ss |
| 2879 | * is attached or not. |
| 2880 | * |
| 2881 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2882 | * registered with @ss. |
| 2883 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2884 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2885 | { |
| 2886 | struct cftype_set *set; |
| 2887 | |
| 2888 | cgroup_cfts_prepare(); |
| 2889 | |
| 2890 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2891 | if (set->cfts == cfts) { |
| 2892 | list_del_init(&set->node); |
| 2893 | cgroup_cfts_commit(ss, cfts, false); |
| 2894 | return 0; |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | cgroup_cfts_commit(ss, NULL, false); |
| 2899 | return -ENOENT; |
| 2900 | } |
| 2901 | |
| 2902 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2903 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2904 | * @cgrp: the cgroup in question |
| 2905 | * |
| 2906 | * Return the number of tasks in the cgroup. |
| 2907 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2908 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2909 | { |
| 2910 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2911 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2912 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2913 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2914 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2915 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2916 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2917 | return count; |
| 2918 | } |
| 2919 | |
| 2920 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2921 | * Advance a list_head iterator. The iterator should be positioned at |
| 2922 | * the start of a css_set |
| 2923 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2924 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2925 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2926 | struct list_head *l = it->cset_link; |
| 2927 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2928 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2929 | |
| 2930 | /* Advance to the next non-empty css_set */ |
| 2931 | do { |
| 2932 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2933 | if (l == &cgrp->cset_links) { |
| 2934 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2935 | return; |
| 2936 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2937 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2938 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2939 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2940 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2941 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2944 | /* |
| 2945 | * To reduce the fork() overhead for systems that are not actually |
| 2946 | * using their cgroups capability, we don't maintain the lists running |
| 2947 | * through each css_set to its tasks until we see the list actually |
| 2948 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2949 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2950 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2951 | { |
| 2952 | struct task_struct *p, *g; |
| 2953 | write_lock(&css_set_lock); |
| 2954 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2955 | /* |
| 2956 | * We need tasklist_lock because RCU is not safe against |
| 2957 | * while_each_thread(). Besides, a forking task that has passed |
| 2958 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2959 | * is not guaranteed to have its child immediately visible in the |
| 2960 | * tasklist if we walk through it with RCU. |
| 2961 | */ |
| 2962 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2963 | do_each_thread(g, p) { |
| 2964 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2965 | /* |
| 2966 | * We should check if the process is exiting, otherwise |
| 2967 | * it will race with cgroup_exit() in that the list |
| 2968 | * entry won't be deleted though the process has exited. |
| 2969 | */ |
| 2970 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2971 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2972 | task_unlock(p); |
| 2973 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2974 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2975 | write_unlock(&css_set_lock); |
| 2976 | } |
| 2977 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2978 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2979 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 2980 | * @pos: the current cgroup |
| 2981 | * |
| 2982 | * This function returns the next sibling of @pos and should be called |
| 2983 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 2984 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 2985 | * state. |
| 2986 | */ |
| 2987 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 2988 | { |
| 2989 | struct cgroup *next; |
| 2990 | |
| 2991 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2992 | |
| 2993 | /* |
| 2994 | * @pos could already have been removed. Once a cgroup is removed, |
| 2995 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 2996 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 2997 | * before the cgroup is taken off the ->sibling list, if we see it |
| 2998 | * unasserted, it's guaranteed that the next sibling hasn't |
| 2999 | * finished its grace period even if it's already removed, and thus |
| 3000 | * safe to dereference from this RCU critical section. If |
| 3001 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3002 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3003 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3004 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3005 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3006 | if (&next->sibling != &pos->parent->children) |
| 3007 | return next; |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * Can't dereference the next pointer. Each cgroup is given a |
| 3013 | * monotonically increasing unique serial number and always |
| 3014 | * appended to the sibling list, so the next one can be found by |
| 3015 | * walking the parent's children until we see a cgroup with higher |
| 3016 | * serial number than @pos's. |
| 3017 | * |
| 3018 | * While this path can be slow, it's taken only when either the |
| 3019 | * current cgroup is removed or iteration and removal race. |
| 3020 | */ |
| 3021 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3022 | if (next->serial_nr > pos->serial_nr) |
| 3023 | return next; |
| 3024 | return NULL; |
| 3025 | } |
| 3026 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3027 | |
| 3028 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3029 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3030 | * @pos: the current position (%NULL to initiate traversal) |
| 3031 | * @cgroup: cgroup whose descendants to walk |
| 3032 | * |
| 3033 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3034 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3035 | * |
| 3036 | * While this function requires RCU read locking, it doesn't require the |
| 3037 | * whole traversal to be contained in a single RCU critical section. This |
| 3038 | * function will return the correct next descendant as long as both @pos |
| 3039 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3040 | */ |
| 3041 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3042 | struct cgroup *cgroup) |
| 3043 | { |
| 3044 | struct cgroup *next; |
| 3045 | |
| 3046 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3047 | |
| 3048 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3049 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3050 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3051 | |
| 3052 | /* visit the first child if exists */ |
| 3053 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3054 | if (next) |
| 3055 | return next; |
| 3056 | |
| 3057 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3058 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3059 | next = cgroup_next_sibling(pos); |
| 3060 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3061 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3062 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3063 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3064 | |
| 3065 | return NULL; |
| 3066 | } |
| 3067 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3068 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3069 | /** |
| 3070 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3071 | * @pos: cgroup of interest |
| 3072 | * |
| 3073 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3074 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3075 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3076 | * |
| 3077 | * While this function requires RCU read locking, it doesn't require the |
| 3078 | * whole traversal to be contained in a single RCU critical section. This |
| 3079 | * function will return the correct rightmost descendant as long as @pos is |
| 3080 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3081 | */ |
| 3082 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3083 | { |
| 3084 | struct cgroup *last, *tmp; |
| 3085 | |
| 3086 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3087 | |
| 3088 | do { |
| 3089 | last = pos; |
| 3090 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3091 | pos = NULL; |
| 3092 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3093 | pos = tmp; |
| 3094 | } while (pos); |
| 3095 | |
| 3096 | return last; |
| 3097 | } |
| 3098 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3099 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3100 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3101 | { |
| 3102 | struct cgroup *last; |
| 3103 | |
| 3104 | do { |
| 3105 | last = pos; |
| 3106 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3107 | sibling); |
| 3108 | } while (pos); |
| 3109 | |
| 3110 | return last; |
| 3111 | } |
| 3112 | |
| 3113 | /** |
| 3114 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3115 | * @pos: the current position (%NULL to initiate traversal) |
| 3116 | * @cgroup: cgroup whose descendants to walk |
| 3117 | * |
| 3118 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3119 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3120 | * |
| 3121 | * While this function requires RCU read locking, it doesn't require the |
| 3122 | * whole traversal to be contained in a single RCU critical section. This |
| 3123 | * function will return the correct next descendant as long as both @pos |
| 3124 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3125 | */ |
| 3126 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3127 | struct cgroup *cgroup) |
| 3128 | { |
| 3129 | struct cgroup *next; |
| 3130 | |
| 3131 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3132 | |
| 3133 | /* if first iteration, visit the leftmost descendant */ |
| 3134 | if (!pos) { |
| 3135 | next = cgroup_leftmost_descendant(cgroup); |
| 3136 | return next != cgroup ? next : NULL; |
| 3137 | } |
| 3138 | |
| 3139 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3140 | next = cgroup_next_sibling(pos); |
| 3141 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3142 | return cgroup_leftmost_descendant(next); |
| 3143 | |
| 3144 | /* no sibling left, visit parent */ |
| 3145 | next = pos->parent; |
| 3146 | return next != cgroup ? next : NULL; |
| 3147 | } |
| 3148 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3149 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3150 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3151 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3152 | { |
| 3153 | /* |
| 3154 | * The first time anyone tries to iterate across a cgroup, |
| 3155 | * we need to enable the list linking each css_set to its |
| 3156 | * tasks, and fix up all existing tasks. |
| 3157 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3158 | if (!use_task_css_set_links) |
| 3159 | cgroup_enable_task_cg_lists(); |
| 3160 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3161 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3162 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3163 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3166 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3167 | struct cgroup_iter *it) |
| 3168 | { |
| 3169 | struct task_struct *res; |
| 3170 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3171 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3172 | |
| 3173 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3174 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3175 | return NULL; |
| 3176 | res = list_entry(l, struct task_struct, cg_list); |
| 3177 | /* Advance iterator to find next entry */ |
| 3178 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3179 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3180 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3181 | /* We reached the end of this task list - move on to |
| 3182 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3183 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3184 | } else { |
| 3185 | it->task = l; |
| 3186 | } |
| 3187 | return res; |
| 3188 | } |
| 3189 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3190 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3191 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3192 | { |
| 3193 | read_unlock(&css_set_lock); |
| 3194 | } |
| 3195 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3196 | static inline int started_after_time(struct task_struct *t1, |
| 3197 | struct timespec *time, |
| 3198 | struct task_struct *t2) |
| 3199 | { |
| 3200 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3201 | if (start_diff > 0) { |
| 3202 | return 1; |
| 3203 | } else if (start_diff < 0) { |
| 3204 | return 0; |
| 3205 | } else { |
| 3206 | /* |
| 3207 | * Arbitrarily, if two processes started at the same |
| 3208 | * time, we'll say that the lower pointer value |
| 3209 | * started first. Note that t2 may have exited by now |
| 3210 | * so this may not be a valid pointer any longer, but |
| 3211 | * that's fine - it still serves to distinguish |
| 3212 | * between two tasks started (effectively) simultaneously. |
| 3213 | */ |
| 3214 | return t1 > t2; |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | /* |
| 3219 | * This function is a callback from heap_insert() and is used to order |
| 3220 | * the heap. |
| 3221 | * In this case we order the heap in descending task start time. |
| 3222 | */ |
| 3223 | static inline int started_after(void *p1, void *p2) |
| 3224 | { |
| 3225 | struct task_struct *t1 = p1; |
| 3226 | struct task_struct *t2 = p2; |
| 3227 | return started_after_time(t1, &t2->start_time, t2); |
| 3228 | } |
| 3229 | |
| 3230 | /** |
| 3231 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3232 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3233 | * |
| 3234 | * Arguments include pointers to callback functions test_task() and |
| 3235 | * process_task(). |
| 3236 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3237 | * and if it returns true, call process_task() for it also. |
| 3238 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3239 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3240 | * but does not lock css_set_lock for the call to process_task(). |
| 3241 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3242 | * creation. |
| 3243 | * It is guaranteed that process_task() will act on every task that |
| 3244 | * is a member of the cgroup for the duration of this call. This |
| 3245 | * function may or may not call process_task() for tasks that exit |
| 3246 | * or move to a different cgroup during the call, or are forked or |
| 3247 | * move into the cgroup during the call. |
| 3248 | * |
| 3249 | * Note that test_task() may be called with locks held, and may in some |
| 3250 | * situations be called multiple times for the same task, so it should |
| 3251 | * be cheap. |
| 3252 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3253 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3254 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3255 | * may cause this function to fail). |
| 3256 | */ |
| 3257 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3258 | { |
| 3259 | int retval, i; |
| 3260 | struct cgroup_iter it; |
| 3261 | struct task_struct *p, *dropped; |
| 3262 | /* Never dereference latest_task, since it's not refcounted */ |
| 3263 | struct task_struct *latest_task = NULL; |
| 3264 | struct ptr_heap tmp_heap; |
| 3265 | struct ptr_heap *heap; |
| 3266 | struct timespec latest_time = { 0, 0 }; |
| 3267 | |
| 3268 | if (scan->heap) { |
| 3269 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3270 | heap = scan->heap; |
| 3271 | heap->gt = &started_after; |
| 3272 | } else { |
| 3273 | /* We need to allocate our own heap memory */ |
| 3274 | heap = &tmp_heap; |
| 3275 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3276 | if (retval) |
| 3277 | /* cannot allocate the heap */ |
| 3278 | return retval; |
| 3279 | } |
| 3280 | |
| 3281 | again: |
| 3282 | /* |
| 3283 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3284 | * to determine which are of interest, and using the scanner's |
| 3285 | * "process_task" callback to process any of them that need an update. |
| 3286 | * Since we don't want to hold any locks during the task updates, |
| 3287 | * gather tasks to be processed in a heap structure. |
| 3288 | * The heap is sorted by descending task start time. |
| 3289 | * If the statically-sized heap fills up, we overflow tasks that |
| 3290 | * started later, and in future iterations only consider tasks that |
| 3291 | * started after the latest task in the previous pass. This |
| 3292 | * guarantees forward progress and that we don't miss any tasks. |
| 3293 | */ |
| 3294 | heap->size = 0; |
| 3295 | cgroup_iter_start(scan->cg, &it); |
| 3296 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3297 | /* |
| 3298 | * Only affect tasks that qualify per the caller's callback, |
| 3299 | * if he provided one |
| 3300 | */ |
| 3301 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3302 | continue; |
| 3303 | /* |
| 3304 | * Only process tasks that started after the last task |
| 3305 | * we processed |
| 3306 | */ |
| 3307 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3308 | continue; |
| 3309 | dropped = heap_insert(heap, p); |
| 3310 | if (dropped == NULL) { |
| 3311 | /* |
| 3312 | * The new task was inserted; the heap wasn't |
| 3313 | * previously full |
| 3314 | */ |
| 3315 | get_task_struct(p); |
| 3316 | } else if (dropped != p) { |
| 3317 | /* |
| 3318 | * The new task was inserted, and pushed out a |
| 3319 | * different task |
| 3320 | */ |
| 3321 | get_task_struct(p); |
| 3322 | put_task_struct(dropped); |
| 3323 | } |
| 3324 | /* |
| 3325 | * Else the new task was newer than anything already in |
| 3326 | * the heap and wasn't inserted |
| 3327 | */ |
| 3328 | } |
| 3329 | cgroup_iter_end(scan->cg, &it); |
| 3330 | |
| 3331 | if (heap->size) { |
| 3332 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3333 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3334 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3335 | latest_time = q->start_time; |
| 3336 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3337 | } |
| 3338 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3339 | scan->process_task(q, scan); |
| 3340 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3341 | } |
| 3342 | /* |
| 3343 | * If we had to process any tasks at all, scan again |
| 3344 | * in case some of them were in the middle of forking |
| 3345 | * children that didn't get processed. |
| 3346 | * Not the most efficient way to do it, but it avoids |
| 3347 | * having to take callback_mutex in the fork path |
| 3348 | */ |
| 3349 | goto again; |
| 3350 | } |
| 3351 | if (heap == &tmp_heap) |
| 3352 | heap_free(&tmp_heap); |
| 3353 | return 0; |
| 3354 | } |
| 3355 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3356 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3357 | struct cgroup_scanner *scan) |
| 3358 | { |
| 3359 | struct cgroup *new_cgroup = scan->data; |
| 3360 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3361 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3362 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3363 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | /** |
| 3367 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3368 | * @to: cgroup to which the tasks will be moved |
| 3369 | * @from: cgroup in which the tasks currently reside |
| 3370 | */ |
| 3371 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3372 | { |
| 3373 | struct cgroup_scanner scan; |
| 3374 | |
| 3375 | scan.cg = from; |
| 3376 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3377 | scan.process_task = cgroup_transfer_one_task; |
| 3378 | scan.heap = NULL; |
| 3379 | scan.data = to; |
| 3380 | |
| 3381 | return cgroup_scan_tasks(&scan); |
| 3382 | } |
| 3383 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3384 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3385 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3386 | * |
| 3387 | * Reading this file can return large amounts of data if a cgroup has |
| 3388 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3389 | * but we cannot guarantee that the information we produce is correct |
| 3390 | * unless we produce it entirely atomically. |
| 3391 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3392 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3393 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3394 | /* which pidlist file are we talking about? */ |
| 3395 | enum cgroup_filetype { |
| 3396 | CGROUP_FILE_PROCS, |
| 3397 | CGROUP_FILE_TASKS, |
| 3398 | }; |
| 3399 | |
| 3400 | /* |
| 3401 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3402 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3403 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3404 | * to the cgroup. |
| 3405 | */ |
| 3406 | struct cgroup_pidlist { |
| 3407 | /* |
| 3408 | * used to find which pidlist is wanted. doesn't change as long as |
| 3409 | * this particular list stays in the list. |
| 3410 | */ |
| 3411 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3412 | /* array of xids */ |
| 3413 | pid_t *list; |
| 3414 | /* how many elements the above list has */ |
| 3415 | int length; |
| 3416 | /* how many files are using the current array */ |
| 3417 | int use_count; |
| 3418 | /* each of these stored in a list by its cgroup */ |
| 3419 | struct list_head links; |
| 3420 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3421 | struct cgroup *owner; |
| 3422 | /* protects the other fields */ |
| 3423 | struct rw_semaphore mutex; |
| 3424 | }; |
| 3425 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3426 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3427 | * The following two functions "fix" the issue where there are more pids |
| 3428 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3429 | * TODO: replace with a kernel-wide solution to this problem |
| 3430 | */ |
| 3431 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3432 | static void *pidlist_allocate(int count) |
| 3433 | { |
| 3434 | if (PIDLIST_TOO_LARGE(count)) |
| 3435 | return vmalloc(count * sizeof(pid_t)); |
| 3436 | else |
| 3437 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3438 | } |
| 3439 | static void pidlist_free(void *p) |
| 3440 | { |
| 3441 | if (is_vmalloc_addr(p)) |
| 3442 | vfree(p); |
| 3443 | else |
| 3444 | kfree(p); |
| 3445 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3446 | |
| 3447 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3448 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3449 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3450 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3451 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3452 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3453 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3454 | |
| 3455 | /* |
| 3456 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3457 | * edge cases first; no work needs to be done for either |
| 3458 | */ |
| 3459 | if (length == 0 || length == 1) |
| 3460 | return length; |
| 3461 | /* src and dest walk down the list; dest counts unique elements */ |
| 3462 | for (src = 1; src < length; src++) { |
| 3463 | /* find next unique element */ |
| 3464 | while (list[src] == list[src-1]) { |
| 3465 | src++; |
| 3466 | if (src == length) |
| 3467 | goto after; |
| 3468 | } |
| 3469 | /* dest always points to where the next unique element goes */ |
| 3470 | list[dest] = list[src]; |
| 3471 | dest++; |
| 3472 | } |
| 3473 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3474 | return dest; |
| 3475 | } |
| 3476 | |
| 3477 | static int cmppid(const void *a, const void *b) |
| 3478 | { |
| 3479 | return *(pid_t *)a - *(pid_t *)b; |
| 3480 | } |
| 3481 | |
| 3482 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3483 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3484 | * returns with the lock on that pidlist already held, and takes care |
| 3485 | * of the use count, or returns NULL with no locks held if we're out of |
| 3486 | * memory. |
| 3487 | */ |
| 3488 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3489 | enum cgroup_filetype type) |
| 3490 | { |
| 3491 | struct cgroup_pidlist *l; |
| 3492 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3493 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3494 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3495 | /* |
| 3496 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3497 | * the last ref-holder is trying to remove l from the list at the same |
| 3498 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3499 | * list we find out from under us - compare release_pid_array(). |
| 3500 | */ |
| 3501 | mutex_lock(&cgrp->pidlist_mutex); |
| 3502 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3503 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3504 | /* make sure l doesn't vanish out from under us */ |
| 3505 | down_write(&l->mutex); |
| 3506 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3507 | return l; |
| 3508 | } |
| 3509 | } |
| 3510 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3511 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3512 | if (!l) { |
| 3513 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3514 | return l; |
| 3515 | } |
| 3516 | init_rwsem(&l->mutex); |
| 3517 | down_write(&l->mutex); |
| 3518 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3519 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3520 | l->owner = cgrp; |
| 3521 | list_add(&l->links, &cgrp->pidlists); |
| 3522 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3523 | return l; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3527 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3528 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3529 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3530 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3531 | { |
| 3532 | pid_t *array; |
| 3533 | int length; |
| 3534 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3535 | struct cgroup_iter it; |
| 3536 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3537 | struct cgroup_pidlist *l; |
| 3538 | |
| 3539 | /* |
| 3540 | * If cgroup gets more users after we read count, we won't have |
| 3541 | * enough space - tough. This race is indistinguishable to the |
| 3542 | * caller from the case that the additional cgroup users didn't |
| 3543 | * show up until sometime later on. |
| 3544 | */ |
| 3545 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3546 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3547 | if (!array) |
| 3548 | return -ENOMEM; |
| 3549 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3550 | cgroup_iter_start(cgrp, &it); |
| 3551 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3552 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3553 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3554 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3555 | if (type == CGROUP_FILE_PROCS) |
| 3556 | pid = task_tgid_vnr(tsk); |
| 3557 | else |
| 3558 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3559 | if (pid > 0) /* make sure to only use valid results */ |
| 3560 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3561 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3562 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3563 | length = n; |
| 3564 | /* now sort & (if procs) strip out duplicates */ |
| 3565 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3566 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3567 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3568 | l = cgroup_pidlist_find(cgrp, type); |
| 3569 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3570 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3571 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3572 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3573 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3574 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | l->list = array; |
| 3576 | l->length = length; |
| 3577 | l->use_count++; |
| 3578 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3579 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3580 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3583 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3584 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3585 | * @stats: cgroupstats to fill information into |
| 3586 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3587 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3588 | * |
| 3589 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3590 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3591 | */ |
| 3592 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3593 | { |
| 3594 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3595 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3596 | struct cgroup_iter it; |
| 3597 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3598 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3599 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3600 | * Validate dentry by checking the superblock operations, |
| 3601 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3602 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3603 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3604 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3605 | goto err; |
| 3606 | |
| 3607 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3608 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3609 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3610 | cgroup_iter_start(cgrp, &it); |
| 3611 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3612 | switch (tsk->state) { |
| 3613 | case TASK_RUNNING: |
| 3614 | stats->nr_running++; |
| 3615 | break; |
| 3616 | case TASK_INTERRUPTIBLE: |
| 3617 | stats->nr_sleeping++; |
| 3618 | break; |
| 3619 | case TASK_UNINTERRUPTIBLE: |
| 3620 | stats->nr_uninterruptible++; |
| 3621 | break; |
| 3622 | case TASK_STOPPED: |
| 3623 | stats->nr_stopped++; |
| 3624 | break; |
| 3625 | default: |
| 3626 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3627 | stats->nr_io_wait++; |
| 3628 | break; |
| 3629 | } |
| 3630 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3631 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3632 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3633 | err: |
| 3634 | return ret; |
| 3635 | } |
| 3636 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3637 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3638 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3639 | * 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] | 3640 | * 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] | 3641 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3642 | */ |
| 3643 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3644 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3645 | { |
| 3646 | /* |
| 3647 | * Initially we receive a position value that corresponds to |
| 3648 | * one more than the last pid shown (or 0 on the first call or |
| 3649 | * after a seek to the start). Use a binary-search to find the |
| 3650 | * next pid to display, if any |
| 3651 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3652 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3653 | int index = 0, pid = *pos; |
| 3654 | int *iter; |
| 3655 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3656 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3657 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3658 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3659 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3660 | while (index < end) { |
| 3661 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | index = mid; |
| 3664 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3666 | index = mid + 1; |
| 3667 | else |
| 3668 | end = mid; |
| 3669 | } |
| 3670 | } |
| 3671 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3672 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3673 | return NULL; |
| 3674 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | *pos = *iter; |
| 3677 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3680 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3681 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3682 | struct cgroup_pidlist *l = s->private; |
| 3683 | up_read(&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 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] | 3687 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3688 | struct cgroup_pidlist *l = s->private; |
| 3689 | pid_t *p = v; |
| 3690 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3691 | /* |
| 3692 | * Advance to the next pid in the array. If this goes off the |
| 3693 | * end, we're done |
| 3694 | */ |
| 3695 | p++; |
| 3696 | if (p >= end) { |
| 3697 | return NULL; |
| 3698 | } else { |
| 3699 | *pos = *p; |
| 3700 | return p; |
| 3701 | } |
| 3702 | } |
| 3703 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3704 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3705 | { |
| 3706 | return seq_printf(s, "%d\n", *(int *)v); |
| 3707 | } |
| 3708 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | /* |
| 3710 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3711 | * independent of whether it's tasks or procs |
| 3712 | */ |
| 3713 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3714 | .start = cgroup_pidlist_start, |
| 3715 | .stop = cgroup_pidlist_stop, |
| 3716 | .next = cgroup_pidlist_next, |
| 3717 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3718 | }; |
| 3719 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3720 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3721 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3722 | /* |
| 3723 | * the case where we're the last user of this particular pidlist will |
| 3724 | * have us remove it from the cgroup's list, which entails taking the |
| 3725 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3726 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3727 | */ |
| 3728 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3729 | down_write(&l->mutex); |
| 3730 | BUG_ON(!l->use_count); |
| 3731 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3732 | /* we're the last user if refcount is 0; remove and free */ |
| 3733 | list_del(&l->links); |
| 3734 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3735 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3736 | put_pid_ns(l->key.ns); |
| 3737 | up_write(&l->mutex); |
| 3738 | kfree(l); |
| 3739 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3740 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3741 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3742 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3745 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3746 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3747 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3748 | if (!(file->f_mode & FMODE_READ)) |
| 3749 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3750 | /* |
| 3751 | * the seq_file will only be initialized if the file was opened for |
| 3752 | * reading; hence we check if it's not null only in that case. |
| 3753 | */ |
| 3754 | l = ((struct seq_file *)file->private_data)->private; |
| 3755 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3756 | return seq_release(inode, file); |
| 3757 | } |
| 3758 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3759 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3760 | .read = seq_read, |
| 3761 | .llseek = seq_lseek, |
| 3762 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3763 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | }; |
| 3765 | |
| 3766 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3767 | * The following functions handle opens on a file that displays a pidlist |
| 3768 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3769 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3770 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3771 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3772 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3773 | { |
| 3774 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3775 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3776 | int retval; |
| 3777 | |
| 3778 | /* Nothing to do for write-only files */ |
| 3779 | if (!(file->f_mode & FMODE_READ)) |
| 3780 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3781 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3782 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3783 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3784 | if (retval) |
| 3785 | return retval; |
| 3786 | /* configure file information */ |
| 3787 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3788 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3789 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3790 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3791 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3792 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3793 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3795 | return 0; |
| 3796 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3797 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3798 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3799 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3800 | } |
| 3801 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3802 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3803 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3804 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3805 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3806 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3807 | struct cftype *cft) |
| 3808 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3809 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3812 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3813 | struct cftype *cft, |
| 3814 | u64 val) |
| 3815 | { |
| 3816 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3817 | if (val) |
| 3818 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3819 | else |
| 3820 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3821 | return 0; |
| 3822 | } |
| 3823 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3824 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3825 | * Unregister event and free resources. |
| 3826 | * |
| 3827 | * Gets called from workqueue. |
| 3828 | */ |
| 3829 | static void cgroup_event_remove(struct work_struct *work) |
| 3830 | { |
| 3831 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3832 | remove); |
| 3833 | struct cgroup *cgrp = event->cgrp; |
| 3834 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3835 | remove_wait_queue(event->wqh, &event->wait); |
| 3836 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3837 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3838 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3839 | /* Notify userspace the event is going away. */ |
| 3840 | eventfd_signal(event->eventfd, 1); |
| 3841 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3842 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3843 | kfree(event); |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3844 | dput(cgrp->dentry); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /* |
| 3848 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3849 | * |
| 3850 | * Called with wqh->lock held and interrupts disabled. |
| 3851 | */ |
| 3852 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3853 | int sync, void *key) |
| 3854 | { |
| 3855 | struct cgroup_event *event = container_of(wait, |
| 3856 | struct cgroup_event, wait); |
| 3857 | struct cgroup *cgrp = event->cgrp; |
| 3858 | unsigned long flags = (unsigned long)key; |
| 3859 | |
| 3860 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3861 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3862 | * If the event has been detached at cgroup removal, we |
| 3863 | * can simply return knowing the other side will cleanup |
| 3864 | * for us. |
| 3865 | * |
| 3866 | * We can't race against event freeing since the other |
| 3867 | * side will require wqh->lock via remove_wait_queue(), |
| 3868 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3869 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3870 | spin_lock(&cgrp->event_list_lock); |
| 3871 | if (!list_empty(&event->list)) { |
| 3872 | list_del_init(&event->list); |
| 3873 | /* |
| 3874 | * We are in atomic context, but cgroup_event_remove() |
| 3875 | * may sleep, so we have to call it in workqueue. |
| 3876 | */ |
| 3877 | schedule_work(&event->remove); |
| 3878 | } |
| 3879 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3886 | wait_queue_head_t *wqh, poll_table *pt) |
| 3887 | { |
| 3888 | struct cgroup_event *event = container_of(pt, |
| 3889 | struct cgroup_event, pt); |
| 3890 | |
| 3891 | event->wqh = wqh; |
| 3892 | add_wait_queue(wqh, &event->wait); |
| 3893 | } |
| 3894 | |
| 3895 | /* |
| 3896 | * Parse input and register new cgroup event handler. |
| 3897 | * |
| 3898 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3899 | * Interpretation of args is defined by control file implementation. |
| 3900 | */ |
| 3901 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3902 | const char *buffer) |
| 3903 | { |
| 3904 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3905 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3906 | unsigned int efd, cfd; |
| 3907 | struct file *efile = NULL; |
| 3908 | struct file *cfile = NULL; |
| 3909 | char *endp; |
| 3910 | int ret; |
| 3911 | |
| 3912 | efd = simple_strtoul(buffer, &endp, 10); |
| 3913 | if (*endp != ' ') |
| 3914 | return -EINVAL; |
| 3915 | buffer = endp + 1; |
| 3916 | |
| 3917 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3918 | if ((*endp != ' ') && (*endp != '\0')) |
| 3919 | return -EINVAL; |
| 3920 | buffer = endp + 1; |
| 3921 | |
| 3922 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3923 | if (!event) |
| 3924 | return -ENOMEM; |
| 3925 | event->cgrp = cgrp; |
| 3926 | INIT_LIST_HEAD(&event->list); |
| 3927 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3928 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3929 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3930 | |
| 3931 | efile = eventfd_fget(efd); |
| 3932 | if (IS_ERR(efile)) { |
| 3933 | ret = PTR_ERR(efile); |
| 3934 | goto fail; |
| 3935 | } |
| 3936 | |
| 3937 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3938 | if (IS_ERR(event->eventfd)) { |
| 3939 | ret = PTR_ERR(event->eventfd); |
| 3940 | goto fail; |
| 3941 | } |
| 3942 | |
| 3943 | cfile = fget(cfd); |
| 3944 | if (!cfile) { |
| 3945 | ret = -EBADF; |
| 3946 | goto fail; |
| 3947 | } |
| 3948 | |
| 3949 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3950 | /* 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] | 3951 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3952 | if (ret < 0) |
| 3953 | goto fail; |
| 3954 | |
| 3955 | event->cft = __file_cft(cfile); |
| 3956 | if (IS_ERR(event->cft)) { |
| 3957 | ret = PTR_ERR(event->cft); |
| 3958 | goto fail; |
| 3959 | } |
| 3960 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3961 | /* |
| 3962 | * The file to be monitored must be in the same cgroup as |
| 3963 | * cgroup.event_control is. |
| 3964 | */ |
| 3965 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3966 | if (cgrp_cfile != cgrp) { |
| 3967 | ret = -EINVAL; |
| 3968 | goto fail; |
| 3969 | } |
| 3970 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3971 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3972 | ret = -EINVAL; |
| 3973 | goto fail; |
| 3974 | } |
| 3975 | |
| 3976 | ret = event->cft->register_event(cgrp, event->cft, |
| 3977 | event->eventfd, buffer); |
| 3978 | if (ret) |
| 3979 | goto fail; |
| 3980 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 3981 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3982 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3983 | /* |
| 3984 | * Events should be removed after rmdir of cgroup directory, but before |
| 3985 | * destroying subsystem state objects. Let's take reference to cgroup |
| 3986 | * directory dentry to do that. |
| 3987 | */ |
| 3988 | dget(cgrp->dentry); |
| 3989 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3990 | spin_lock(&cgrp->event_list_lock); |
| 3991 | list_add(&event->list, &cgrp->event_list); |
| 3992 | spin_unlock(&cgrp->event_list_lock); |
| 3993 | |
| 3994 | fput(cfile); |
| 3995 | fput(efile); |
| 3996 | |
| 3997 | return 0; |
| 3998 | |
| 3999 | fail: |
| 4000 | if (cfile) |
| 4001 | fput(cfile); |
| 4002 | |
| 4003 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4004 | eventfd_ctx_put(event->eventfd); |
| 4005 | |
| 4006 | if (!IS_ERR_OR_NULL(efile)) |
| 4007 | fput(efile); |
| 4008 | |
| 4009 | kfree(event); |
| 4010 | |
| 4011 | return ret; |
| 4012 | } |
| 4013 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4014 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4015 | struct cftype *cft) |
| 4016 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4017 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4021 | struct cftype *cft, |
| 4022 | u64 val) |
| 4023 | { |
| 4024 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4025 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4026 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4027 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4028 | return 0; |
| 4029 | } |
| 4030 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4031 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4032 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4033 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4034 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4035 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4036 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4037 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4038 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4039 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4040 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4041 | .write_string = cgroup_write_event_control, |
| 4042 | .mode = S_IWUGO, |
| 4043 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4044 | { |
| 4045 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4046 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4047 | .read_u64 = cgroup_clone_children_read, |
| 4048 | .write_u64 = cgroup_clone_children_write, |
| 4049 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4050 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4051 | .name = "cgroup.sane_behavior", |
| 4052 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4053 | .read_seq_string = cgroup_sane_behavior_show, |
| 4054 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4055 | |
| 4056 | /* |
| 4057 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4058 | * don't exist if sane_behavior. If you're depending on these, be |
| 4059 | * prepared to be burned. |
| 4060 | */ |
| 4061 | { |
| 4062 | .name = "tasks", |
| 4063 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4064 | .open = cgroup_tasks_open, |
| 4065 | .write_u64 = cgroup_tasks_write, |
| 4066 | .release = cgroup_pidlist_release, |
| 4067 | .mode = S_IRUGO | S_IWUSR, |
| 4068 | }, |
| 4069 | { |
| 4070 | .name = "notify_on_release", |
| 4071 | .flags = CFTYPE_INSANE, |
| 4072 | .read_u64 = cgroup_read_notify_on_release, |
| 4073 | .write_u64 = cgroup_write_notify_on_release, |
| 4074 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4075 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4076 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4077 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4078 | .read_seq_string = cgroup_release_agent_show, |
| 4079 | .write_string = cgroup_release_agent_write, |
| 4080 | .max_write_len = PATH_MAX, |
| 4081 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4082 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4083 | }; |
| 4084 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4085 | /** |
| 4086 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4087 | * @cgrp: target cgroup |
| 4088 | * @base_files: true if the base files should be added |
| 4089 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4090 | */ |
| 4091 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4092 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4093 | { |
| 4094 | int err; |
| 4095 | struct cgroup_subsys *ss; |
| 4096 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4097 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4098 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4099 | if (err < 0) |
| 4100 | return err; |
| 4101 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4102 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4103 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4104 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4105 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4106 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4107 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4108 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4109 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4110 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4111 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4113 | /* This cgroup is ready now */ |
| 4114 | for_each_subsys(cgrp->root, ss) { |
| 4115 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4116 | /* |
| 4117 | * Update id->css pointer and make this css visible from |
| 4118 | * CSS ID functions. This pointer will be dereferened |
| 4119 | * from RCU-read-side without locks. |
| 4120 | */ |
| 4121 | if (css->id) |
| 4122 | rcu_assign_pointer(css->id->css, css); |
| 4123 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4124 | |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4128 | static void css_dput_fn(struct work_struct *work) |
| 4129 | { |
| 4130 | struct cgroup_subsys_state *css = |
| 4131 | container_of(work, struct cgroup_subsys_state, dput_work); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4132 | struct dentry *dentry = css->cgroup->dentry; |
| 4133 | struct super_block *sb = dentry->d_sb; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4134 | |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4135 | atomic_inc(&sb->s_active); |
| 4136 | dput(dentry); |
| 4137 | deactivate_super(sb); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4140 | static void css_release(struct percpu_ref *ref) |
| 4141 | { |
| 4142 | struct cgroup_subsys_state *css = |
| 4143 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4144 | |
| 4145 | schedule_work(&css->dput_work); |
| 4146 | } |
| 4147 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4148 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4149 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4150 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4151 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4152 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4153 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4154 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4155 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4156 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4157 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4158 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4159 | |
| 4160 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4161 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4162 | * css_put(). dput() requires process context, which css_put() may |
| 4163 | * be called without. @css->dput_work will be used to invoke |
| 4164 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4165 | */ |
| 4166 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4167 | } |
| 4168 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4169 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4170 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4171 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4172 | int ret = 0; |
| 4173 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4174 | lockdep_assert_held(&cgroup_mutex); |
| 4175 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4176 | if (ss->css_online) |
| 4177 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4178 | if (!ret) |
| 4179 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4180 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4184 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4185 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4186 | { |
| 4187 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4188 | |
| 4189 | lockdep_assert_held(&cgroup_mutex); |
| 4190 | |
| 4191 | if (!(css->flags & CSS_ONLINE)) |
| 4192 | return; |
| 4193 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4194 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4195 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4196 | |
| 4197 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4198 | } |
| 4199 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4200 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4201 | * cgroup_create - create a cgroup |
| 4202 | * @parent: cgroup that will be parent of the new cgroup |
| 4203 | * @dentry: dentry of the new cgroup |
| 4204 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4205 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4206 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4207 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4208 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4209 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4210 | { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4211 | static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4212 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4213 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4214 | struct cgroupfs_root *root = parent->root; |
| 4215 | int err = 0; |
| 4216 | struct cgroup_subsys *ss; |
| 4217 | struct super_block *sb = root->sb; |
| 4218 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4219 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4220 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4221 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4222 | return -ENOMEM; |
| 4223 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4224 | name = cgroup_alloc_name(dentry); |
| 4225 | if (!name) |
| 4226 | goto err_free_cgrp; |
| 4227 | rcu_assign_pointer(cgrp->name, name); |
| 4228 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4229 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4230 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4231 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4232 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4233 | /* |
| 4234 | * Only live parents can have children. Note that the liveliness |
| 4235 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4236 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4237 | * anyway so that locking is contained inside cgroup proper and we |
| 4238 | * don't get nasty surprises if we ever grow another caller. |
| 4239 | */ |
| 4240 | if (!cgroup_lock_live_group(parent)) { |
| 4241 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4242 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4243 | } |
| 4244 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4245 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4246 | * get deleted on unmount if there are child cgroups. This |
| 4247 | * can be done outside cgroup_mutex, since the sb can't |
| 4248 | * disappear while someone has an open control file on the |
| 4249 | * fs */ |
| 4250 | atomic_inc(&sb->s_active); |
| 4251 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4252 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4253 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4254 | dentry->d_fsdata = cgrp; |
| 4255 | cgrp->dentry = dentry; |
| 4256 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4257 | cgrp->parent = parent; |
| 4258 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4259 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4260 | if (notify_on_release(parent)) |
| 4261 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4262 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4263 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4264 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4265 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4266 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4267 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4268 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4269 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4270 | if (IS_ERR(css)) { |
| 4271 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4272 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4273 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4274 | |
| 4275 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4276 | if (err) |
| 4277 | goto err_free_all; |
| 4278 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4279 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4280 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4281 | if (ss->use_id) { |
| 4282 | err = alloc_css_id(ss, parent, cgrp); |
| 4283 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4284 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4285 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4286 | } |
| 4287 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4288 | /* |
| 4289 | * Create directory. cgroup_create_file() returns with the new |
| 4290 | * directory locked on success so that it can be populated without |
| 4291 | * dropping cgroup_mutex. |
| 4292 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4293 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4294 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4295 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4296 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4297 | |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4298 | /* |
| 4299 | * Assign a monotonically increasing serial number. With the list |
| 4300 | * appending below, it guarantees that sibling cgroups are always |
| 4301 | * sorted in the ascending serial number order on the parent's |
| 4302 | * ->children. |
| 4303 | */ |
| 4304 | cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor); |
| 4305 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4306 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4307 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4308 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4309 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4310 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4311 | /* each css holds a ref to the cgroup's dentry */ |
| 4312 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4313 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4314 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4315 | /* hold a ref to the parent's dentry */ |
| 4316 | dget(parent->dentry); |
| 4317 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4318 | /* creation succeeded, notify subsystems */ |
| 4319 | for_each_subsys(root, ss) { |
| 4320 | err = online_css(ss, cgrp); |
| 4321 | if (err) |
| 4322 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4323 | |
| 4324 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4325 | parent->parent) { |
| 4326 | 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", |
| 4327 | current->comm, current->pid, ss->name); |
| 4328 | if (!strcmp(ss->name, "memory")) |
| 4329 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4330 | ss->warned_broken_hierarchy = true; |
| 4331 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4332 | } |
| 4333 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4334 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4335 | if (err) |
| 4336 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4337 | |
| 4338 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4339 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4340 | |
| 4341 | return 0; |
| 4342 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4343 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4344 | for_each_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4345 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4346 | |
| 4347 | if (css) { |
| 4348 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4349 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4350 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4351 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4352 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4353 | /* Release the reference count that we took on the superblock */ |
| 4354 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4355 | err_free_id: |
| 4356 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4357 | err_free_name: |
| 4358 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4359 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4360 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4361 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4362 | |
| 4363 | err_destroy: |
| 4364 | cgroup_destroy_locked(cgrp); |
| 4365 | mutex_unlock(&cgroup_mutex); |
| 4366 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4367 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4370 | 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] | 4371 | { |
| 4372 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4373 | |
| 4374 | /* the vfs holds inode->i_mutex already */ |
| 4375 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4376 | } |
| 4377 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4378 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4379 | { |
| 4380 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4381 | return; |
| 4382 | |
| 4383 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4384 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4385 | schedule_work(&cgrp->destroy_work); |
| 4386 | } |
| 4387 | |
| 4388 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4389 | { |
| 4390 | struct cgroup_subsys_state *css = |
| 4391 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4392 | |
| 4393 | cgroup_css_killed(css->cgroup); |
| 4394 | } |
| 4395 | |
| 4396 | /** |
| 4397 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4398 | * @cgrp: cgroup to be destroyed |
| 4399 | * |
| 4400 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4401 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4402 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4403 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4404 | * the following two steps. |
| 4405 | * |
| 4406 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4407 | * userland visible parts and start killing the percpu refcnts of |
| 4408 | * css's. Set up so that the next stage will be kicked off once all |
| 4409 | * the percpu refcnts are confirmed to be killed. |
| 4410 | * |
| 4411 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4412 | * rest of destruction. Once all cgroup references are gone, the |
| 4413 | * cgroup is RCU-freed. |
| 4414 | * |
| 4415 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4416 | * the userland is concerned and a new cgroup with the same name may be |
| 4417 | * created. As cgroup doesn't care about the names internally, this |
| 4418 | * doesn't cause any problem. |
| 4419 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4420 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4421 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4422 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4423 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4424 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4425 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4426 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4427 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4428 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4429 | lockdep_assert_held(&cgroup_mutex); |
| 4430 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4431 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4432 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4433 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4434 | */ |
| 4435 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4436 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4437 | read_unlock(&css_set_lock); |
| 4438 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4439 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4440 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4441 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4442 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4443 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4444 | * css reference will be given out via css_tryget(). We can't |
| 4445 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4446 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4447 | * as killed on all CPUs on return. |
| 4448 | * |
| 4449 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4450 | * css is confirmed to be seen as killed on all CPUs. The |
| 4451 | * notification callback keeps track of the number of css's to be |
| 4452 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4453 | * destruction once the percpu refs of all css's are confirmed to |
| 4454 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4455 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4456 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4457 | for_each_subsys(cgrp->root, ss) { |
| 4458 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4459 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4460 | /* |
| 4461 | * Killing would put the base ref, but we need to keep it |
| 4462 | * alive until after ->css_offline. |
| 4463 | */ |
| 4464 | percpu_ref_get(&css->refcnt); |
| 4465 | |
| 4466 | atomic_inc(&cgrp->css_kill_cnt); |
| 4467 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4468 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4469 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4470 | |
| 4471 | /* |
| 4472 | * Mark @cgrp dead. This prevents further task migration and child |
| 4473 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4474 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4475 | * resume iteration after dropping RCU read lock. See |
| 4476 | * cgroup_next_sibling() for details. |
| 4477 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4478 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4479 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4480 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4481 | raw_spin_lock(&release_list_lock); |
| 4482 | if (!list_empty(&cgrp->release_list)) |
| 4483 | list_del_init(&cgrp->release_list); |
| 4484 | raw_spin_unlock(&release_list_lock); |
| 4485 | |
| 4486 | /* |
| 4487 | * Remove @cgrp directory. The removal puts the base ref but we |
| 4488 | * aren't quite done with @cgrp yet, so hold onto it. |
| 4489 | */ |
| 4490 | dget(d); |
| 4491 | cgroup_d_remove_dir(d); |
| 4492 | |
| 4493 | /* |
| 4494 | * Unregister events and notify userspace. |
| 4495 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4496 | * directory to avoid race between userspace and kernelspace. |
| 4497 | */ |
| 4498 | spin_lock(&cgrp->event_list_lock); |
| 4499 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4500 | list_del_init(&event->list); |
| 4501 | schedule_work(&event->remove); |
| 4502 | } |
| 4503 | spin_unlock(&cgrp->event_list_lock); |
| 4504 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4505 | return 0; |
| 4506 | }; |
| 4507 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4508 | /** |
| 4509 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4510 | * @work: cgroup->destroy_free_work |
| 4511 | * |
| 4512 | * This function is invoked from a work item for a cgroup which is being |
| 4513 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4514 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4515 | * is the second step of destruction described in the comment above |
| 4516 | * cgroup_destroy_locked(). |
| 4517 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4518 | static void cgroup_offline_fn(struct work_struct *work) |
| 4519 | { |
| 4520 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4521 | struct cgroup *parent = cgrp->parent; |
| 4522 | struct dentry *d = cgrp->dentry; |
| 4523 | struct cgroup_subsys *ss; |
| 4524 | |
| 4525 | mutex_lock(&cgroup_mutex); |
| 4526 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4527 | /* |
| 4528 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4529 | * initate destruction. |
| 4530 | */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4531 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4532 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4533 | |
| 4534 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4535 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4536 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4537 | * proceeds regardless of css refs. On the last put of each css, |
| 4538 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4539 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4540 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4541 | for_each_subsys(cgrp->root, ss) |
| 4542 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4543 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4544 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4545 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4546 | list_del_init(&cgrp->allcg_node); |
| 4547 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4548 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4549 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4550 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4551 | check_for_release(parent); |
| 4552 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4553 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4556 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4557 | { |
| 4558 | int ret; |
| 4559 | |
| 4560 | mutex_lock(&cgroup_mutex); |
| 4561 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4562 | mutex_unlock(&cgroup_mutex); |
| 4563 | |
| 4564 | return ret; |
| 4565 | } |
| 4566 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4567 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4568 | { |
| 4569 | INIT_LIST_HEAD(&ss->cftsets); |
| 4570 | |
| 4571 | /* |
| 4572 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4573 | * deregistration. |
| 4574 | */ |
| 4575 | if (ss->base_cftypes) { |
| 4576 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4577 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4578 | } |
| 4579 | } |
| 4580 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4581 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4582 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4583 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4584 | |
| 4585 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4586 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4587 | mutex_lock(&cgroup_mutex); |
| 4588 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4589 | /* init base cftset */ |
| 4590 | cgroup_init_cftsets(ss); |
| 4591 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4592 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4593 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4594 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4595 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4596 | /* We don't handle early failures gracefully */ |
| 4597 | BUG_ON(IS_ERR(css)); |
| 4598 | init_cgroup_css(css, ss, dummytop); |
| 4599 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4600 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4601 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4602 | * newly registered, all tasks and hence the |
| 4603 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4604 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4605 | |
| 4606 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4607 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4608 | /* At system boot, before all subsystems have been |
| 4609 | * registered, no tasks have been forked, so we don't |
| 4610 | * need to invoke fork callbacks here. */ |
| 4611 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4612 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4613 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4614 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4615 | mutex_unlock(&cgroup_mutex); |
| 4616 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4617 | /* this function shouldn't be used with modular subsystems, since they |
| 4618 | * need to register a subsys_id, among other things */ |
| 4619 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4623 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4624 | * @ss: the subsystem to load |
| 4625 | * |
| 4626 | * 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] | 4627 | * 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] | 4628 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4629 | * simpler cgroup_init_subsys. |
| 4630 | */ |
| 4631 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4632 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4633 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4634 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4635 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4636 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4637 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4638 | |
| 4639 | /* check name and function validity */ |
| 4640 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4641 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4642 | return -EINVAL; |
| 4643 | |
| 4644 | /* |
| 4645 | * we don't support callbacks in modular subsystems. this check is |
| 4646 | * before the ss->module check for consistency; a subsystem that could |
| 4647 | * be a module should still have no callbacks even if the user isn't |
| 4648 | * compiling it as one. |
| 4649 | */ |
| 4650 | if (ss->fork || ss->exit) |
| 4651 | return -EINVAL; |
| 4652 | |
| 4653 | /* |
| 4654 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4655 | * since cgroup_init_subsys will have already taken care of it. |
| 4656 | */ |
| 4657 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4658 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4659 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4660 | return 0; |
| 4661 | } |
| 4662 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4663 | /* init base cftset */ |
| 4664 | cgroup_init_cftsets(ss); |
| 4665 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4666 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4667 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4668 | |
| 4669 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4670 | * no ss->css_alloc seems to need anything important in the ss |
| 4671 | * struct, so this can happen first (i.e. before the rootnode |
| 4672 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4673 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4674 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4675 | if (IS_ERR(css)) { |
| 4676 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4677 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4678 | mutex_unlock(&cgroup_mutex); |
| 4679 | return PTR_ERR(css); |
| 4680 | } |
| 4681 | |
| 4682 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4683 | ss->root = &rootnode; |
| 4684 | |
| 4685 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4686 | init_cgroup_css(css, ss, dummytop); |
| 4687 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4688 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4689 | ret = cgroup_init_idr(ss, css); |
| 4690 | if (ret) |
| 4691 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | /* |
| 4695 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4696 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4697 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4698 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4699 | * table state, so each changed css_set will need its hash recomputed. |
| 4700 | * this is all done under the css_set_lock. |
| 4701 | */ |
| 4702 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4703 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4704 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4705 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4706 | continue; |
| 4707 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4708 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4709 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4710 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4711 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4712 | key = css_set_hash(cset->subsys); |
| 4713 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4714 | } |
| 4715 | write_unlock(&css_set_lock); |
| 4716 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4717 | ret = online_css(ss, dummytop); |
| 4718 | if (ret) |
| 4719 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4720 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4721 | /* success! */ |
| 4722 | mutex_unlock(&cgroup_mutex); |
| 4723 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4724 | |
| 4725 | err_unload: |
| 4726 | mutex_unlock(&cgroup_mutex); |
| 4727 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4728 | cgroup_unload_subsys(ss); |
| 4729 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4730 | } |
| 4731 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4732 | |
| 4733 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4734 | * cgroup_unload_subsys: unload a modular subsystem |
| 4735 | * @ss: the subsystem to unload |
| 4736 | * |
| 4737 | * This function should be called in a modular subsystem's exitcall. When this |
| 4738 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4739 | * the subsystem will not be attached to any hierarchy. |
| 4740 | */ |
| 4741 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4742 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4743 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4744 | |
| 4745 | BUG_ON(ss->module == NULL); |
| 4746 | |
| 4747 | /* |
| 4748 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4749 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4750 | * doesn't start being used while we're killing it off. |
| 4751 | */ |
| 4752 | BUG_ON(ss->root != &rootnode); |
| 4753 | |
| 4754 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4755 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4756 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4757 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4758 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4759 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4760 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4761 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4762 | subsys[ss->subsys_id] = NULL; |
| 4763 | |
| 4764 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4765 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4766 | |
| 4767 | /* |
| 4768 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4769 | * in loading, we need to pay our respects to the hashtable gods. |
| 4770 | */ |
| 4771 | write_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4772 | list_for_each_entry(link, &dummytop->cset_links, cset_link) { |
| 4773 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4774 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4775 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4776 | hash_del(&cset->hlist); |
| 4777 | cset->subsys[ss->subsys_id] = NULL; |
| 4778 | key = css_set_hash(cset->subsys); |
| 4779 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4780 | } |
| 4781 | write_unlock(&css_set_lock); |
| 4782 | |
| 4783 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4784 | * remove subsystem's css from the dummytop and free it - need to |
| 4785 | * free before marking as null because ss->css_free needs the |
| 4786 | * cgrp->subsys pointer to find their state. note that this also |
| 4787 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4788 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4789 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4790 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4791 | |
| 4792 | mutex_unlock(&cgroup_mutex); |
| 4793 | } |
| 4794 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4795 | |
| 4796 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4797 | * cgroup_init_early - cgroup initialization at system boot |
| 4798 | * |
| 4799 | * Initialize cgroups at system boot, and initialize any |
| 4800 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4801 | */ |
| 4802 | int __init cgroup_init_early(void) |
| 4803 | { |
| 4804 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4805 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4806 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4807 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4808 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4809 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4810 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4811 | root_count = 1; |
| 4812 | init_task.cgroups = &init_css_set; |
| 4813 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4814 | init_cgrp_cset_link.cset = &init_css_set; |
| 4815 | init_cgrp_cset_link.cgrp = dummytop; |
| 4816 | list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links); |
| 4817 | list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4818 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4819 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4820 | struct cgroup_subsys *ss = subsys[i]; |
| 4821 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4822 | /* at bootup time, we don't worry about modular subsystems */ |
| 4823 | if (!ss || ss->module) |
| 4824 | continue; |
| 4825 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4826 | BUG_ON(!ss->name); |
| 4827 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4828 | BUG_ON(!ss->css_alloc); |
| 4829 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4830 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4831 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4832 | ss->name, ss->subsys_id); |
| 4833 | BUG(); |
| 4834 | } |
| 4835 | |
| 4836 | if (ss->early_init) |
| 4837 | cgroup_init_subsys(ss); |
| 4838 | } |
| 4839 | return 0; |
| 4840 | } |
| 4841 | |
| 4842 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4843 | * cgroup_init - cgroup initialization |
| 4844 | * |
| 4845 | * Register cgroup filesystem and /proc file, and initialize |
| 4846 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4847 | */ |
| 4848 | int __init cgroup_init(void) |
| 4849 | { |
| 4850 | int err; |
| 4851 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4852 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4853 | |
| 4854 | err = bdi_init(&cgroup_backing_dev_info); |
| 4855 | if (err) |
| 4856 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4857 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4858 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4859 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4860 | |
| 4861 | /* at bootup time, we don't worry about modular subsystems */ |
| 4862 | if (!ss || ss->module) |
| 4863 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4864 | if (!ss->early_init) |
| 4865 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4866 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4867 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4868 | } |
| 4869 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4870 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4871 | key = css_set_hash(init_css_set.subsys); |
| 4872 | hash_add(css_set_table, &init_css_set.hlist, key); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4873 | |
| 4874 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4875 | mutex_lock(&cgroup_mutex); |
| 4876 | mutex_lock(&cgroup_root_mutex); |
| 4877 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4878 | BUG_ON(cgroup_init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4879 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4880 | mutex_unlock(&cgroup_root_mutex); |
| 4881 | mutex_unlock(&cgroup_mutex); |
| 4882 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4883 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4884 | if (!cgroup_kobj) { |
| 4885 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4886 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | err = register_filesystem(&cgroup_fs_type); |
| 4890 | if (err < 0) { |
| 4891 | kobject_put(cgroup_kobj); |
| 4892 | goto out; |
| 4893 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4894 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4895 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4896 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4897 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4898 | if (err) |
| 4899 | bdi_destroy(&cgroup_backing_dev_info); |
| 4900 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4901 | return err; |
| 4902 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4903 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4904 | /* |
| 4905 | * proc_cgroup_show() |
| 4906 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4907 | * - Used for /proc/<pid>/cgroup. |
| 4908 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4909 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4910 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4911 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4912 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4913 | * cgroup to top_cgroup. |
| 4914 | */ |
| 4915 | |
| 4916 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4917 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4918 | { |
| 4919 | struct pid *pid; |
| 4920 | struct task_struct *tsk; |
| 4921 | char *buf; |
| 4922 | int retval; |
| 4923 | struct cgroupfs_root *root; |
| 4924 | |
| 4925 | retval = -ENOMEM; |
| 4926 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4927 | if (!buf) |
| 4928 | goto out; |
| 4929 | |
| 4930 | retval = -ESRCH; |
| 4931 | pid = m->private; |
| 4932 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4933 | if (!tsk) |
| 4934 | goto out_free; |
| 4935 | |
| 4936 | retval = 0; |
| 4937 | |
| 4938 | mutex_lock(&cgroup_mutex); |
| 4939 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4940 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4941 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4942 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4943 | int count = 0; |
| 4944 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4945 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4946 | for_each_subsys(root, ss) |
| 4947 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4948 | if (strlen(root->name)) |
| 4949 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4950 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4951 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4952 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4953 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4954 | if (retval < 0) |
| 4955 | goto out_unlock; |
| 4956 | seq_puts(m, buf); |
| 4957 | seq_putc(m, '\n'); |
| 4958 | } |
| 4959 | |
| 4960 | out_unlock: |
| 4961 | mutex_unlock(&cgroup_mutex); |
| 4962 | put_task_struct(tsk); |
| 4963 | out_free: |
| 4964 | kfree(buf); |
| 4965 | out: |
| 4966 | return retval; |
| 4967 | } |
| 4968 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4969 | /* Display information about each subsystem and each hierarchy */ |
| 4970 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4971 | { |
| 4972 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4973 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4974 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4975 | /* |
| 4976 | * ideally we don't want subsystems moving around while we do this. |
| 4977 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4978 | * subsys/hierarchy state. |
| 4979 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4980 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4981 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4982 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4983 | if (ss == NULL) |
| 4984 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4985 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4986 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4987 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4988 | } |
| 4989 | mutex_unlock(&cgroup_mutex); |
| 4990 | return 0; |
| 4991 | } |
| 4992 | |
| 4993 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4994 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4995 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4996 | } |
| 4997 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4998 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4999 | .open = cgroupstats_open, |
| 5000 | .read = seq_read, |
| 5001 | .llseek = seq_lseek, |
| 5002 | .release = single_release, |
| 5003 | }; |
| 5004 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5005 | /** |
| 5006 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5007 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5008 | * |
| 5009 | * Description: A task inherits its parent's cgroup at fork(). |
| 5010 | * |
| 5011 | * A pointer to the shared css_set was automatically copied in |
| 5012 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5013 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5014 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5015 | * have already changed current->cgroups, allowing the previously |
| 5016 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5017 | * |
| 5018 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5019 | * task, and the passed argument 'child' points to the child task. |
| 5020 | */ |
| 5021 | void cgroup_fork(struct task_struct *child) |
| 5022 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5023 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5024 | child->cgroups = current->cgroups; |
| 5025 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5026 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5027 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5031 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5032 | * @child: the task in question |
| 5033 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5034 | * Adds the task to the list running through its css_set if necessary and |
| 5035 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5036 | * visible on the task list in case we race with the first call to |
| 5037 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5038 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5039 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5040 | void cgroup_post_fork(struct task_struct *child) |
| 5041 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5042 | int i; |
| 5043 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5044 | /* |
| 5045 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5046 | * under the tasklist_lock and we read it here after we added the child |
| 5047 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5048 | * yet in the tasklist when we walked through it from |
| 5049 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5050 | * should be visible now due to the paired locking and barriers implied |
| 5051 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5052 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5053 | * lock on fork. |
| 5054 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5055 | if (use_task_css_set_links) { |
| 5056 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5057 | task_lock(child); |
| 5058 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5059 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5060 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5061 | write_unlock(&css_set_lock); |
| 5062 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5063 | |
| 5064 | /* |
| 5065 | * Call ss->fork(). This must happen after @child is linked on |
| 5066 | * css_set; otherwise, @child might change state between ->fork() |
| 5067 | * and addition to css_set. |
| 5068 | */ |
| 5069 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5070 | /* |
| 5071 | * fork/exit callbacks are supported only for builtin |
| 5072 | * subsystems, and the builtin section of the subsys |
| 5073 | * array is immutable, so we don't need to lock the |
| 5074 | * subsys array here. On the other hand, modular section |
| 5075 | * of the array can be freed at module unload, so we |
| 5076 | * can't touch that. |
| 5077 | */ |
| 5078 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5079 | struct cgroup_subsys *ss = subsys[i]; |
| 5080 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5081 | if (ss->fork) |
| 5082 | ss->fork(child); |
| 5083 | } |
| 5084 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5085 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5086 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5087 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5088 | * cgroup_exit - detach cgroup from exiting task |
| 5089 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5090 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5091 | * |
| 5092 | * Description: Detach cgroup from @tsk and release it. |
| 5093 | * |
| 5094 | * Note that cgroups marked notify_on_release force every task in |
| 5095 | * them to take the global cgroup_mutex mutex when exiting. |
| 5096 | * This could impact scaling on very large systems. Be reluctant to |
| 5097 | * use notify_on_release cgroups where very high task exit scaling |
| 5098 | * is required on large systems. |
| 5099 | * |
| 5100 | * the_top_cgroup_hack: |
| 5101 | * |
| 5102 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5103 | * |
| 5104 | * We call cgroup_exit() while the task is still competent to |
| 5105 | * handle notify_on_release(), then leave the task attached to the |
| 5106 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5107 | * |
| 5108 | * To do this properly, we would increment the reference count on |
| 5109 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5110 | * code we would add a second cgroup function call, to drop that |
| 5111 | * reference. This would just create an unnecessary hot spot on |
| 5112 | * the top_cgroup reference count, to no avail. |
| 5113 | * |
| 5114 | * Normally, holding a reference to a cgroup without bumping its |
| 5115 | * count is unsafe. The cgroup could go away, or someone could |
| 5116 | * attach us to a different cgroup, decrementing the count on |
| 5117 | * the first cgroup that we never incremented. But in this case, |
| 5118 | * 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] | 5119 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5120 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5121 | */ |
| 5122 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5123 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5124 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5125 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5126 | |
| 5127 | /* |
| 5128 | * Unlink from the css_set task list if necessary. |
| 5129 | * Optimistically check cg_list before taking |
| 5130 | * css_set_lock |
| 5131 | */ |
| 5132 | if (!list_empty(&tsk->cg_list)) { |
| 5133 | write_lock(&css_set_lock); |
| 5134 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5135 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5136 | write_unlock(&css_set_lock); |
| 5137 | } |
| 5138 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5139 | /* Reassign the task to the init_css_set. */ |
| 5140 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5141 | cset = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5142 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5143 | |
| 5144 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5145 | /* |
| 5146 | * fork/exit callbacks are supported only for builtin |
| 5147 | * subsystems, see cgroup_post_fork() for details. |
| 5148 | */ |
| 5149 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5150 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5151 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5152 | if (ss->exit) { |
| 5153 | struct cgroup *old_cgrp = |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5154 | rcu_dereference_raw(cset->subsys[i])->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5155 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5156 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5157 | } |
| 5158 | } |
| 5159 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5160 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5161 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5162 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5163 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5164 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5165 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5166 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5167 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5168 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5169 | /* |
| 5170 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5171 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5172 | * it now |
| 5173 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5174 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5175 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5176 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5177 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5178 | list_empty(&cgrp->release_list)) { |
| 5179 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5180 | need_schedule_work = 1; |
| 5181 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5182 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5183 | if (need_schedule_work) |
| 5184 | schedule_work(&release_agent_work); |
| 5185 | } |
| 5186 | } |
| 5187 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5188 | /* |
| 5189 | * Notify userspace when a cgroup is released, by running the |
| 5190 | * configured release agent with the name of the cgroup (path |
| 5191 | * relative to the root of cgroup file system) as the argument. |
| 5192 | * |
| 5193 | * Most likely, this user command will try to rmdir this cgroup. |
| 5194 | * |
| 5195 | * This races with the possibility that some other task will be |
| 5196 | * attached to this cgroup before it is removed, or that some other |
| 5197 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5198 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5199 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5200 | * to continue to serve a useful existence. Next time it's released, |
| 5201 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5202 | * |
| 5203 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5204 | * means only wait until the task is successfully execve()'d. The |
| 5205 | * separate release agent task is forked by call_usermodehelper(), |
| 5206 | * then control in this thread returns here, without waiting for the |
| 5207 | * release agent task. We don't bother to wait because the caller of |
| 5208 | * this routine has no use for the exit status of the release agent |
| 5209 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5210 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5211 | static void cgroup_release_agent(struct work_struct *work) |
| 5212 | { |
| 5213 | BUG_ON(work != &release_agent_work); |
| 5214 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5215 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5216 | while (!list_empty(&release_list)) { |
| 5217 | char *argv[3], *envp[3]; |
| 5218 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5219 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5220 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5221 | struct cgroup, |
| 5222 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5223 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5224 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5225 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5226 | if (!pathbuf) |
| 5227 | goto continue_free; |
| 5228 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5229 | goto continue_free; |
| 5230 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5231 | if (!agentbuf) |
| 5232 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5233 | |
| 5234 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5235 | argv[i++] = agentbuf; |
| 5236 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5237 | argv[i] = NULL; |
| 5238 | |
| 5239 | i = 0; |
| 5240 | /* minimal command environment */ |
| 5241 | envp[i++] = "HOME=/"; |
| 5242 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5243 | envp[i] = NULL; |
| 5244 | |
| 5245 | /* Drop the lock while we invoke the usermode helper, |
| 5246 | * since the exec could involve hitting disk and hence |
| 5247 | * be a slow process */ |
| 5248 | mutex_unlock(&cgroup_mutex); |
| 5249 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5250 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5251 | continue_free: |
| 5252 | kfree(pathbuf); |
| 5253 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5254 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5255 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5256 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5257 | mutex_unlock(&cgroup_mutex); |
| 5258 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5259 | |
| 5260 | static int __init cgroup_disable(char *str) |
| 5261 | { |
| 5262 | int i; |
| 5263 | char *token; |
| 5264 | |
| 5265 | while ((token = strsep(&str, ",")) != NULL) { |
| 5266 | if (!*token) |
| 5267 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5268 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5269 | struct cgroup_subsys *ss = subsys[i]; |
| 5270 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5271 | /* |
| 5272 | * cgroup_disable, being at boot time, can't |
| 5273 | * know about module subsystems, so we don't |
| 5274 | * worry about them. |
| 5275 | */ |
| 5276 | if (!ss || ss->module) |
| 5277 | continue; |
| 5278 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5279 | if (!strcmp(token, ss->name)) { |
| 5280 | ss->disabled = 1; |
| 5281 | printk(KERN_INFO "Disabling %s control group" |
| 5282 | " subsystem\n", ss->name); |
| 5283 | break; |
| 5284 | } |
| 5285 | } |
| 5286 | } |
| 5287 | return 1; |
| 5288 | } |
| 5289 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5290 | |
| 5291 | /* |
| 5292 | * Functons for CSS ID. |
| 5293 | */ |
| 5294 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5295 | /* to get ID other than 0, this should be called when !cgroup_is_dead() */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5296 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5297 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5298 | struct css_id *cssid; |
| 5299 | |
| 5300 | /* |
| 5301 | * This css_id() can return correct value when somone has refcnt |
| 5302 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5303 | * it's unchanged until freed. |
| 5304 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5305 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5306 | |
| 5307 | if (cssid) |
| 5308 | return cssid->id; |
| 5309 | return 0; |
| 5310 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5311 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5312 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5313 | /** |
| 5314 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5315 | * @child: the css to be tested. |
| 5316 | * @root: the css supporsed to be an ancestor of the child. |
| 5317 | * |
| 5318 | * 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] | 5319 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5320 | * But, considering usual usage, the csses should be valid objects after test. |
| 5321 | * Assuming that the caller will do some action to the child if this returns |
| 5322 | * returns true, the caller must take "child";s reference count. |
| 5323 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5324 | */ |
| 5325 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5326 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5327 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5328 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5329 | struct css_id *child_id; |
| 5330 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5331 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5332 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5333 | if (!child_id) |
| 5334 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5335 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5336 | if (!root_id) |
| 5337 | return false; |
| 5338 | if (child_id->depth < root_id->depth) |
| 5339 | return false; |
| 5340 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5341 | return false; |
| 5342 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5343 | } |
| 5344 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5345 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5346 | { |
| 5347 | struct css_id *id = css->id; |
| 5348 | /* When this is called before css_id initialization, id can be NULL */ |
| 5349 | if (!id) |
| 5350 | return; |
| 5351 | |
| 5352 | BUG_ON(!ss->use_id); |
| 5353 | |
| 5354 | rcu_assign_pointer(id->css, NULL); |
| 5355 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5356 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5357 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5358 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5359 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5360 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5361 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5362 | |
| 5363 | /* |
| 5364 | * This is called by init or create(). Then, calls to this function are |
| 5365 | * always serialized (By cgroup_mutex() at create()). |
| 5366 | */ |
| 5367 | |
| 5368 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5369 | { |
| 5370 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5371 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5372 | |
| 5373 | BUG_ON(!ss->use_id); |
| 5374 | |
| 5375 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5376 | newid = kzalloc(size, GFP_KERNEL); |
| 5377 | if (!newid) |
| 5378 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5379 | |
| 5380 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5381 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5382 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5383 | 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] | 5384 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5385 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5386 | |
| 5387 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5388 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5389 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5390 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5391 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5392 | newid->depth = depth; |
| 5393 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5394 | err_out: |
| 5395 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5396 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5397 | |
| 5398 | } |
| 5399 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5400 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5401 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5402 | { |
| 5403 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5404 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5405 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5406 | idr_init(&ss->idr); |
| 5407 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5408 | newid = get_new_cssid(ss, 0); |
| 5409 | if (IS_ERR(newid)) |
| 5410 | return PTR_ERR(newid); |
| 5411 | |
| 5412 | newid->stack[0] = newid->id; |
| 5413 | newid->css = rootcss; |
| 5414 | rootcss->id = newid; |
| 5415 | return 0; |
| 5416 | } |
| 5417 | |
| 5418 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5419 | struct cgroup *child) |
| 5420 | { |
| 5421 | int subsys_id, i, depth = 0; |
| 5422 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5423 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5424 | |
| 5425 | subsys_id = ss->subsys_id; |
| 5426 | parent_css = parent->subsys[subsys_id]; |
| 5427 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5428 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5429 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5430 | |
| 5431 | child_id = get_new_cssid(ss, depth); |
| 5432 | if (IS_ERR(child_id)) |
| 5433 | return PTR_ERR(child_id); |
| 5434 | |
| 5435 | for (i = 0; i < depth; i++) |
| 5436 | child_id->stack[i] = parent_id->stack[i]; |
| 5437 | child_id->stack[depth] = child_id->id; |
| 5438 | /* |
| 5439 | * child_id->css pointer will be set after this cgroup is available |
| 5440 | * see cgroup_populate_dir() |
| 5441 | */ |
| 5442 | rcu_assign_pointer(child_css->id, child_id); |
| 5443 | |
| 5444 | return 0; |
| 5445 | } |
| 5446 | |
| 5447 | /** |
| 5448 | * css_lookup - lookup css by id |
| 5449 | * @ss: cgroup subsys to be looked into. |
| 5450 | * @id: the id |
| 5451 | * |
| 5452 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5453 | * NULL if not. Should be called under rcu_read_lock() |
| 5454 | */ |
| 5455 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5456 | { |
| 5457 | struct css_id *cssid = NULL; |
| 5458 | |
| 5459 | BUG_ON(!ss->use_id); |
| 5460 | cssid = idr_find(&ss->idr, id); |
| 5461 | |
| 5462 | if (unlikely(!cssid)) |
| 5463 | return NULL; |
| 5464 | |
| 5465 | return rcu_dereference(cssid->css); |
| 5466 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5467 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5468 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5469 | /* |
| 5470 | * get corresponding css from file open on cgroupfs directory |
| 5471 | */ |
| 5472 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5473 | { |
| 5474 | struct cgroup *cgrp; |
| 5475 | struct inode *inode; |
| 5476 | struct cgroup_subsys_state *css; |
| 5477 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5478 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5479 | /* check in cgroup filesystem dir */ |
| 5480 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5481 | return ERR_PTR(-EBADF); |
| 5482 | |
| 5483 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5484 | return ERR_PTR(-EINVAL); |
| 5485 | |
| 5486 | /* get cgroup */ |
| 5487 | cgrp = __d_cgrp(f->f_dentry); |
| 5488 | css = cgrp->subsys[id]; |
| 5489 | return css ? css : ERR_PTR(-ENOENT); |
| 5490 | } |
| 5491 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5492 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5493 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5494 | { |
| 5495 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5496 | |
| 5497 | if (!css) |
| 5498 | return ERR_PTR(-ENOMEM); |
| 5499 | |
| 5500 | return css; |
| 5501 | } |
| 5502 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5503 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5504 | { |
| 5505 | kfree(cont->subsys[debug_subsys_id]); |
| 5506 | } |
| 5507 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5508 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5509 | { |
| 5510 | return cgroup_task_count(cont); |
| 5511 | } |
| 5512 | |
| 5513 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5514 | { |
| 5515 | return (u64)(unsigned long)current->cgroups; |
| 5516 | } |
| 5517 | |
| 5518 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5519 | struct cftype *cft) |
| 5520 | { |
| 5521 | u64 count; |
| 5522 | |
| 5523 | rcu_read_lock(); |
| 5524 | count = atomic_read(¤t->cgroups->refcount); |
| 5525 | rcu_read_unlock(); |
| 5526 | return count; |
| 5527 | } |
| 5528 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5529 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5530 | struct cftype *cft, |
| 5531 | struct seq_file *seq) |
| 5532 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5533 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5534 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5535 | |
| 5536 | read_lock(&css_set_lock); |
| 5537 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5538 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5539 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5540 | struct cgroup *c = link->cgrp; |
| 5541 | const char *name; |
| 5542 | |
| 5543 | if (c->dentry) |
| 5544 | name = c->dentry->d_name.name; |
| 5545 | else |
| 5546 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5547 | seq_printf(seq, "Root %d group %s\n", |
| 5548 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5549 | } |
| 5550 | rcu_read_unlock(); |
| 5551 | read_unlock(&css_set_lock); |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5556 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5557 | struct cftype *cft, |
| 5558 | struct seq_file *seq) |
| 5559 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5560 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5561 | |
| 5562 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5563 | list_for_each_entry(link, &cont->cset_links, cset_link) { |
| 5564 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5565 | struct task_struct *task; |
| 5566 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5567 | seq_printf(seq, "css_set %p\n", cset); |
| 5568 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5569 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5570 | seq_puts(seq, " ...\n"); |
| 5571 | break; |
| 5572 | } else { |
| 5573 | seq_printf(seq, " task %d\n", |
| 5574 | task_pid_vnr(task)); |
| 5575 | } |
| 5576 | } |
| 5577 | } |
| 5578 | read_unlock(&css_set_lock); |
| 5579 | return 0; |
| 5580 | } |
| 5581 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5582 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5583 | { |
| 5584 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5585 | } |
| 5586 | |
| 5587 | static struct cftype debug_files[] = { |
| 5588 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5589 | .name = "taskcount", |
| 5590 | .read_u64 = debug_taskcount_read, |
| 5591 | }, |
| 5592 | |
| 5593 | { |
| 5594 | .name = "current_css_set", |
| 5595 | .read_u64 = current_css_set_read, |
| 5596 | }, |
| 5597 | |
| 5598 | { |
| 5599 | .name = "current_css_set_refcount", |
| 5600 | .read_u64 = current_css_set_refcount_read, |
| 5601 | }, |
| 5602 | |
| 5603 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5604 | .name = "current_css_set_cg_links", |
| 5605 | .read_seq_string = current_css_set_cg_links_read, |
| 5606 | }, |
| 5607 | |
| 5608 | { |
| 5609 | .name = "cgroup_css_links", |
| 5610 | .read_seq_string = cgroup_css_links_read, |
| 5611 | }, |
| 5612 | |
| 5613 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5614 | .name = "releasable", |
| 5615 | .read_u64 = releasable_read, |
| 5616 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5617 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5618 | { } /* terminate */ |
| 5619 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5620 | |
| 5621 | struct cgroup_subsys debug_subsys = { |
| 5622 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5623 | .css_alloc = debug_css_alloc, |
| 5624 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5625 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5626 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5627 | }; |
| 5628 | #endif /* CONFIG_CGROUP_DEBUG */ |