Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/magic.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 54 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 55 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 56 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 57 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 58 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 59 | #include <linux/eventfd.h> |
| 60 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 61 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 62 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 66 | /* css deactivation bias, makes css->refcnt negative to deny new trygets */ |
| 67 | #define CSS_DEACT_BIAS INT_MIN |
| 68 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 69 | /* |
| 70 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 71 | * hierarchy must be performed while holding it. |
| 72 | * |
| 73 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 74 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 75 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 76 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 77 | * break the following locking order cycle. |
| 78 | * |
| 79 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 80 | * B. namespace_sem -> cgroup_mutex |
| 81 | * |
| 82 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 83 | * breaks it. |
| 84 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_PROVE_RCU |
| 86 | DEFINE_MUTEX(cgroup_mutex); |
| 87 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 88 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 90 | #endif |
| 91 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 92 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 93 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | /* |
| 95 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 96 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 97 | * registered after that. The mutable section of this array is protected by |
| 98 | * cgroup_mutex. |
| 99 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 100 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 101 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 102 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | #include <linux/cgroup_subsys.h> |
| 104 | }; |
| 105 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 106 | /* |
| 107 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 108 | * subsystems that are otherwise unattached - it never has more than a |
| 109 | * single cgroup, and all tasks are part of that cgroup. |
| 110 | */ |
| 111 | static struct cgroupfs_root rootnode; |
| 112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 113 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 114 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 115 | */ |
| 116 | struct cfent { |
| 117 | struct list_head node; |
| 118 | struct dentry *dentry; |
| 119 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 120 | |
| 121 | /* file xattrs */ |
| 122 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 126 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 127 | * cgroup_subsys->use_id != 0. |
| 128 | */ |
| 129 | #define CSS_ID_MAX (65535) |
| 130 | struct css_id { |
| 131 | /* |
| 132 | * The css to which this ID points. This pointer is set to valid value |
| 133 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 134 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 135 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 136 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 137 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 138 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 139 | /* |
| 140 | * ID of this css. |
| 141 | */ |
| 142 | unsigned short id; |
| 143 | /* |
| 144 | * Depth in hierarchy which this ID belongs to. |
| 145 | */ |
| 146 | unsigned short depth; |
| 147 | /* |
| 148 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 149 | */ |
| 150 | struct rcu_head rcu_head; |
| 151 | /* |
| 152 | * Hierarchy of CSS ID belongs to. |
| 153 | */ |
| 154 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 155 | }; |
| 156 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 157 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 158 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 159 | */ |
| 160 | struct cgroup_event { |
| 161 | /* |
| 162 | * Cgroup which the event belongs to. |
| 163 | */ |
| 164 | struct cgroup *cgrp; |
| 165 | /* |
| 166 | * Control file which the event associated. |
| 167 | */ |
| 168 | struct cftype *cft; |
| 169 | /* |
| 170 | * eventfd to signal userspace about the event. |
| 171 | */ |
| 172 | struct eventfd_ctx *eventfd; |
| 173 | /* |
| 174 | * Each of these stored in a list by the cgroup. |
| 175 | */ |
| 176 | struct list_head list; |
| 177 | /* |
| 178 | * All fields below needed to unregister event when |
| 179 | * userspace closes eventfd. |
| 180 | */ |
| 181 | poll_table pt; |
| 182 | wait_queue_head_t *wqh; |
| 183 | wait_queue_t wait; |
| 184 | struct work_struct remove; |
| 185 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | /* The list of hierarchy roots */ |
| 188 | |
| 189 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 190 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 191 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 194 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 195 | * writes, either for reads. |
| 196 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 197 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 198 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 199 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 200 | #define dummytop (&rootnode.top_cgroup) |
| 201 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 202 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 203 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 204 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 205 | * check for fork/exit handlers to call. This avoids us having to do |
| 206 | * extra work in the fork/exit path if none of the subsystems need to |
| 207 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 208 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 209 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 210 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 211 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 212 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 213 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 214 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 215 | static int css_unbias_refcnt(int refcnt) |
| 216 | { |
| 217 | return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS; |
| 218 | } |
| 219 | |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 220 | /* the current nr of refs, always >= 0 whether @css is deactivated or not */ |
| 221 | static int css_refcnt(struct cgroup_subsys_state *css) |
| 222 | { |
| 223 | int v = atomic_read(&css->refcnt); |
| 224 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 225 | return css_unbias_refcnt(v); |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 228 | /* convenient tests for these bits */ |
Tejun Heo | bdc7119 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 229 | static inline bool cgroup_is_removed(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 230 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 231 | return test_bit(CGRP_REMOVED, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 234 | /** |
| 235 | * cgroup_is_descendant - test ancestry |
| 236 | * @cgrp: the cgroup to be tested |
| 237 | * @ancestor: possible ancestor of @cgrp |
| 238 | * |
| 239 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 240 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 241 | * and @ancestor are accessible. |
| 242 | */ |
| 243 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 244 | { |
| 245 | while (cgrp) { |
| 246 | if (cgrp == ancestor) |
| 247 | return true; |
| 248 | cgrp = cgrp->parent; |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 253 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 254 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 255 | { |
| 256 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 257 | (1 << CGRP_RELEASABLE) | |
| 258 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 259 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 262 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 263 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 264 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 267 | /* |
| 268 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 269 | * an active hierarchy |
| 270 | */ |
| 271 | #define for_each_subsys(_root, _ss) \ |
| 272 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 273 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 274 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 275 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 276 | list_for_each_entry(_root, &roots, root_list) |
| 277 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 278 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 279 | { |
| 280 | return dentry->d_fsdata; |
| 281 | } |
| 282 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 283 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 284 | { |
| 285 | return dentry->d_fsdata; |
| 286 | } |
| 287 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 288 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 289 | { |
| 290 | return __d_cfe(dentry)->type; |
| 291 | } |
| 292 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 293 | /** |
| 294 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 295 | * @cgrp: the cgroup to be checked for liveness |
| 296 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 297 | * On success, returns true; the mutex should be later unlocked. On |
| 298 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 299 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 300 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 301 | { |
| 302 | mutex_lock(&cgroup_mutex); |
| 303 | if (cgroup_is_removed(cgrp)) { |
| 304 | mutex_unlock(&cgroup_mutex); |
| 305 | return false; |
| 306 | } |
| 307 | return true; |
| 308 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 309 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 310 | /* the list of cgroups eligible for automatic release. Protected by |
| 311 | * release_list_lock */ |
| 312 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 313 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 314 | static void cgroup_release_agent(struct work_struct *work); |
| 315 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 316 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 317 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 318 | /* Link structure for associating css_set objects with cgroups */ |
| 319 | struct cg_cgroup_link { |
| 320 | /* |
| 321 | * List running through cg_cgroup_links associated with a |
| 322 | * cgroup, anchored on cgroup->css_sets |
| 323 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 324 | struct list_head cgrp_link_list; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 325 | struct cgroup *cgrp; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 326 | /* |
| 327 | * List running through cg_cgroup_links pointing at a |
| 328 | * single css_set object, anchored on css_set->cg_links |
| 329 | */ |
| 330 | struct list_head cg_link_list; |
| 331 | struct css_set *cg; |
| 332 | }; |
| 333 | |
| 334 | /* The default css_set - used by init and its children prior to any |
| 335 | * hierarchies being mounted. It contains a pointer to the root state |
| 336 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 337 | * reference-counted, to improve performance when child cgroups |
| 338 | * haven't been created. |
| 339 | */ |
| 340 | |
| 341 | static struct css_set init_css_set; |
| 342 | static struct cg_cgroup_link init_css_set_link; |
| 343 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 344 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 345 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 346 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 347 | /* css_set_lock protects the list of css_set objects, and the |
| 348 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 349 | * due to cgroup_iter_start() */ |
| 350 | static DEFINE_RWLOCK(css_set_lock); |
| 351 | static int css_set_count; |
| 352 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 353 | /* |
| 354 | * hash table for cgroup groups. This improves the performance to find |
| 355 | * an existing css_set. This hash doesn't (currently) take into |
| 356 | * account cgroups in empty hierarchies. |
| 357 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 358 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 359 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 360 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 361 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 362 | { |
| 363 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 364 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 365 | |
| 366 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 367 | key += (unsigned long)css[i]; |
| 368 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 369 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 370 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 373 | /* We don't maintain the lists running through each css_set to its |
| 374 | * task until after the first call to cgroup_iter_start(). This |
| 375 | * reduces the fork()/exit() overhead for people who have cgroups |
| 376 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 377 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 378 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 379 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 380 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 381 | struct cg_cgroup_link *link; |
| 382 | struct cg_cgroup_link *saved_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 383 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 384 | /* |
| 385 | * Ensure that the refcount doesn't hit zero while any readers |
| 386 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 387 | * rwlock |
| 388 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 389 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 390 | return; |
| 391 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 392 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 393 | write_unlock(&css_set_lock); |
| 394 | return; |
| 395 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 396 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 397 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 398 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 399 | css_set_count--; |
| 400 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 401 | list_for_each_entry_safe(link, saved_link, &cset->cg_links, |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 402 | cg_link_list) { |
| 403 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 404 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 405 | list_del(&link->cg_link_list); |
| 406 | list_del(&link->cgrp_link_list); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * We may not be holding cgroup_mutex, and if cgrp->count is |
| 410 | * dropped to 0 the cgroup can be destroyed at any time, hence |
| 411 | * rcu_read_lock is used to keep it alive. |
| 412 | */ |
| 413 | rcu_read_lock(); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 414 | if (atomic_dec_and_test(&cgrp->count) && |
| 415 | notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 416 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 417 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 418 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 419 | } |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 420 | rcu_read_unlock(); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 421 | |
| 422 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 423 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 424 | |
| 425 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 426 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
| 430 | * refcounted get/put for css_set objects |
| 431 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 432 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 433 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 434 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 437 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 438 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 439 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 442 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 443 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 444 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 447 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 448 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 449 | * @cset: candidate css_set being tested |
| 450 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 451 | * @new_cgrp: cgroup that's being entered by the task |
| 452 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 453 | * |
| 454 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 455 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 456 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 457 | static bool compare_css_sets(struct css_set *cset, |
| 458 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 459 | struct cgroup *new_cgrp, |
| 460 | struct cgroup_subsys_state *template[]) |
| 461 | { |
| 462 | struct list_head *l1, *l2; |
| 463 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 464 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 465 | /* Not all subsystems matched */ |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Compare cgroup pointers in order to distinguish between |
| 471 | * different cgroups in heirarchies with no subsystems. We |
| 472 | * could get by with just this check alone (and skip the |
| 473 | * memcmp above) but on most setups the memcmp check will |
| 474 | * avoid the need for this more expensive check on almost all |
| 475 | * candidates. |
| 476 | */ |
| 477 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 478 | l1 = &cset->cg_links; |
| 479 | l2 = &old_cset->cg_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | while (1) { |
| 481 | struct cg_cgroup_link *cgl1, *cgl2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 482 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 483 | |
| 484 | l1 = l1->next; |
| 485 | l2 = l2->next; |
| 486 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 487 | if (l1 == &cset->cg_links) { |
| 488 | BUG_ON(l2 != &old_cset->cg_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | break; |
| 490 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 491 | BUG_ON(l2 == &old_cset->cg_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 492 | } |
| 493 | /* Locate the cgroups associated with these links. */ |
| 494 | cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list); |
| 495 | cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 496 | cgrp1 = cgl1->cgrp; |
| 497 | cgrp2 = cgl2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 499 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * If this hierarchy is the hierarchy of the cgroup |
| 503 | * that's changing, then we need to check that this |
| 504 | * css_set points to the new cgroup; if it's any other |
| 505 | * hierarchy, then this css_set should point to the |
| 506 | * same cgroup as the old css_set. |
| 507 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 508 | if (cgrp1->root == new_cgrp->root) { |
| 509 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 512 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | } |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 520 | * find_existing_css_set() is a helper for |
| 521 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 522 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 523 | * |
| 524 | * oldcg: the cgroup group that we're using before the cgroup |
| 525 | * transition |
| 526 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 527 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 528 | * |
| 529 | * template: location in which to build the desired set of subsystem |
| 530 | * state objects for the new cgroup group |
| 531 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 532 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 533 | struct cgroup *cgrp, |
| 534 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | { |
| 536 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 537 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 538 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 539 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 540 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 541 | /* |
| 542 | * Build the set of subsystem state objects that we want to see in the |
| 543 | * new css_set. while subsystems can change globally, the entries here |
| 544 | * won't change, so no need for locking. |
| 545 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 547 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 548 | /* Subsystem is in this hierarchy. So we want |
| 549 | * the subsystem state from the new |
| 550 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 551 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 552 | } else { |
| 553 | /* Subsystem is not in this hierarchy, so we |
| 554 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 555 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 559 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 560 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 561 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 562 | continue; |
| 563 | |
| 564 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 565 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 566 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 567 | |
| 568 | /* No existing cgroup group matched */ |
| 569 | return NULL; |
| 570 | } |
| 571 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 572 | static void free_cg_links(struct list_head *tmp) |
| 573 | { |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 574 | struct cg_cgroup_link *link; |
| 575 | struct cg_cgroup_link *saved_link; |
| 576 | |
| 577 | list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 578 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 579 | kfree(link); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 584 | * allocate_cg_links() allocates "count" cg_cgroup_link structures |
| 585 | * and chains them on tmp through their cgrp_link_list fields. Returns 0 on |
| 586 | * success or a negative error |
| 587 | */ |
| 588 | static int allocate_cg_links(int count, struct list_head *tmp) |
| 589 | { |
| 590 | struct cg_cgroup_link *link; |
| 591 | int i; |
| 592 | INIT_LIST_HEAD(tmp); |
| 593 | for (i = 0; i < count; i++) { |
| 594 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
| 595 | if (!link) { |
| 596 | free_cg_links(tmp); |
| 597 | return -ENOMEM; |
| 598 | } |
| 599 | list_add(&link->cgrp_link_list, tmp); |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 604 | /** |
| 605 | * link_css_set - a helper function to link a css_set to a cgroup |
| 606 | * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 607 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 608 | * @cgrp: the destination cgroup |
| 609 | */ |
| 610 | static void link_css_set(struct list_head *tmp_cg_links, |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 611 | struct css_set *cset, struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 612 | { |
| 613 | struct cg_cgroup_link *link; |
| 614 | |
| 615 | BUG_ON(list_empty(tmp_cg_links)); |
| 616 | link = list_first_entry(tmp_cg_links, struct cg_cgroup_link, |
| 617 | cgrp_link_list); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 618 | link->cg = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 619 | link->cgrp = cgrp; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 620 | atomic_inc(&cgrp->count); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | list_move(&link->cgrp_link_list, &cgrp->css_sets); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 622 | /* |
| 623 | * Always add links to the tail of the list so that the list |
| 624 | * is sorted by order of hierarchy creation |
| 625 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 626 | list_add_tail(&link->cg_link_list, &cset->cg_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 629 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 630 | * find_css_set() takes an existing cgroup group and a |
| 631 | * cgroup object, and returns a css_set object that's |
| 632 | * equivalent to the old group, but with the given cgroup |
| 633 | * substituted into the appropriate hierarchy. Must be called with |
| 634 | * cgroup_mutex held |
| 635 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 636 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 637 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 638 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 639 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 640 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 641 | |
| 642 | struct list_head tmp_cg_links; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 643 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 644 | struct cg_cgroup_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 645 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 646 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | /* First see if we already have a cgroup group that matches |
| 648 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 649 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 650 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 651 | if (cset) |
| 652 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 653 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 655 | if (cset) |
| 656 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 657 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 658 | cset = kmalloc(sizeof(*cset), GFP_KERNEL); |
| 659 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 660 | return NULL; |
| 661 | |
| 662 | /* Allocate all the cg_cgroup_link objects that we'll need */ |
| 663 | if (allocate_cg_links(root_count, &tmp_cg_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 664 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 665 | return NULL; |
| 666 | } |
| 667 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 668 | atomic_set(&cset->refcount, 1); |
| 669 | INIT_LIST_HEAD(&cset->cg_links); |
| 670 | INIT_LIST_HEAD(&cset->tasks); |
| 671 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | |
| 673 | /* Copy the set of subsystem state objects generated in |
| 674 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 675 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | |
| 677 | write_lock(&css_set_lock); |
| 678 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 679 | list_for_each_entry(link, &old_cset->cg_links, cg_link_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 680 | struct cgroup *c = link->cgrp; |
| 681 | if (c->root == cgrp->root) |
| 682 | c = cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 683 | link_css_set(&tmp_cg_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 684 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | |
| 686 | BUG_ON(!list_empty(&tmp_cg_links)); |
| 687 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 689 | |
| 690 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 691 | key = css_set_hash(cset->subsys); |
| 692 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 693 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 694 | write_unlock(&css_set_lock); |
| 695 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 696 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 699 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 700 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 701 | * called with cgroup_mutex held. |
| 702 | */ |
| 703 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 704 | struct cgroupfs_root *root) |
| 705 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 706 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 707 | struct cgroup *res = NULL; |
| 708 | |
| 709 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 710 | read_lock(&css_set_lock); |
| 711 | /* |
| 712 | * No need to lock the task - since we hold cgroup_mutex the |
| 713 | * task can't change groups, so the only thing that can happen |
| 714 | * is that it exits and its css is set back to init_css_set. |
| 715 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 716 | cset = task->cgroups; |
| 717 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 718 | res = &root->top_cgroup; |
| 719 | } else { |
| 720 | struct cg_cgroup_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 721 | list_for_each_entry(link, &cset->cg_links, cg_link_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 722 | struct cgroup *c = link->cgrp; |
| 723 | if (c->root == root) { |
| 724 | res = c; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | read_unlock(&css_set_lock); |
| 730 | BUG_ON(!res); |
| 731 | return res; |
| 732 | } |
| 733 | |
| 734 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 735 | * There is one global cgroup mutex. We also require taking |
| 736 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 737 | * See "The task_lock() exception", at the end of this comment. |
| 738 | * |
| 739 | * A task must hold cgroup_mutex to modify cgroups. |
| 740 | * |
| 741 | * Any task can increment and decrement the count field without lock. |
| 742 | * So in general, code holding cgroup_mutex can't rely on the count |
| 743 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 744 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 745 | * means that no tasks are currently attached, therefore there is no |
| 746 | * way a task attached to that cgroup can fork (the other way to |
| 747 | * increment the count). So code holding cgroup_mutex can safely |
| 748 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 749 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 750 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 751 | * needs that mutex. |
| 752 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 753 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 754 | * (usually) take cgroup_mutex. These are the two most performance |
| 755 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 756 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 757 | * 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] | 758 | * to the release agent with the name of the cgroup (path relative to |
| 759 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 760 | * |
| 761 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 762 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 763 | * tasks in the system use _some_ cgroup, and since there is always at |
| 764 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 765 | * always has either children cgroups and/or using tasks. So we don't |
| 766 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 767 | * |
| 768 | * The task_lock() exception |
| 769 | * |
| 770 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 771 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 772 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 773 | * several performance critical places that need to reference |
| 774 | * task->cgroup without the expense of grabbing a system global |
| 775 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 776 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 777 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 778 | * the task_struct routinely used for such matters. |
| 779 | * |
| 780 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 781 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 782 | */ |
| 783 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 784 | /* |
| 785 | * A couple of forward declarations required, due to cyclic reference loop: |
| 786 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 787 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 788 | * -> cgroup_mkdir. |
| 789 | */ |
| 790 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 791 | 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] | 792 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 793 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 794 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 795 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 796 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 797 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 798 | |
| 799 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 800 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 801 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 802 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 804 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 805 | struct cgroup *parent, struct cgroup *child); |
| 806 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 807 | 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] | 808 | { |
| 809 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 810 | |
| 811 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 812 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 813 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 814 | inode->i_uid = current_fsuid(); |
| 815 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 816 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 817 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 818 | } |
| 819 | return inode; |
| 820 | } |
| 821 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 822 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 823 | { |
| 824 | struct cgroup_name *name; |
| 825 | |
| 826 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 827 | if (!name) |
| 828 | return NULL; |
| 829 | strcpy(name->name, dentry->d_name.name); |
| 830 | return name; |
| 831 | } |
| 832 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 833 | static void cgroup_free_fn(struct work_struct *work) |
| 834 | { |
| 835 | struct cgroup *cgrp = container_of(work, struct cgroup, free_work); |
| 836 | struct cgroup_subsys *ss; |
| 837 | |
| 838 | mutex_lock(&cgroup_mutex); |
| 839 | /* |
| 840 | * Release the subsystem state objects. |
| 841 | */ |
| 842 | for_each_subsys(cgrp->root, ss) |
| 843 | ss->css_free(cgrp); |
| 844 | |
| 845 | cgrp->root->number_of_cgroups--; |
| 846 | mutex_unlock(&cgroup_mutex); |
| 847 | |
| 848 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 849 | * We get a ref to the parent's dentry, and put the ref when |
| 850 | * this cgroup is being freed, so it's guaranteed that the |
| 851 | * parent won't be destroyed before its children. |
| 852 | */ |
| 853 | dput(cgrp->parent->dentry); |
| 854 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 855 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 856 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 857 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 858 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 859 | * created the cgroup. This will free cgrp->root, if we are |
| 860 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 861 | */ |
| 862 | deactivate_super(cgrp->root->sb); |
| 863 | |
| 864 | /* |
| 865 | * if we're getting rid of the cgroup, refcount should ensure |
| 866 | * that there are no pidlists left. |
| 867 | */ |
| 868 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 869 | |
| 870 | simple_xattrs_free(&cgrp->xattrs); |
| 871 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 872 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 873 | kfree(cgrp); |
| 874 | } |
| 875 | |
| 876 | static void cgroup_free_rcu(struct rcu_head *head) |
| 877 | { |
| 878 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 879 | |
| 880 | schedule_work(&cgrp->free_work); |
| 881 | } |
| 882 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 883 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 884 | { |
| 885 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 886 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 887 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 888 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 889 | BUG_ON(!(cgroup_is_removed(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 890 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 891 | } else { |
| 892 | struct cfent *cfe = __d_cfe(dentry); |
| 893 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 894 | |
| 895 | WARN_ONCE(!list_empty(&cfe->node) && |
| 896 | cgrp != &cgrp->root->top_cgroup, |
| 897 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 898 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 899 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 900 | } |
| 901 | iput(inode); |
| 902 | } |
| 903 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 904 | static int cgroup_delete(const struct dentry *d) |
| 905 | { |
| 906 | return 1; |
| 907 | } |
| 908 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 909 | static void remove_dir(struct dentry *d) |
| 910 | { |
| 911 | struct dentry *parent = dget(d->d_parent); |
| 912 | |
| 913 | d_delete(d); |
| 914 | simple_rmdir(parent->d_inode, d); |
| 915 | dput(parent); |
| 916 | } |
| 917 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 918 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 919 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 920 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 921 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 922 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 923 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 924 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 925 | /* |
| 926 | * If we're doing cleanup due to failure of cgroup_create(), |
| 927 | * the corresponding @cfe may not exist. |
| 928 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 929 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 930 | struct dentry *d = cfe->dentry; |
| 931 | |
| 932 | if (cft && cfe->type != cft) |
| 933 | continue; |
| 934 | |
| 935 | dget(d); |
| 936 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 937 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 938 | list_del_init(&cfe->node); |
| 939 | dput(d); |
| 940 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 941 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 942 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 945 | /** |
| 946 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 947 | * @dir: directory containing the files |
| 948 | * @base_files: true if the base files should be removed |
| 949 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 950 | */ |
| 951 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 952 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 953 | { |
| 954 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 955 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 956 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 957 | for_each_subsys(cgrp->root, ss) { |
| 958 | struct cftype_set *set; |
| 959 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 960 | continue; |
| 961 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 962 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 963 | } |
| 964 | if (base_files) { |
| 965 | while (!list_empty(&cgrp->files)) |
| 966 | cgroup_rm_file(cgrp, NULL); |
| 967 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* |
| 971 | * NOTE : the dentry must have been dget()'ed |
| 972 | */ |
| 973 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 974 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 975 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 976 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 977 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 978 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 979 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 980 | parent = dentry->d_parent; |
| 981 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 982 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 984 | spin_unlock(&dentry->d_lock); |
| 985 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 | remove_dir(dentry); |
| 987 | } |
| 988 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 989 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 990 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 991 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 992 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 993 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 994 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 995 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 996 | { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 997 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 998 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 999 | int i; |
| 1000 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1001 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1002 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1003 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1004 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 1005 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | /* Check that any added subsystems are currently free */ |
| 1007 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1008 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1009 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1010 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1011 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1012 | /* |
| 1013 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1014 | * parse_cgroupfs_options should catch that case and refcounts |
| 1015 | * ensure that subsystems won't disappear once selected. |
| 1016 | */ |
| 1017 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1018 | if (ss->root != &rootnode) { |
| 1019 | /* Subsystem isn't free */ |
| 1020 | return -EBUSY; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /* Currently we don't handle adding/removing subsystems when |
| 1025 | * any child cgroups exist. This is theoretically supportable |
| 1026 | * but involves complex error handling, so it's being left until |
| 1027 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1028 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1029 | return -EBUSY; |
| 1030 | |
| 1031 | /* Process each subsystem */ |
| 1032 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1033 | struct cgroup_subsys *ss = subsys[i]; |
| 1034 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1035 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1036 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1037 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1038 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | BUG_ON(!dummytop->subsys[i]); |
| 1040 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1041 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1042 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1043 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1044 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1045 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1046 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1047 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1048 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1049 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1050 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1051 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1052 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1053 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1054 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1055 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1056 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1057 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1058 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1059 | /* subsystem is now free - drop reference on module */ |
| 1060 | module_put(ss->module); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1061 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1062 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1063 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1064 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1065 | /* |
| 1066 | * a refcount was taken, but we already had one, so |
| 1067 | * drop the extra reference. |
| 1068 | */ |
| 1069 | module_put(ss->module); |
| 1070 | #ifdef CONFIG_MODULE_UNLOAD |
| 1071 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1072 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | } else { |
| 1074 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1075 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1078 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1083 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1084 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1085 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1086 | struct cgroup_subsys *ss; |
| 1087 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1088 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1089 | for_each_subsys(root, ss) |
| 1090 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1091 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1092 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1093 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1095 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1096 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1097 | if (strlen(root->release_agent_path)) |
| 1098 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1099 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1100 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1101 | if (strlen(root->name)) |
| 1102 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1103 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1108 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1109 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1110 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1111 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1112 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1113 | /* User explicitly requested empty subsystem */ |
| 1114 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1115 | |
| 1116 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1117 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1118 | }; |
| 1119 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1120 | /* |
| 1121 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1122 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1123 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1124 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1125 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1126 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1127 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1128 | char *token, *o = data; |
| 1129 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1130 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1131 | int i; |
| 1132 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1133 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1134 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1135 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1136 | #ifdef CONFIG_CPUSETS |
| 1137 | mask = ~(1UL << cpuset_subsys_id); |
| 1138 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1139 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1140 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1141 | |
| 1142 | while ((token = strsep(&o, ",")) != NULL) { |
| 1143 | if (!*token) |
| 1144 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1145 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1146 | /* Explicitly have no subsystems */ |
| 1147 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1148 | continue; |
| 1149 | } |
| 1150 | if (!strcmp(token, "all")) { |
| 1151 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1152 | if (one_ss) |
| 1153 | return -EINVAL; |
| 1154 | all_ss = true; |
| 1155 | continue; |
| 1156 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1157 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1158 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1159 | continue; |
| 1160 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1161 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1162 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
| 1165 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1166 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1167 | continue; |
| 1168 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1169 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1170 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1171 | continue; |
| 1172 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1173 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1174 | /* Specifying two release agents is forbidden */ |
| 1175 | if (opts->release_agent) |
| 1176 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1177 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1178 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1179 | if (!opts->release_agent) |
| 1180 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1181 | continue; |
| 1182 | } |
| 1183 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1184 | const char *name = token + 5; |
| 1185 | /* Can't specify an empty name */ |
| 1186 | if (!strlen(name)) |
| 1187 | return -EINVAL; |
| 1188 | /* Must match [\w.-]+ */ |
| 1189 | for (i = 0; i < strlen(name); i++) { |
| 1190 | char c = name[i]; |
| 1191 | if (isalnum(c)) |
| 1192 | continue; |
| 1193 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1194 | continue; |
| 1195 | return -EINVAL; |
| 1196 | } |
| 1197 | /* Specifying two names is forbidden */ |
| 1198 | if (opts->name) |
| 1199 | return -EINVAL; |
| 1200 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1201 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1202 | GFP_KERNEL); |
| 1203 | if (!opts->name) |
| 1204 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1205 | |
| 1206 | continue; |
| 1207 | } |
| 1208 | |
| 1209 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1210 | struct cgroup_subsys *ss = subsys[i]; |
| 1211 | if (ss == NULL) |
| 1212 | continue; |
| 1213 | if (strcmp(token, ss->name)) |
| 1214 | continue; |
| 1215 | if (ss->disabled) |
| 1216 | continue; |
| 1217 | |
| 1218 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1219 | if (all_ss) |
| 1220 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1221 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1222 | one_ss = true; |
| 1223 | |
| 1224 | break; |
| 1225 | } |
| 1226 | if (i == CGROUP_SUBSYS_COUNT) |
| 1227 | return -ENOENT; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1232 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1233 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1234 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1235 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1236 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1237 | struct cgroup_subsys *ss = subsys[i]; |
| 1238 | if (ss == NULL) |
| 1239 | continue; |
| 1240 | if (ss->disabled) |
| 1241 | continue; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1242 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1246 | /* Consistency checks */ |
| 1247 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1248 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1249 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1250 | |
| 1251 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1252 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1253 | return -EINVAL; |
| 1254 | } |
| 1255 | |
| 1256 | if (opts->cpuset_clone_children) { |
| 1257 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1258 | return -EINVAL; |
| 1259 | } |
| 1260 | } |
| 1261 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1262 | /* |
| 1263 | * Option noprefix was introduced just for backward compatibility |
| 1264 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1265 | * the cpuset subsystem. |
| 1266 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1267 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1268 | return -EINVAL; |
| 1269 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1270 | |
| 1271 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1272 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1273 | return -EINVAL; |
| 1274 | |
| 1275 | /* |
| 1276 | * We either have to specify by name or by subsystems. (So all |
| 1277 | * empty hierarchies must have a name). |
| 1278 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1279 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1280 | return -EINVAL; |
| 1281 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1282 | /* |
| 1283 | * Grab references on all the modules we'll need, so the subsystems |
| 1284 | * don't dance around before rebind_subsystems attaches them. This may |
| 1285 | * take duplicate reference counts on a subsystem that's already used, |
| 1286 | * but rebind_subsystems handles this case. |
| 1287 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1288 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1289 | unsigned long bit = 1UL << i; |
| 1290 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1291 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1292 | continue; |
| 1293 | if (!try_module_get(subsys[i]->module)) { |
| 1294 | module_pin_failed = true; |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | if (module_pin_failed) { |
| 1299 | /* |
| 1300 | * oops, one of the modules was going away. this means that we |
| 1301 | * raced with a module_delete call, and to the user this is |
| 1302 | * essentially a "subsystem doesn't exist" case. |
| 1303 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1304 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1305 | /* drop refcounts only on the ones we took */ |
| 1306 | unsigned long bit = 1UL << i; |
| 1307 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1308 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1309 | continue; |
| 1310 | module_put(subsys[i]->module); |
| 1311 | } |
| 1312 | return -ENOENT; |
| 1313 | } |
| 1314 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1315 | return 0; |
| 1316 | } |
| 1317 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1318 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1319 | { |
| 1320 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1321 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1322 | unsigned long bit = 1UL << i; |
| 1323 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1324 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1325 | continue; |
| 1326 | module_put(subsys[i]->module); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1331 | { |
| 1332 | int ret = 0; |
| 1333 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1334 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1335 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1336 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1337 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1338 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1339 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1340 | return -EINVAL; |
| 1341 | } |
| 1342 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1343 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1345 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1346 | |
| 1347 | /* See what subsystems are wanted */ |
| 1348 | ret = parse_cgroupfs_options(data, &opts); |
| 1349 | if (ret) |
| 1350 | goto out_unlock; |
| 1351 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1352 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1353 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1354 | task_tgid_nr(current), current->comm); |
| 1355 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1356 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1357 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1358 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1359 | /* Don't allow flags or name to change at remount */ |
| 1360 | if (opts.flags != root->flags || |
| 1361 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1362 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1363 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1364 | goto out_unlock; |
| 1365 | } |
| 1366 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1367 | /* |
| 1368 | * Clear out the files of subsystems that should be removed, do |
| 1369 | * this before rebind_subsystems, since rebind_subsystems may |
| 1370 | * change this hierarchy's subsys_list. |
| 1371 | */ |
| 1372 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1373 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1374 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1375 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1376 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1377 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1378 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1379 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1380 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1382 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1383 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1384 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1385 | if (opts.release_agent) |
| 1386 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1387 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1388 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1389 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1390 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1392 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1393 | return ret; |
| 1394 | } |
| 1395 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1396 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1397 | .statfs = simple_statfs, |
| 1398 | .drop_inode = generic_delete_inode, |
| 1399 | .show_options = cgroup_show_options, |
| 1400 | .remount_fs = cgroup_remount, |
| 1401 | }; |
| 1402 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1403 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1404 | { |
| 1405 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1406 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1407 | INIT_LIST_HEAD(&cgrp->files); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1408 | INIT_LIST_HEAD(&cgrp->css_sets); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1409 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1410 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1411 | INIT_LIST_HEAD(&cgrp->pidlists); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 1412 | INIT_WORK(&cgrp->free_work, cgroup_free_fn); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1413 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1414 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1415 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1416 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1417 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1418 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1419 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1420 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1421 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1422 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1423 | INIT_LIST_HEAD(&root->subsys_list); |
| 1424 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1425 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1426 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1427 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1428 | cgrp->name = &root_cgroup_name; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1429 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1430 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1433 | static int cgroup_init_root_id(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1434 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1435 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1436 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1437 | lockdep_assert_held(&cgroup_mutex); |
| 1438 | lockdep_assert_held(&cgroup_root_mutex); |
| 1439 | |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1440 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL); |
| 1441 | if (id < 0) |
| 1442 | return id; |
| 1443 | |
| 1444 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1449 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1450 | lockdep_assert_held(&cgroup_mutex); |
| 1451 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1452 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1453 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1454 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1455 | root->hierarchy_id = 0; |
| 1456 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1459 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1460 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1461 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1462 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1463 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1464 | /* If we asked for a name then it must match */ |
| 1465 | if (opts->name && strcmp(opts->name, root->name)) |
| 1466 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1467 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1468 | /* |
| 1469 | * If we asked for subsystems (or explicitly for no |
| 1470 | * subsystems) then they must match |
| 1471 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1472 | if ((opts->subsys_mask || opts->none) |
| 1473 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1474 | return 0; |
| 1475 | |
| 1476 | return 1; |
| 1477 | } |
| 1478 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1479 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1480 | { |
| 1481 | struct cgroupfs_root *root; |
| 1482 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1483 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1484 | return NULL; |
| 1485 | |
| 1486 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1487 | if (!root) |
| 1488 | return ERR_PTR(-ENOMEM); |
| 1489 | |
| 1490 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1491 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1492 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1493 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1494 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1495 | if (opts->release_agent) |
| 1496 | strcpy(root->release_agent_path, opts->release_agent); |
| 1497 | if (opts->name) |
| 1498 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1499 | if (opts->cpuset_clone_children) |
| 1500 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1501 | return root; |
| 1502 | } |
| 1503 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1504 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1505 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1506 | if (root) { |
| 1507 | /* hierarhcy ID shoulid already have been released */ |
| 1508 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1509 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1510 | ida_destroy(&root->cgroup_ida); |
| 1511 | kfree(root); |
| 1512 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1515 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1516 | { |
| 1517 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1518 | struct cgroup_sb_opts *opts = data; |
| 1519 | |
| 1520 | /* If we don't have a new root, we can't set up a new sb */ |
| 1521 | if (!opts->new_root) |
| 1522 | return -EINVAL; |
| 1523 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1524 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1525 | |
| 1526 | ret = set_anon_super(sb, NULL); |
| 1527 | if (ret) |
| 1528 | return ret; |
| 1529 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1530 | sb->s_fs_info = opts->new_root; |
| 1531 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1532 | |
| 1533 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1534 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1535 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1536 | sb->s_op = &cgroup_ops; |
| 1537 | |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1542 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1543 | static const struct dentry_operations cgroup_dops = { |
| 1544 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1545 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1546 | }; |
| 1547 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | struct inode *inode = |
| 1549 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | |
| 1551 | if (!inode) |
| 1552 | return -ENOMEM; |
| 1553 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1554 | inode->i_fop = &simple_dir_operations; |
| 1555 | inode->i_op = &cgroup_dir_inode_operations; |
| 1556 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1557 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1558 | sb->s_root = d_make_root(inode); |
| 1559 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1560 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1561 | /* for everything else we want ->d_op set */ |
| 1562 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
| 1565 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1566 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1567 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1568 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1569 | { |
| 1570 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1571 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1572 | int ret = 0; |
| 1573 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1574 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1575 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1576 | |
| 1577 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1578 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1579 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1580 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1581 | if (ret) |
| 1582 | goto out_err; |
| 1583 | |
| 1584 | /* |
| 1585 | * Allocate a new cgroup root. We may not need it if we're |
| 1586 | * reusing an existing hierarchy. |
| 1587 | */ |
| 1588 | new_root = cgroup_root_from_opts(&opts); |
| 1589 | if (IS_ERR(new_root)) { |
| 1590 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1591 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1592 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1593 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1594 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1595 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1596 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1597 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1598 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1599 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1600 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1603 | root = sb->s_fs_info; |
| 1604 | BUG_ON(!root); |
| 1605 | if (root == opts.new_root) { |
| 1606 | /* We used the new root structure, so this is a new hierarchy */ |
| 1607 | struct list_head tmp_cg_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1608 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1609 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1610 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1611 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1612 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1613 | |
| 1614 | BUG_ON(sb->s_root != NULL); |
| 1615 | |
| 1616 | ret = cgroup_get_rootdir(sb); |
| 1617 | if (ret) |
| 1618 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1619 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1620 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1621 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1622 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1623 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1624 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1625 | /* Check for name clashes with existing mounts */ |
| 1626 | ret = -EBUSY; |
| 1627 | if (strlen(root->name)) |
| 1628 | for_each_active_root(existing_root) |
| 1629 | if (!strcmp(existing_root->name, root->name)) |
| 1630 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1631 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1632 | /* |
| 1633 | * We're accessing css_set_count without locking |
| 1634 | * css_set_lock here, but that's OK - it can only be |
| 1635 | * increased by someone holding cgroup_lock, and |
| 1636 | * that's us. The worst that can happen is that we |
| 1637 | * have some link structures left over |
| 1638 | */ |
| 1639 | ret = allocate_cg_links(css_set_count, &tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1640 | if (ret) |
| 1641 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1642 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1643 | ret = cgroup_init_root_id(root); |
| 1644 | if (ret) |
| 1645 | goto unlock_drop; |
| 1646 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1647 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1648 | if (ret == -EBUSY) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1649 | free_cg_links(&tmp_cg_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1650 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1652 | /* |
| 1653 | * There must be no failure case after here, since rebinding |
| 1654 | * takes care of subsystems' refcounts, which are explicitly |
| 1655 | * dropped in the failure exit path. |
| 1656 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | |
| 1658 | /* EBUSY should be the only error here */ |
| 1659 | BUG_ON(ret); |
| 1660 | |
| 1661 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1662 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1663 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1664 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | root->top_cgroup.dentry = sb->s_root; |
| 1666 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1667 | /* Link the top cgroup in this hierarchy into all |
| 1668 | * the css_set objects */ |
| 1669 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1670 | hash_for_each(css_set_table, i, cset, hlist) |
| 1671 | link_css_set(&tmp_cg_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1672 | write_unlock(&css_set_lock); |
| 1673 | |
| 1674 | free_cg_links(&tmp_cg_links); |
| 1675 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1676 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1677 | BUG_ON(root->number_of_cgroups != 1); |
| 1678 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1679 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1680 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1681 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1682 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1683 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1684 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1685 | } else { |
| 1686 | /* |
| 1687 | * We re-used an existing hierarchy - the new root (if |
| 1688 | * any) is not needed |
| 1689 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1690 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1691 | |
| 1692 | if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) && |
| 1693 | root->flags != opts.flags) { |
| 1694 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1695 | ret = -EINVAL; |
| 1696 | goto drop_new_super; |
| 1697 | } |
| 1698 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1699 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1700 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1703 | kfree(opts.release_agent); |
| 1704 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1705 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1706 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1707 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1708 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1709 | mutex_unlock(&cgroup_root_mutex); |
| 1710 | mutex_unlock(&cgroup_mutex); |
| 1711 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1712 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1713 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1714 | drop_modules: |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1715 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1716 | out_err: |
| 1717 | kfree(opts.release_agent); |
| 1718 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1719 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1723 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1724 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | int ret; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1726 | struct cg_cgroup_link *link; |
| 1727 | struct cg_cgroup_link *saved_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1728 | |
| 1729 | BUG_ON(!root); |
| 1730 | |
| 1731 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1732 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1733 | |
| 1734 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1735 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1736 | |
| 1737 | /* Rebind all subsystems back to the default hierarchy */ |
| 1738 | ret = rebind_subsystems(root, 0); |
| 1739 | /* Shouldn't be able to fail ... */ |
| 1740 | BUG_ON(ret); |
| 1741 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Release all the links from css_sets to this hierarchy's |
| 1744 | * root cgroup |
| 1745 | */ |
| 1746 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1747 | |
| 1748 | list_for_each_entry_safe(link, saved_link, &cgrp->css_sets, |
| 1749 | cgrp_link_list) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1750 | list_del(&link->cg_link_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1751 | list_del(&link->cgrp_link_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1752 | kfree(link); |
| 1753 | } |
| 1754 | write_unlock(&css_set_lock); |
| 1755 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1756 | if (!list_empty(&root->root_list)) { |
| 1757 | list_del(&root->root_list); |
| 1758 | root_count--; |
| 1759 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1760 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1761 | cgroup_exit_root_id(root); |
| 1762 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1763 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | mutex_unlock(&cgroup_mutex); |
| 1765 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1766 | simple_xattrs_free(&cgrp->xattrs); |
| 1767 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1768 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1769 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | static struct file_system_type cgroup_fs_type = { |
| 1773 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1774 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1775 | .kill_sb = cgroup_kill_sb, |
| 1776 | }; |
| 1777 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1778 | static struct kobject *cgroup_kobj; |
| 1779 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1780 | /** |
| 1781 | * cgroup_path - generate the path of a cgroup |
| 1782 | * @cgrp: the cgroup in question |
| 1783 | * @buf: the buffer to write the path into |
| 1784 | * @buflen: the length of the buffer |
| 1785 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1786 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1787 | * |
| 1788 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1789 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1790 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1791 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1792 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1793 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1794 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1795 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1796 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1797 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1798 | if (!cgrp->parent) { |
| 1799 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1800 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1801 | return 0; |
| 1802 | } |
| 1803 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1804 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1805 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1806 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1807 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1808 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | const char *name = cgroup_name(cgrp); |
| 1810 | int len; |
| 1811 | |
| 1812 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1813 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1814 | goto out; |
| 1815 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1816 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1817 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1818 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1819 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1820 | |
| 1821 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1822 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1823 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1824 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1825 | out: |
| 1826 | rcu_read_unlock(); |
| 1827 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1828 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1829 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1830 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1831 | /** |
| 1832 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1833 | * @task: target task |
| 1834 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1835 | * @buf: the buffer to write the path into |
| 1836 | * @buflen: the length of the buffer |
| 1837 | * |
| 1838 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1839 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1840 | * be used inside locks used by cgroup controller callbacks. |
| 1841 | */ |
| 1842 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1843 | char *buf, size_t buflen) |
| 1844 | { |
| 1845 | struct cgroupfs_root *root; |
| 1846 | struct cgroup *cgrp = NULL; |
| 1847 | int ret = -ENOENT; |
| 1848 | |
| 1849 | mutex_lock(&cgroup_mutex); |
| 1850 | |
| 1851 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1852 | if (root) { |
| 1853 | cgrp = task_cgroup_from_root(task, root); |
| 1854 | ret = cgroup_path(cgrp, buf, buflen); |
| 1855 | } |
| 1856 | |
| 1857 | mutex_unlock(&cgroup_mutex); |
| 1858 | |
| 1859 | return ret; |
| 1860 | } |
| 1861 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1862 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1863 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1864 | * Control Group taskset |
| 1865 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1866 | struct task_and_cgroup { |
| 1867 | struct task_struct *task; |
| 1868 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1869 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1870 | }; |
| 1871 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1872 | struct cgroup_taskset { |
| 1873 | struct task_and_cgroup single; |
| 1874 | struct flex_array *tc_array; |
| 1875 | int tc_array_len; |
| 1876 | int idx; |
| 1877 | struct cgroup *cur_cgrp; |
| 1878 | }; |
| 1879 | |
| 1880 | /** |
| 1881 | * cgroup_taskset_first - reset taskset and return the first task |
| 1882 | * @tset: taskset of interest |
| 1883 | * |
| 1884 | * @tset iteration is initialized and the first task is returned. |
| 1885 | */ |
| 1886 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1887 | { |
| 1888 | if (tset->tc_array) { |
| 1889 | tset->idx = 0; |
| 1890 | return cgroup_taskset_next(tset); |
| 1891 | } else { |
| 1892 | tset->cur_cgrp = tset->single.cgrp; |
| 1893 | return tset->single.task; |
| 1894 | } |
| 1895 | } |
| 1896 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1897 | |
| 1898 | /** |
| 1899 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1900 | * @tset: taskset of interest |
| 1901 | * |
| 1902 | * Return the next task in @tset. Iteration must have been initialized |
| 1903 | * with cgroup_taskset_first(). |
| 1904 | */ |
| 1905 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1906 | { |
| 1907 | struct task_and_cgroup *tc; |
| 1908 | |
| 1909 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1910 | return NULL; |
| 1911 | |
| 1912 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1913 | tset->cur_cgrp = tc->cgrp; |
| 1914 | return tc->task; |
| 1915 | } |
| 1916 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1917 | |
| 1918 | /** |
| 1919 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1920 | * @tset: taskset of interest |
| 1921 | * |
| 1922 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1923 | * function must be preceded by either cgroup_taskset_first() or |
| 1924 | * cgroup_taskset_next(). |
| 1925 | */ |
| 1926 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1927 | { |
| 1928 | return tset->cur_cgrp; |
| 1929 | } |
| 1930 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1931 | |
| 1932 | /** |
| 1933 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1934 | * @tset: taskset of interest |
| 1935 | */ |
| 1936 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1937 | { |
| 1938 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1939 | } |
| 1940 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1941 | |
| 1942 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1945 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1946 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1947 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1948 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1949 | struct task_struct *tsk, |
| 1950 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1952 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1955 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1956 | * setting such that we can't race against cgroup_exit() changing the |
| 1957 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1958 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1959 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1960 | old_cset = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1962 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1963 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1964 | task_unlock(tsk); |
| 1965 | |
| 1966 | /* Update the css_set linked lists if we're using them */ |
| 1967 | write_lock(&css_set_lock); |
| 1968 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1969 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | write_unlock(&css_set_lock); |
| 1971 | |
| 1972 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1973 | * We just gained a reference on old_cset by taking it from the |
| 1974 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1975 | * 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] | 1976 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 1977 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1978 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1981 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1982 | * 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] | 1983 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1984 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1985 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1986 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1987 | * 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] | 1988 | * 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] | 1989 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1990 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1991 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1992 | { |
| 1993 | int retval, i, group_size; |
| 1994 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1995 | struct cgroupfs_root *root = cgrp->root; |
| 1996 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1997 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1998 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1999 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2000 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2001 | |
| 2002 | /* |
| 2003 | * step 0: in order to do expensive, possibly blocking operations for |
| 2004 | * every thread, we cannot iterate the thread group list, since it needs |
| 2005 | * 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] | 2006 | * group - group_rwsem prevents new threads from appearing, and if |
| 2007 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2008 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2009 | if (threadgroup) |
| 2010 | group_size = get_nr_threads(tsk); |
| 2011 | else |
| 2012 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2013 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2014 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2015 | if (!group) |
| 2016 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2017 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2018 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2019 | if (retval) |
| 2020 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2021 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2022 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2023 | /* |
| 2024 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2025 | * already PF_EXITING could be freed from underneath us unless we |
| 2026 | * take an rcu_read_lock. |
| 2027 | */ |
| 2028 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2030 | struct task_and_cgroup ent; |
| 2031 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2032 | /* @tsk either already exited or can't exit until the end */ |
| 2033 | if (tsk->flags & PF_EXITING) |
| 2034 | continue; |
| 2035 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2036 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2037 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2038 | ent.task = tsk; |
| 2039 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2040 | /* nothing to do if this task is already in the cgroup */ |
| 2041 | if (ent.cgrp == cgrp) |
| 2042 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2043 | /* |
| 2044 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2045 | * earlier, but it's good form to communicate our expectations. |
| 2046 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2047 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2048 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2049 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2050 | |
| 2051 | if (!threadgroup) |
| 2052 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2053 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2054 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2055 | /* remember the number of threads in the array for later. */ |
| 2056 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2057 | tset.tc_array = group; |
| 2058 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2059 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2060 | /* methods shouldn't be called if no task is actually migrating */ |
| 2061 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2062 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2063 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2064 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | /* |
| 2066 | * step 1: check that we can legitimately attach to the cgroup. |
| 2067 | */ |
| 2068 | for_each_subsys(root, ss) { |
| 2069 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2070 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | if (retval) { |
| 2072 | failed_ss = ss; |
| 2073 | goto out_cancel_attach; |
| 2074 | } |
| 2075 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /* |
| 2079 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2080 | * we use find_css_set, which allocates a new one if necessary. |
| 2081 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2083 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2084 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2085 | if (!tc->cg) { |
| 2086 | retval = -ENOMEM; |
| 2087 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2092 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2093 | * proceed to move all tasks to the new cgroup. There are no |
| 2094 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2095 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2096 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2097 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2098 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2099 | } |
| 2100 | /* nothing is sensitive to fork() after this point. */ |
| 2101 | |
| 2102 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2103 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | */ |
| 2105 | for_each_subsys(root, ss) { |
| 2106 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2107 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * step 5: success! and cleanup |
| 2112 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2113 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2114 | out_put_css_set_refs: |
| 2115 | if (retval) { |
| 2116 | for (i = 0; i < group_size; i++) { |
| 2117 | tc = flex_array_get(group, i); |
| 2118 | if (!tc->cg) |
| 2119 | break; |
| 2120 | put_css_set(tc->cg); |
| 2121 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | } |
| 2123 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2124 | if (retval) { |
| 2125 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2126 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2127 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2128 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2129 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2130 | } |
| 2131 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2132 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2133 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | return retval; |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * 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] | 2139 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2140 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | */ |
| 2142 | 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] | 2143 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2144 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2145 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2146 | int ret; |
| 2147 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2148 | if (!cgroup_lock_live_group(cgrp)) |
| 2149 | return -ENODEV; |
| 2150 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2151 | retry_find_task: |
| 2152 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2153 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2154 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2156 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2157 | ret= -ESRCH; |
| 2158 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2159 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2160 | /* |
| 2161 | * even if we're attaching all tasks in the thread group, we |
| 2162 | * only need to check permissions on one of them. |
| 2163 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2164 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2165 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2166 | !uid_eq(cred->euid, tcred->uid) && |
| 2167 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2168 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2169 | ret = -EACCES; |
| 2170 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2171 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2172 | } else |
| 2173 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2174 | |
| 2175 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2176 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2177 | |
| 2178 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2179 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2180 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2181 | * with no rt_runtime allocated. Just say no. |
| 2182 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2183 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2184 | ret = -EINVAL; |
| 2185 | rcu_read_unlock(); |
| 2186 | goto out_unlock_cgroup; |
| 2187 | } |
| 2188 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2189 | get_task_struct(tsk); |
| 2190 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2191 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2192 | threadgroup_lock(tsk); |
| 2193 | if (threadgroup) { |
| 2194 | if (!thread_group_leader(tsk)) { |
| 2195 | /* |
| 2196 | * a race with de_thread from another thread's exec() |
| 2197 | * may strip us of our leadership, if this happens, |
| 2198 | * there is no choice but to throw this task away and |
| 2199 | * try again; this is |
| 2200 | * "double-double-toil-and-trouble-check locking". |
| 2201 | */ |
| 2202 | threadgroup_unlock(tsk); |
| 2203 | put_task_struct(tsk); |
| 2204 | goto retry_find_task; |
| 2205 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2209 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2210 | threadgroup_unlock(tsk); |
| 2211 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2212 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2213 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2214 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2215 | return ret; |
| 2216 | } |
| 2217 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2218 | /** |
| 2219 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2220 | * @from: attach to all cgroups of a given task |
| 2221 | * @tsk: the task to be attached |
| 2222 | */ |
| 2223 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2224 | { |
| 2225 | struct cgroupfs_root *root; |
| 2226 | int retval = 0; |
| 2227 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2228 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2229 | for_each_active_root(root) { |
| 2230 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2231 | |
| 2232 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2233 | if (retval) |
| 2234 | break; |
| 2235 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2236 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2237 | |
| 2238 | return retval; |
| 2239 | } |
| 2240 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2241 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2242 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2243 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2244 | return attach_task_by_pid(cgrp, pid, false); |
| 2245 | } |
| 2246 | |
| 2247 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2248 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2249 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2252 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2253 | const char *buffer) |
| 2254 | { |
| 2255 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2256 | if (strlen(buffer) >= PATH_MAX) |
| 2257 | return -EINVAL; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2258 | if (!cgroup_lock_live_group(cgrp)) |
| 2259 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2260 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2261 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2262 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2263 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | |
| 2267 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2268 | struct seq_file *seq) |
| 2269 | { |
| 2270 | if (!cgroup_lock_live_group(cgrp)) |
| 2271 | return -ENODEV; |
| 2272 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2273 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2274 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2275 | return 0; |
| 2276 | } |
| 2277 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2278 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2279 | struct seq_file *seq) |
| 2280 | { |
| 2281 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2282 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2285 | /* A buffer size big enough for numbers or short strings */ |
| 2286 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2287 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2288 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2289 | struct file *file, |
| 2290 | const char __user *userbuf, |
| 2291 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2292 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2293 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2294 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2295 | char *end; |
| 2296 | |
| 2297 | if (!nbytes) |
| 2298 | return -EINVAL; |
| 2299 | if (nbytes >= sizeof(buffer)) |
| 2300 | return -E2BIG; |
| 2301 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2302 | return -EFAULT; |
| 2303 | |
| 2304 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2305 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2306 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2307 | if (*end) |
| 2308 | return -EINVAL; |
| 2309 | retval = cft->write_u64(cgrp, cft, val); |
| 2310 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2311 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2312 | if (*end) |
| 2313 | return -EINVAL; |
| 2314 | retval = cft->write_s64(cgrp, cft, val); |
| 2315 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2316 | if (!retval) |
| 2317 | retval = nbytes; |
| 2318 | return retval; |
| 2319 | } |
| 2320 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2321 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2322 | struct file *file, |
| 2323 | const char __user *userbuf, |
| 2324 | size_t nbytes, loff_t *unused_ppos) |
| 2325 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2326 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2327 | int retval = 0; |
| 2328 | size_t max_bytes = cft->max_write_len; |
| 2329 | char *buffer = local_buffer; |
| 2330 | |
| 2331 | if (!max_bytes) |
| 2332 | max_bytes = sizeof(local_buffer) - 1; |
| 2333 | if (nbytes >= max_bytes) |
| 2334 | return -E2BIG; |
| 2335 | /* Allocate a dynamic buffer if we need one */ |
| 2336 | if (nbytes >= sizeof(local_buffer)) { |
| 2337 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2338 | if (buffer == NULL) |
| 2339 | return -ENOMEM; |
| 2340 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2341 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2342 | retval = -EFAULT; |
| 2343 | goto out; |
| 2344 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2345 | |
| 2346 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2347 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2348 | if (!retval) |
| 2349 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2350 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2351 | if (buffer != local_buffer) |
| 2352 | kfree(buffer); |
| 2353 | return retval; |
| 2354 | } |
| 2355 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2356 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2357 | size_t nbytes, loff_t *ppos) |
| 2358 | { |
| 2359 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2360 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2361 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2362 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2363 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2364 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2365 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2366 | if (cft->write_u64 || cft->write_s64) |
| 2367 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2368 | if (cft->write_string) |
| 2369 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2370 | if (cft->trigger) { |
| 2371 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2372 | return ret ? ret : nbytes; |
| 2373 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2374 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2377 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2378 | struct file *file, |
| 2379 | char __user *buf, size_t nbytes, |
| 2380 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2381 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2382 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2383 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2384 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2385 | |
| 2386 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2387 | } |
| 2388 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2389 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2390 | struct file *file, |
| 2391 | char __user *buf, size_t nbytes, |
| 2392 | loff_t *ppos) |
| 2393 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2394 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2395 | s64 val = cft->read_s64(cgrp, cft); |
| 2396 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2397 | |
| 2398 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2399 | } |
| 2400 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2401 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2402 | size_t nbytes, loff_t *ppos) |
| 2403 | { |
| 2404 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2405 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2406 | |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2407 | if (cgroup_is_removed(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2408 | return -ENODEV; |
| 2409 | |
| 2410 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2411 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2412 | if (cft->read_u64) |
| 2413 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2414 | if (cft->read_s64) |
| 2415 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2416 | return -EINVAL; |
| 2417 | } |
| 2418 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2419 | /* |
| 2420 | * seqfile ops/methods for returning structured data. Currently just |
| 2421 | * supports string->u64 maps, but can be extended in future. |
| 2422 | */ |
| 2423 | |
| 2424 | struct cgroup_seqfile_state { |
| 2425 | struct cftype *cft; |
| 2426 | struct cgroup *cgroup; |
| 2427 | }; |
| 2428 | |
| 2429 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2430 | { |
| 2431 | struct seq_file *sf = cb->state; |
| 2432 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2433 | } |
| 2434 | |
| 2435 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2436 | { |
| 2437 | struct cgroup_seqfile_state *state = m->private; |
| 2438 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2439 | if (cft->read_map) { |
| 2440 | struct cgroup_map_cb cb = { |
| 2441 | .fill = cgroup_map_add, |
| 2442 | .state = m, |
| 2443 | }; |
| 2444 | return cft->read_map(state->cgroup, cft, &cb); |
| 2445 | } |
| 2446 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2449 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2450 | { |
| 2451 | struct seq_file *seq = file->private_data; |
| 2452 | kfree(seq->private); |
| 2453 | return single_release(inode, file); |
| 2454 | } |
| 2455 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2456 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2457 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2458 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2459 | .llseek = seq_lseek, |
| 2460 | .release = cgroup_seqfile_release, |
| 2461 | }; |
| 2462 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2463 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2464 | { |
| 2465 | int err; |
| 2466 | struct cftype *cft; |
| 2467 | |
| 2468 | err = generic_file_open(inode, file); |
| 2469 | if (err) |
| 2470 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2471 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2472 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2473 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2474 | struct cgroup_seqfile_state *state = |
| 2475 | kzalloc(sizeof(*state), GFP_USER); |
| 2476 | if (!state) |
| 2477 | return -ENOMEM; |
| 2478 | state->cft = cft; |
| 2479 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2480 | file->f_op = &cgroup_seqfile_operations; |
| 2481 | err = single_open(file, cgroup_seqfile_show, state); |
| 2482 | if (err < 0) |
| 2483 | kfree(state); |
| 2484 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2485 | err = cft->open(inode, file); |
| 2486 | else |
| 2487 | err = 0; |
| 2488 | |
| 2489 | return err; |
| 2490 | } |
| 2491 | |
| 2492 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2493 | { |
| 2494 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2495 | if (cft->release) |
| 2496 | return cft->release(inode, file); |
| 2497 | return 0; |
| 2498 | } |
| 2499 | |
| 2500 | /* |
| 2501 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2502 | */ |
| 2503 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2504 | struct inode *new_dir, struct dentry *new_dentry) |
| 2505 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2506 | int ret; |
| 2507 | struct cgroup_name *name, *old_name; |
| 2508 | struct cgroup *cgrp; |
| 2509 | |
| 2510 | /* |
| 2511 | * It's convinient to use parent dir's i_mutex to protected |
| 2512 | * cgrp->name. |
| 2513 | */ |
| 2514 | lockdep_assert_held(&old_dir->i_mutex); |
| 2515 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2516 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2517 | return -ENOTDIR; |
| 2518 | if (new_dentry->d_inode) |
| 2519 | return -EEXIST; |
| 2520 | if (old_dir != new_dir) |
| 2521 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2522 | |
| 2523 | cgrp = __d_cgrp(old_dentry); |
| 2524 | |
| 2525 | name = cgroup_alloc_name(new_dentry); |
| 2526 | if (!name) |
| 2527 | return -ENOMEM; |
| 2528 | |
| 2529 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2530 | if (ret) { |
| 2531 | kfree(name); |
| 2532 | return ret; |
| 2533 | } |
| 2534 | |
| 2535 | old_name = cgrp->name; |
| 2536 | rcu_assign_pointer(cgrp->name, name); |
| 2537 | |
| 2538 | kfree_rcu(old_name, rcu_head); |
| 2539 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2542 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2543 | { |
| 2544 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2545 | return &__d_cgrp(dentry)->xattrs; |
| 2546 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2547 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | static inline int xattr_enabled(struct dentry *dentry) |
| 2551 | { |
| 2552 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2553 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | static bool is_valid_xattr(const char *name) |
| 2557 | { |
| 2558 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2559 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2560 | return true; |
| 2561 | return false; |
| 2562 | } |
| 2563 | |
| 2564 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2565 | const void *val, size_t size, int flags) |
| 2566 | { |
| 2567 | if (!xattr_enabled(dentry)) |
| 2568 | return -EOPNOTSUPP; |
| 2569 | if (!is_valid_xattr(name)) |
| 2570 | return -EINVAL; |
| 2571 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2572 | } |
| 2573 | |
| 2574 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2575 | { |
| 2576 | if (!xattr_enabled(dentry)) |
| 2577 | return -EOPNOTSUPP; |
| 2578 | if (!is_valid_xattr(name)) |
| 2579 | return -EINVAL; |
| 2580 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2581 | } |
| 2582 | |
| 2583 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2584 | void *buf, size_t size) |
| 2585 | { |
| 2586 | if (!xattr_enabled(dentry)) |
| 2587 | return -EOPNOTSUPP; |
| 2588 | if (!is_valid_xattr(name)) |
| 2589 | return -EINVAL; |
| 2590 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2591 | } |
| 2592 | |
| 2593 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2594 | { |
| 2595 | if (!xattr_enabled(dentry)) |
| 2596 | return -EOPNOTSUPP; |
| 2597 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2598 | } |
| 2599 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2600 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2601 | .read = cgroup_file_read, |
| 2602 | .write = cgroup_file_write, |
| 2603 | .llseek = generic_file_llseek, |
| 2604 | .open = cgroup_file_open, |
| 2605 | .release = cgroup_file_release, |
| 2606 | }; |
| 2607 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2608 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2609 | .setxattr = cgroup_setxattr, |
| 2610 | .getxattr = cgroup_getxattr, |
| 2611 | .listxattr = cgroup_listxattr, |
| 2612 | .removexattr = cgroup_removexattr, |
| 2613 | }; |
| 2614 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2615 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2616 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2617 | .mkdir = cgroup_mkdir, |
| 2618 | .rmdir = cgroup_rmdir, |
| 2619 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2620 | .setxattr = cgroup_setxattr, |
| 2621 | .getxattr = cgroup_getxattr, |
| 2622 | .listxattr = cgroup_listxattr, |
| 2623 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2624 | }; |
| 2625 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2626 | 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] | 2627 | { |
| 2628 | if (dentry->d_name.len > NAME_MAX) |
| 2629 | return ERR_PTR(-ENAMETOOLONG); |
| 2630 | d_add(dentry, NULL); |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2634 | /* |
| 2635 | * Check if a file is a control file |
| 2636 | */ |
| 2637 | static inline struct cftype *__file_cft(struct file *file) |
| 2638 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2639 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2640 | return ERR_PTR(-EINVAL); |
| 2641 | return __d_cft(file->f_dentry); |
| 2642 | } |
| 2643 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2644 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2645 | struct super_block *sb) |
| 2646 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2647 | struct inode *inode; |
| 2648 | |
| 2649 | if (!dentry) |
| 2650 | return -ENOENT; |
| 2651 | if (dentry->d_inode) |
| 2652 | return -EEXIST; |
| 2653 | |
| 2654 | inode = cgroup_new_inode(mode, sb); |
| 2655 | if (!inode) |
| 2656 | return -ENOMEM; |
| 2657 | |
| 2658 | if (S_ISDIR(mode)) { |
| 2659 | inode->i_op = &cgroup_dir_inode_operations; |
| 2660 | inode->i_fop = &simple_dir_operations; |
| 2661 | |
| 2662 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2663 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2664 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2665 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2666 | /* |
| 2667 | * Control reaches here with cgroup_mutex held. |
| 2668 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2669 | * want to populate it immediately without releasing |
| 2670 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2671 | * yet, trylock will always succeed without affecting |
| 2672 | * lockdep checks. |
| 2673 | */ |
| 2674 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2675 | } else if (S_ISREG(mode)) { |
| 2676 | inode->i_size = 0; |
| 2677 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2678 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2679 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2680 | d_instantiate(dentry, inode); |
| 2681 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2685 | /** |
| 2686 | * cgroup_file_mode - deduce file mode of a control file |
| 2687 | * @cft: the control file in question |
| 2688 | * |
| 2689 | * returns cft->mode if ->mode is not 0 |
| 2690 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2691 | * returns S_IRUGO if it has only a read handler |
| 2692 | * returns S_IWUSR if it has only a write hander |
| 2693 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2694 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2695 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2696 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2697 | |
| 2698 | if (cft->mode) |
| 2699 | return cft->mode; |
| 2700 | |
| 2701 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2702 | cft->read_map || cft->read_seq_string) |
| 2703 | mode |= S_IRUGO; |
| 2704 | |
| 2705 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2706 | cft->write_string || cft->trigger) |
| 2707 | mode |= S_IWUSR; |
| 2708 | |
| 2709 | return mode; |
| 2710 | } |
| 2711 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2712 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2713 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2714 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2715 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2716 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2717 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2718 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2719 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2720 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2721 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2722 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2723 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2724 | strcpy(name, subsys->name); |
| 2725 | strcat(name, "."); |
| 2726 | } |
| 2727 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2728 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2729 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2730 | |
| 2731 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2732 | if (!cfe) |
| 2733 | return -ENOMEM; |
| 2734 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2735 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2736 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2737 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2738 | goto out; |
| 2739 | } |
| 2740 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2741 | cfe->type = (void *)cft; |
| 2742 | cfe->dentry = dentry; |
| 2743 | dentry->d_fsdata = cfe; |
| 2744 | simple_xattrs_init(&cfe->xattrs); |
| 2745 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2746 | mode = cgroup_file_mode(cft); |
| 2747 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2748 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2749 | list_add_tail(&cfe->node, &parent->files); |
| 2750 | cfe = NULL; |
| 2751 | } |
| 2752 | dput(dentry); |
| 2753 | out: |
| 2754 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2755 | return error; |
| 2756 | } |
| 2757 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2758 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2759 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2760 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2761 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2762 | int err, ret = 0; |
| 2763 | |
| 2764 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2765 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2766 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2767 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2768 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2769 | continue; |
| 2770 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2771 | continue; |
| 2772 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2773 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2774 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2775 | if (err) |
| 2776 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2777 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2778 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2779 | } else { |
| 2780 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2781 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2782 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2783 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2786 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2787 | |
| 2788 | static void cgroup_cfts_prepare(void) |
| 2789 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2790 | { |
| 2791 | /* |
| 2792 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2793 | * the existing cgroups under cgroup_mutex and create files. |
| 2794 | * Instead, we increment reference on all cgroups and build list of |
| 2795 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2796 | * exclusive access to the field. |
| 2797 | */ |
| 2798 | mutex_lock(&cgroup_cft_mutex); |
| 2799 | mutex_lock(&cgroup_mutex); |
| 2800 | } |
| 2801 | |
| 2802 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2803 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2804 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2805 | { |
| 2806 | LIST_HEAD(pending); |
| 2807 | struct cgroup *cgrp, *n; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2808 | |
| 2809 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
| 2810 | if (cfts && ss->root != &rootnode) { |
| 2811 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2812 | dget(cgrp->dentry); |
| 2813 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | mutex_unlock(&cgroup_mutex); |
| 2818 | |
| 2819 | /* |
| 2820 | * All new cgroups will see @cfts update on @ss->cftsets. Add/rm |
| 2821 | * files for all cgroups which were created before. |
| 2822 | */ |
| 2823 | list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { |
| 2824 | struct inode *inode = cgrp->dentry->d_inode; |
| 2825 | |
| 2826 | mutex_lock(&inode->i_mutex); |
| 2827 | mutex_lock(&cgroup_mutex); |
| 2828 | if (!cgroup_is_removed(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2829 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2830 | mutex_unlock(&cgroup_mutex); |
| 2831 | mutex_unlock(&inode->i_mutex); |
| 2832 | |
| 2833 | list_del_init(&cgrp->cft_q_node); |
| 2834 | dput(cgrp->dentry); |
| 2835 | } |
| 2836 | |
| 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; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2911 | struct cg_cgroup_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); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 2914 | list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 2915 | count += atomic_read(&link->cg->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2916 | } |
| 2917 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2918 | return count; |
| 2919 | } |
| 2920 | |
| 2921 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2922 | * Advance a list_head iterator. The iterator should be positioned at |
| 2923 | * the start of a css_set |
| 2924 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2925 | static void cgroup_advance_iter(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 2926 | struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2927 | { |
| 2928 | struct list_head *l = it->cg_link; |
| 2929 | struct cg_cgroup_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 2930 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2931 | |
| 2932 | /* Advance to the next non-empty css_set */ |
| 2933 | do { |
| 2934 | l = l->next; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2935 | if (l == &cgrp->css_sets) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2936 | it->cg_link = NULL; |
| 2937 | return; |
| 2938 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2939 | link = list_entry(l, struct cg_cgroup_link, cgrp_link_list); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 2940 | cset = link->cg; |
| 2941 | } while (list_empty(&cset->tasks)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2942 | it->cg_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 2943 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2946 | /* |
| 2947 | * To reduce the fork() overhead for systems that are not actually |
| 2948 | * using their cgroups capability, we don't maintain the lists running |
| 2949 | * through each css_set to its tasks until we see the list actually |
| 2950 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2951 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2952 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2953 | { |
| 2954 | struct task_struct *p, *g; |
| 2955 | write_lock(&css_set_lock); |
| 2956 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2957 | /* |
| 2958 | * We need tasklist_lock because RCU is not safe against |
| 2959 | * while_each_thread(). Besides, a forking task that has passed |
| 2960 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2961 | * is not guaranteed to have its child immediately visible in the |
| 2962 | * tasklist if we walk through it with RCU. |
| 2963 | */ |
| 2964 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2965 | do_each_thread(g, p) { |
| 2966 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2967 | /* |
| 2968 | * We should check if the process is exiting, otherwise |
| 2969 | * it will race with cgroup_exit() in that the list |
| 2970 | * entry won't be deleted though the process has exited. |
| 2971 | */ |
| 2972 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2973 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2974 | task_unlock(p); |
| 2975 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2976 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2977 | write_unlock(&css_set_lock); |
| 2978 | } |
| 2979 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2980 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2981 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 2982 | * @pos: the current cgroup |
| 2983 | * |
| 2984 | * This function returns the next sibling of @pos and should be called |
| 2985 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 2986 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 2987 | * state. |
| 2988 | */ |
| 2989 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 2990 | { |
| 2991 | struct cgroup *next; |
| 2992 | |
| 2993 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 2994 | |
| 2995 | /* |
| 2996 | * @pos could already have been removed. Once a cgroup is removed, |
| 2997 | * its ->sibling.next is no longer updated when its next sibling |
| 2998 | * changes. As CGRP_REMOVED is set on removal which is fully |
| 2999 | * serialized, if we see it unasserted, it's guaranteed that the |
| 3000 | * next sibling hasn't finished its grace period even if it's |
| 3001 | * already removed, and thus safe to dereference from this RCU |
| 3002 | * critical section. If ->sibling.next is inaccessible, |
| 3003 | * cgroup_is_removed() is guaranteed to be visible as %true here. |
| 3004 | */ |
| 3005 | if (likely(!cgroup_is_removed(pos))) { |
| 3006 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3007 | if (&next->sibling != &pos->parent->children) |
| 3008 | return next; |
| 3009 | return NULL; |
| 3010 | } |
| 3011 | |
| 3012 | /* |
| 3013 | * Can't dereference the next pointer. Each cgroup is given a |
| 3014 | * monotonically increasing unique serial number and always |
| 3015 | * appended to the sibling list, so the next one can be found by |
| 3016 | * walking the parent's children until we see a cgroup with higher |
| 3017 | * serial number than @pos's. |
| 3018 | * |
| 3019 | * While this path can be slow, it's taken only when either the |
| 3020 | * current cgroup is removed or iteration and removal race. |
| 3021 | */ |
| 3022 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3023 | if (next->serial_nr > pos->serial_nr) |
| 3024 | return next; |
| 3025 | return NULL; |
| 3026 | } |
| 3027 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3028 | |
| 3029 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3030 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3031 | * @pos: the current position (%NULL to initiate traversal) |
| 3032 | * @cgroup: cgroup whose descendants to walk |
| 3033 | * |
| 3034 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3035 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3036 | * |
| 3037 | * While this function requires RCU read locking, it doesn't require the |
| 3038 | * whole traversal to be contained in a single RCU critical section. This |
| 3039 | * function will return the correct next descendant as long as both @pos |
| 3040 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3041 | */ |
| 3042 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3043 | struct cgroup *cgroup) |
| 3044 | { |
| 3045 | struct cgroup *next; |
| 3046 | |
| 3047 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3048 | |
| 3049 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3050 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3051 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3052 | |
| 3053 | /* visit the first child if exists */ |
| 3054 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3055 | if (next) |
| 3056 | return next; |
| 3057 | |
| 3058 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3059 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3060 | next = cgroup_next_sibling(pos); |
| 3061 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3062 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3063 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3064 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3065 | |
| 3066 | return NULL; |
| 3067 | } |
| 3068 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3069 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3070 | /** |
| 3071 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3072 | * @pos: cgroup of interest |
| 3073 | * |
| 3074 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3075 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3076 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3077 | * |
| 3078 | * While this function requires RCU read locking, it doesn't require the |
| 3079 | * whole traversal to be contained in a single RCU critical section. This |
| 3080 | * function will return the correct rightmost descendant as long as @pos is |
| 3081 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3082 | */ |
| 3083 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3084 | { |
| 3085 | struct cgroup *last, *tmp; |
| 3086 | |
| 3087 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3088 | |
| 3089 | do { |
| 3090 | last = pos; |
| 3091 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3092 | pos = NULL; |
| 3093 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3094 | pos = tmp; |
| 3095 | } while (pos); |
| 3096 | |
| 3097 | return last; |
| 3098 | } |
| 3099 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3100 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3101 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3102 | { |
| 3103 | struct cgroup *last; |
| 3104 | |
| 3105 | do { |
| 3106 | last = pos; |
| 3107 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3108 | sibling); |
| 3109 | } while (pos); |
| 3110 | |
| 3111 | return last; |
| 3112 | } |
| 3113 | |
| 3114 | /** |
| 3115 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3116 | * @pos: the current position (%NULL to initiate traversal) |
| 3117 | * @cgroup: cgroup whose descendants to walk |
| 3118 | * |
| 3119 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3120 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3121 | * |
| 3122 | * While this function requires RCU read locking, it doesn't require the |
| 3123 | * whole traversal to be contained in a single RCU critical section. This |
| 3124 | * function will return the correct next descendant as long as both @pos |
| 3125 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3126 | */ |
| 3127 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3128 | struct cgroup *cgroup) |
| 3129 | { |
| 3130 | struct cgroup *next; |
| 3131 | |
| 3132 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3133 | |
| 3134 | /* if first iteration, visit the leftmost descendant */ |
| 3135 | if (!pos) { |
| 3136 | next = cgroup_leftmost_descendant(cgroup); |
| 3137 | return next != cgroup ? next : NULL; |
| 3138 | } |
| 3139 | |
| 3140 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3141 | next = cgroup_next_sibling(pos); |
| 3142 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3143 | return cgroup_leftmost_descendant(next); |
| 3144 | |
| 3145 | /* no sibling left, visit parent */ |
| 3146 | next = pos->parent; |
| 3147 | return next != cgroup ? next : NULL; |
| 3148 | } |
| 3149 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3150 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3151 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3152 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3153 | { |
| 3154 | /* |
| 3155 | * The first time anyone tries to iterate across a cgroup, |
| 3156 | * we need to enable the list linking each css_set to its |
| 3157 | * tasks, and fix up all existing tasks. |
| 3158 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3159 | if (!use_task_css_set_links) |
| 3160 | cgroup_enable_task_cg_lists(); |
| 3161 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3162 | read_lock(&css_set_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3163 | it->cg_link = &cgrp->css_sets; |
| 3164 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3167 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3168 | struct cgroup_iter *it) |
| 3169 | { |
| 3170 | struct task_struct *res; |
| 3171 | struct list_head *l = it->task; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3172 | struct cg_cgroup_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3173 | |
| 3174 | /* If the iterator cg is NULL, we have no tasks */ |
| 3175 | if (!it->cg_link) |
| 3176 | return NULL; |
| 3177 | res = list_entry(l, struct task_struct, cg_list); |
| 3178 | /* Advance iterator to find next entry */ |
| 3179 | l = l->next; |
Lai Jiangshan | 2019f63 | 2009-01-07 18:07:36 -0800 | [diff] [blame] | 3180 | link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list); |
| 3181 | if (l == &link->cg->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3182 | /* We reached the end of this task list - move on to |
| 3183 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3184 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3185 | } else { |
| 3186 | it->task = l; |
| 3187 | } |
| 3188 | return res; |
| 3189 | } |
| 3190 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3191 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3192 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3193 | { |
| 3194 | read_unlock(&css_set_lock); |
| 3195 | } |
| 3196 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3197 | static inline int started_after_time(struct task_struct *t1, |
| 3198 | struct timespec *time, |
| 3199 | struct task_struct *t2) |
| 3200 | { |
| 3201 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3202 | if (start_diff > 0) { |
| 3203 | return 1; |
| 3204 | } else if (start_diff < 0) { |
| 3205 | return 0; |
| 3206 | } else { |
| 3207 | /* |
| 3208 | * Arbitrarily, if two processes started at the same |
| 3209 | * time, we'll say that the lower pointer value |
| 3210 | * started first. Note that t2 may have exited by now |
| 3211 | * so this may not be a valid pointer any longer, but |
| 3212 | * that's fine - it still serves to distinguish |
| 3213 | * between two tasks started (effectively) simultaneously. |
| 3214 | */ |
| 3215 | return t1 > t2; |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * This function is a callback from heap_insert() and is used to order |
| 3221 | * the heap. |
| 3222 | * In this case we order the heap in descending task start time. |
| 3223 | */ |
| 3224 | static inline int started_after(void *p1, void *p2) |
| 3225 | { |
| 3226 | struct task_struct *t1 = p1; |
| 3227 | struct task_struct *t2 = p2; |
| 3228 | return started_after_time(t1, &t2->start_time, t2); |
| 3229 | } |
| 3230 | |
| 3231 | /** |
| 3232 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3233 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3234 | * |
| 3235 | * Arguments include pointers to callback functions test_task() and |
| 3236 | * process_task(). |
| 3237 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3238 | * and if it returns true, call process_task() for it also. |
| 3239 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3240 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3241 | * but does not lock css_set_lock for the call to process_task(). |
| 3242 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3243 | * creation. |
| 3244 | * It is guaranteed that process_task() will act on every task that |
| 3245 | * is a member of the cgroup for the duration of this call. This |
| 3246 | * function may or may not call process_task() for tasks that exit |
| 3247 | * or move to a different cgroup during the call, or are forked or |
| 3248 | * move into the cgroup during the call. |
| 3249 | * |
| 3250 | * Note that test_task() may be called with locks held, and may in some |
| 3251 | * situations be called multiple times for the same task, so it should |
| 3252 | * be cheap. |
| 3253 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3254 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3255 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3256 | * may cause this function to fail). |
| 3257 | */ |
| 3258 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3259 | { |
| 3260 | int retval, i; |
| 3261 | struct cgroup_iter it; |
| 3262 | struct task_struct *p, *dropped; |
| 3263 | /* Never dereference latest_task, since it's not refcounted */ |
| 3264 | struct task_struct *latest_task = NULL; |
| 3265 | struct ptr_heap tmp_heap; |
| 3266 | struct ptr_heap *heap; |
| 3267 | struct timespec latest_time = { 0, 0 }; |
| 3268 | |
| 3269 | if (scan->heap) { |
| 3270 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3271 | heap = scan->heap; |
| 3272 | heap->gt = &started_after; |
| 3273 | } else { |
| 3274 | /* We need to allocate our own heap memory */ |
| 3275 | heap = &tmp_heap; |
| 3276 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3277 | if (retval) |
| 3278 | /* cannot allocate the heap */ |
| 3279 | return retval; |
| 3280 | } |
| 3281 | |
| 3282 | again: |
| 3283 | /* |
| 3284 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3285 | * to determine which are of interest, and using the scanner's |
| 3286 | * "process_task" callback to process any of them that need an update. |
| 3287 | * Since we don't want to hold any locks during the task updates, |
| 3288 | * gather tasks to be processed in a heap structure. |
| 3289 | * The heap is sorted by descending task start time. |
| 3290 | * If the statically-sized heap fills up, we overflow tasks that |
| 3291 | * started later, and in future iterations only consider tasks that |
| 3292 | * started after the latest task in the previous pass. This |
| 3293 | * guarantees forward progress and that we don't miss any tasks. |
| 3294 | */ |
| 3295 | heap->size = 0; |
| 3296 | cgroup_iter_start(scan->cg, &it); |
| 3297 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3298 | /* |
| 3299 | * Only affect tasks that qualify per the caller's callback, |
| 3300 | * if he provided one |
| 3301 | */ |
| 3302 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3303 | continue; |
| 3304 | /* |
| 3305 | * Only process tasks that started after the last task |
| 3306 | * we processed |
| 3307 | */ |
| 3308 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3309 | continue; |
| 3310 | dropped = heap_insert(heap, p); |
| 3311 | if (dropped == NULL) { |
| 3312 | /* |
| 3313 | * The new task was inserted; the heap wasn't |
| 3314 | * previously full |
| 3315 | */ |
| 3316 | get_task_struct(p); |
| 3317 | } else if (dropped != p) { |
| 3318 | /* |
| 3319 | * The new task was inserted, and pushed out a |
| 3320 | * different task |
| 3321 | */ |
| 3322 | get_task_struct(p); |
| 3323 | put_task_struct(dropped); |
| 3324 | } |
| 3325 | /* |
| 3326 | * Else the new task was newer than anything already in |
| 3327 | * the heap and wasn't inserted |
| 3328 | */ |
| 3329 | } |
| 3330 | cgroup_iter_end(scan->cg, &it); |
| 3331 | |
| 3332 | if (heap->size) { |
| 3333 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3334 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3335 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3336 | latest_time = q->start_time; |
| 3337 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3338 | } |
| 3339 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3340 | scan->process_task(q, scan); |
| 3341 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3342 | } |
| 3343 | /* |
| 3344 | * If we had to process any tasks at all, scan again |
| 3345 | * in case some of them were in the middle of forking |
| 3346 | * children that didn't get processed. |
| 3347 | * Not the most efficient way to do it, but it avoids |
| 3348 | * having to take callback_mutex in the fork path |
| 3349 | */ |
| 3350 | goto again; |
| 3351 | } |
| 3352 | if (heap == &tmp_heap) |
| 3353 | heap_free(&tmp_heap); |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3357 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3358 | struct cgroup_scanner *scan) |
| 3359 | { |
| 3360 | struct cgroup *new_cgroup = scan->data; |
| 3361 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3362 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3363 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3364 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | /** |
| 3368 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3369 | * @to: cgroup to which the tasks will be moved |
| 3370 | * @from: cgroup in which the tasks currently reside |
| 3371 | */ |
| 3372 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3373 | { |
| 3374 | struct cgroup_scanner scan; |
| 3375 | |
| 3376 | scan.cg = from; |
| 3377 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3378 | scan.process_task = cgroup_transfer_one_task; |
| 3379 | scan.heap = NULL; |
| 3380 | scan.data = to; |
| 3381 | |
| 3382 | return cgroup_scan_tasks(&scan); |
| 3383 | } |
| 3384 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3385 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3386 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3387 | * |
| 3388 | * Reading this file can return large amounts of data if a cgroup has |
| 3389 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3390 | * but we cannot guarantee that the information we produce is correct |
| 3391 | * unless we produce it entirely atomically. |
| 3392 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3393 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3394 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3395 | /* which pidlist file are we talking about? */ |
| 3396 | enum cgroup_filetype { |
| 3397 | CGROUP_FILE_PROCS, |
| 3398 | CGROUP_FILE_TASKS, |
| 3399 | }; |
| 3400 | |
| 3401 | /* |
| 3402 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3403 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3404 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3405 | * to the cgroup. |
| 3406 | */ |
| 3407 | struct cgroup_pidlist { |
| 3408 | /* |
| 3409 | * used to find which pidlist is wanted. doesn't change as long as |
| 3410 | * this particular list stays in the list. |
| 3411 | */ |
| 3412 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3413 | /* array of xids */ |
| 3414 | pid_t *list; |
| 3415 | /* how many elements the above list has */ |
| 3416 | int length; |
| 3417 | /* how many files are using the current array */ |
| 3418 | int use_count; |
| 3419 | /* each of these stored in a list by its cgroup */ |
| 3420 | struct list_head links; |
| 3421 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3422 | struct cgroup *owner; |
| 3423 | /* protects the other fields */ |
| 3424 | struct rw_semaphore mutex; |
| 3425 | }; |
| 3426 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3427 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3428 | * The following two functions "fix" the issue where there are more pids |
| 3429 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3430 | * TODO: replace with a kernel-wide solution to this problem |
| 3431 | */ |
| 3432 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3433 | static void *pidlist_allocate(int count) |
| 3434 | { |
| 3435 | if (PIDLIST_TOO_LARGE(count)) |
| 3436 | return vmalloc(count * sizeof(pid_t)); |
| 3437 | else |
| 3438 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3439 | } |
| 3440 | static void pidlist_free(void *p) |
| 3441 | { |
| 3442 | if (is_vmalloc_addr(p)) |
| 3443 | vfree(p); |
| 3444 | else |
| 3445 | kfree(p); |
| 3446 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3447 | |
| 3448 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3449 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3450 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3451 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3452 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3453 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3454 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3455 | |
| 3456 | /* |
| 3457 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3458 | * edge cases first; no work needs to be done for either |
| 3459 | */ |
| 3460 | if (length == 0 || length == 1) |
| 3461 | return length; |
| 3462 | /* src and dest walk down the list; dest counts unique elements */ |
| 3463 | for (src = 1; src < length; src++) { |
| 3464 | /* find next unique element */ |
| 3465 | while (list[src] == list[src-1]) { |
| 3466 | src++; |
| 3467 | if (src == length) |
| 3468 | goto after; |
| 3469 | } |
| 3470 | /* dest always points to where the next unique element goes */ |
| 3471 | list[dest] = list[src]; |
| 3472 | dest++; |
| 3473 | } |
| 3474 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3475 | return dest; |
| 3476 | } |
| 3477 | |
| 3478 | static int cmppid(const void *a, const void *b) |
| 3479 | { |
| 3480 | return *(pid_t *)a - *(pid_t *)b; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3484 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3485 | * returns with the lock on that pidlist already held, and takes care |
| 3486 | * of the use count, or returns NULL with no locks held if we're out of |
| 3487 | * memory. |
| 3488 | */ |
| 3489 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3490 | enum cgroup_filetype type) |
| 3491 | { |
| 3492 | struct cgroup_pidlist *l; |
| 3493 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3494 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3495 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3496 | /* |
| 3497 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3498 | * the last ref-holder is trying to remove l from the list at the same |
| 3499 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3500 | * list we find out from under us - compare release_pid_array(). |
| 3501 | */ |
| 3502 | mutex_lock(&cgrp->pidlist_mutex); |
| 3503 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3504 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3505 | /* make sure l doesn't vanish out from under us */ |
| 3506 | down_write(&l->mutex); |
| 3507 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3508 | return l; |
| 3509 | } |
| 3510 | } |
| 3511 | /* entry not found; create a new one */ |
| 3512 | l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
| 3513 | if (!l) { |
| 3514 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3515 | return l; |
| 3516 | } |
| 3517 | init_rwsem(&l->mutex); |
| 3518 | down_write(&l->mutex); |
| 3519 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3520 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3521 | l->use_count = 0; /* don't increment here */ |
| 3522 | l->list = NULL; |
| 3523 | l->owner = cgrp; |
| 3524 | list_add(&l->links, &cgrp->pidlists); |
| 3525 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3526 | return l; |
| 3527 | } |
| 3528 | |
| 3529 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3530 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3531 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3532 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3533 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3534 | { |
| 3535 | pid_t *array; |
| 3536 | int length; |
| 3537 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3538 | struct cgroup_iter it; |
| 3539 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3540 | struct cgroup_pidlist *l; |
| 3541 | |
| 3542 | /* |
| 3543 | * If cgroup gets more users after we read count, we won't have |
| 3544 | * enough space - tough. This race is indistinguishable to the |
| 3545 | * caller from the case that the additional cgroup users didn't |
| 3546 | * show up until sometime later on. |
| 3547 | */ |
| 3548 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3549 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3550 | if (!array) |
| 3551 | return -ENOMEM; |
| 3552 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3553 | cgroup_iter_start(cgrp, &it); |
| 3554 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3555 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3556 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3557 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3558 | if (type == CGROUP_FILE_PROCS) |
| 3559 | pid = task_tgid_vnr(tsk); |
| 3560 | else |
| 3561 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3562 | if (pid > 0) /* make sure to only use valid results */ |
| 3563 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3564 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3565 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3566 | length = n; |
| 3567 | /* now sort & (if procs) strip out duplicates */ |
| 3568 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3569 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3570 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3571 | l = cgroup_pidlist_find(cgrp, type); |
| 3572 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3573 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3574 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3576 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3577 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3578 | l->list = array; |
| 3579 | l->length = length; |
| 3580 | l->use_count++; |
| 3581 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3582 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3583 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3586 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3587 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3588 | * @stats: cgroupstats to fill information into |
| 3589 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3590 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3591 | * |
| 3592 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3593 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3594 | */ |
| 3595 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3596 | { |
| 3597 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3598 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3599 | struct cgroup_iter it; |
| 3600 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3601 | |
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 | * Validate dentry by checking the superblock operations, |
| 3604 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3605 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3606 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3607 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3608 | goto err; |
| 3609 | |
| 3610 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3611 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3612 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3613 | cgroup_iter_start(cgrp, &it); |
| 3614 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3615 | switch (tsk->state) { |
| 3616 | case TASK_RUNNING: |
| 3617 | stats->nr_running++; |
| 3618 | break; |
| 3619 | case TASK_INTERRUPTIBLE: |
| 3620 | stats->nr_sleeping++; |
| 3621 | break; |
| 3622 | case TASK_UNINTERRUPTIBLE: |
| 3623 | stats->nr_uninterruptible++; |
| 3624 | break; |
| 3625 | case TASK_STOPPED: |
| 3626 | stats->nr_stopped++; |
| 3627 | break; |
| 3628 | default: |
| 3629 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3630 | stats->nr_io_wait++; |
| 3631 | break; |
| 3632 | } |
| 3633 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3634 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3635 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3636 | err: |
| 3637 | return ret; |
| 3638 | } |
| 3639 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3640 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3641 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3642 | * 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] | 3643 | * 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] | 3644 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3645 | */ |
| 3646 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3648 | { |
| 3649 | /* |
| 3650 | * Initially we receive a position value that corresponds to |
| 3651 | * one more than the last pid shown (or 0 on the first call or |
| 3652 | * after a seek to the start). Use a binary-search to find the |
| 3653 | * next pid to display, if any |
| 3654 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3655 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3656 | int index = 0, pid = *pos; |
| 3657 | int *iter; |
| 3658 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3659 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3660 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3661 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3662 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | while (index < end) { |
| 3664 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3666 | index = mid; |
| 3667 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3668 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3669 | index = mid + 1; |
| 3670 | else |
| 3671 | end = mid; |
| 3672 | } |
| 3673 | } |
| 3674 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | return NULL; |
| 3677 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3678 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3679 | *pos = *iter; |
| 3680 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3683 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3684 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3685 | struct cgroup_pidlist *l = s->private; |
| 3686 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3689 | 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] | 3690 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3691 | struct cgroup_pidlist *l = s->private; |
| 3692 | pid_t *p = v; |
| 3693 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3694 | /* |
| 3695 | * Advance to the next pid in the array. If this goes off the |
| 3696 | * end, we're done |
| 3697 | */ |
| 3698 | p++; |
| 3699 | if (p >= end) { |
| 3700 | return NULL; |
| 3701 | } else { |
| 3702 | *pos = *p; |
| 3703 | return p; |
| 3704 | } |
| 3705 | } |
| 3706 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3707 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3708 | { |
| 3709 | return seq_printf(s, "%d\n", *(int *)v); |
| 3710 | } |
| 3711 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3712 | /* |
| 3713 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3714 | * independent of whether it's tasks or procs |
| 3715 | */ |
| 3716 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3717 | .start = cgroup_pidlist_start, |
| 3718 | .stop = cgroup_pidlist_stop, |
| 3719 | .next = cgroup_pidlist_next, |
| 3720 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3721 | }; |
| 3722 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3723 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3724 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3725 | /* |
| 3726 | * the case where we're the last user of this particular pidlist will |
| 3727 | * have us remove it from the cgroup's list, which entails taking the |
| 3728 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3729 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3730 | */ |
| 3731 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3732 | down_write(&l->mutex); |
| 3733 | BUG_ON(!l->use_count); |
| 3734 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3735 | /* we're the last user if refcount is 0; remove and free */ |
| 3736 | list_del(&l->links); |
| 3737 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3738 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3739 | put_pid_ns(l->key.ns); |
| 3740 | up_write(&l->mutex); |
| 3741 | kfree(l); |
| 3742 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3743 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3744 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3745 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3748 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3749 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3750 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3751 | if (!(file->f_mode & FMODE_READ)) |
| 3752 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3753 | /* |
| 3754 | * the seq_file will only be initialized if the file was opened for |
| 3755 | * reading; hence we check if it's not null only in that case. |
| 3756 | */ |
| 3757 | l = ((struct seq_file *)file->private_data)->private; |
| 3758 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3759 | return seq_release(inode, file); |
| 3760 | } |
| 3761 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3762 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3763 | .read = seq_read, |
| 3764 | .llseek = seq_lseek, |
| 3765 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3766 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3767 | }; |
| 3768 | |
| 3769 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3770 | * The following functions handle opens on a file that displays a pidlist |
| 3771 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3772 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3773 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3774 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3775 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3776 | { |
| 3777 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3778 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | int retval; |
| 3780 | |
| 3781 | /* Nothing to do for write-only files */ |
| 3782 | if (!(file->f_mode & FMODE_READ)) |
| 3783 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3784 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3785 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3786 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3787 | if (retval) |
| 3788 | return retval; |
| 3789 | /* configure file information */ |
| 3790 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3791 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3792 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3793 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3795 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3796 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3797 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3798 | return 0; |
| 3799 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3800 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3801 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3802 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3803 | } |
| 3804 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3805 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3806 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3807 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3808 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3809 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3810 | struct cftype *cft) |
| 3811 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3812 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3815 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3816 | struct cftype *cft, |
| 3817 | u64 val) |
| 3818 | { |
| 3819 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3820 | if (val) |
| 3821 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3822 | else |
| 3823 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3824 | return 0; |
| 3825 | } |
| 3826 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3827 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3828 | * Unregister event and free resources. |
| 3829 | * |
| 3830 | * Gets called from workqueue. |
| 3831 | */ |
| 3832 | static void cgroup_event_remove(struct work_struct *work) |
| 3833 | { |
| 3834 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3835 | remove); |
| 3836 | struct cgroup *cgrp = event->cgrp; |
| 3837 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3838 | remove_wait_queue(event->wqh, &event->wait); |
| 3839 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3840 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3841 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3842 | /* Notify userspace the event is going away. */ |
| 3843 | eventfd_signal(event->eventfd, 1); |
| 3844 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3845 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3846 | kfree(event); |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3847 | dput(cgrp->dentry); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | /* |
| 3851 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3852 | * |
| 3853 | * Called with wqh->lock held and interrupts disabled. |
| 3854 | */ |
| 3855 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3856 | int sync, void *key) |
| 3857 | { |
| 3858 | struct cgroup_event *event = container_of(wait, |
| 3859 | struct cgroup_event, wait); |
| 3860 | struct cgroup *cgrp = event->cgrp; |
| 3861 | unsigned long flags = (unsigned long)key; |
| 3862 | |
| 3863 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3864 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3865 | * If the event has been detached at cgroup removal, we |
| 3866 | * can simply return knowing the other side will cleanup |
| 3867 | * for us. |
| 3868 | * |
| 3869 | * We can't race against event freeing since the other |
| 3870 | * side will require wqh->lock via remove_wait_queue(), |
| 3871 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3872 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3873 | spin_lock(&cgrp->event_list_lock); |
| 3874 | if (!list_empty(&event->list)) { |
| 3875 | list_del_init(&event->list); |
| 3876 | /* |
| 3877 | * We are in atomic context, but cgroup_event_remove() |
| 3878 | * may sleep, so we have to call it in workqueue. |
| 3879 | */ |
| 3880 | schedule_work(&event->remove); |
| 3881 | } |
| 3882 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
| 3888 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3889 | wait_queue_head_t *wqh, poll_table *pt) |
| 3890 | { |
| 3891 | struct cgroup_event *event = container_of(pt, |
| 3892 | struct cgroup_event, pt); |
| 3893 | |
| 3894 | event->wqh = wqh; |
| 3895 | add_wait_queue(wqh, &event->wait); |
| 3896 | } |
| 3897 | |
| 3898 | /* |
| 3899 | * Parse input and register new cgroup event handler. |
| 3900 | * |
| 3901 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3902 | * Interpretation of args is defined by control file implementation. |
| 3903 | */ |
| 3904 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3905 | const char *buffer) |
| 3906 | { |
| 3907 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3908 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3909 | unsigned int efd, cfd; |
| 3910 | struct file *efile = NULL; |
| 3911 | struct file *cfile = NULL; |
| 3912 | char *endp; |
| 3913 | int ret; |
| 3914 | |
| 3915 | efd = simple_strtoul(buffer, &endp, 10); |
| 3916 | if (*endp != ' ') |
| 3917 | return -EINVAL; |
| 3918 | buffer = endp + 1; |
| 3919 | |
| 3920 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3921 | if ((*endp != ' ') && (*endp != '\0')) |
| 3922 | return -EINVAL; |
| 3923 | buffer = endp + 1; |
| 3924 | |
| 3925 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3926 | if (!event) |
| 3927 | return -ENOMEM; |
| 3928 | event->cgrp = cgrp; |
| 3929 | INIT_LIST_HEAD(&event->list); |
| 3930 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3931 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3932 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3933 | |
| 3934 | efile = eventfd_fget(efd); |
| 3935 | if (IS_ERR(efile)) { |
| 3936 | ret = PTR_ERR(efile); |
| 3937 | goto fail; |
| 3938 | } |
| 3939 | |
| 3940 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3941 | if (IS_ERR(event->eventfd)) { |
| 3942 | ret = PTR_ERR(event->eventfd); |
| 3943 | goto fail; |
| 3944 | } |
| 3945 | |
| 3946 | cfile = fget(cfd); |
| 3947 | if (!cfile) { |
| 3948 | ret = -EBADF; |
| 3949 | goto fail; |
| 3950 | } |
| 3951 | |
| 3952 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3953 | /* 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] | 3954 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3955 | if (ret < 0) |
| 3956 | goto fail; |
| 3957 | |
| 3958 | event->cft = __file_cft(cfile); |
| 3959 | if (IS_ERR(event->cft)) { |
| 3960 | ret = PTR_ERR(event->cft); |
| 3961 | goto fail; |
| 3962 | } |
| 3963 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3964 | /* |
| 3965 | * The file to be monitored must be in the same cgroup as |
| 3966 | * cgroup.event_control is. |
| 3967 | */ |
| 3968 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3969 | if (cgrp_cfile != cgrp) { |
| 3970 | ret = -EINVAL; |
| 3971 | goto fail; |
| 3972 | } |
| 3973 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3974 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3975 | ret = -EINVAL; |
| 3976 | goto fail; |
| 3977 | } |
| 3978 | |
| 3979 | ret = event->cft->register_event(cgrp, event->cft, |
| 3980 | event->eventfd, buffer); |
| 3981 | if (ret) |
| 3982 | goto fail; |
| 3983 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 3984 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3985 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 3986 | /* |
| 3987 | * Events should be removed after rmdir of cgroup directory, but before |
| 3988 | * destroying subsystem state objects. Let's take reference to cgroup |
| 3989 | * directory dentry to do that. |
| 3990 | */ |
| 3991 | dget(cgrp->dentry); |
| 3992 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3993 | spin_lock(&cgrp->event_list_lock); |
| 3994 | list_add(&event->list, &cgrp->event_list); |
| 3995 | spin_unlock(&cgrp->event_list_lock); |
| 3996 | |
| 3997 | fput(cfile); |
| 3998 | fput(efile); |
| 3999 | |
| 4000 | return 0; |
| 4001 | |
| 4002 | fail: |
| 4003 | if (cfile) |
| 4004 | fput(cfile); |
| 4005 | |
| 4006 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4007 | eventfd_ctx_put(event->eventfd); |
| 4008 | |
| 4009 | if (!IS_ERR_OR_NULL(efile)) |
| 4010 | fput(efile); |
| 4011 | |
| 4012 | kfree(event); |
| 4013 | |
| 4014 | return ret; |
| 4015 | } |
| 4016 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4017 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4018 | struct cftype *cft) |
| 4019 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4020 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4024 | struct cftype *cft, |
| 4025 | u64 val) |
| 4026 | { |
| 4027 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4028 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4029 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4030 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4031 | return 0; |
| 4032 | } |
| 4033 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4034 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4035 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4036 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4037 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4038 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4039 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4040 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4041 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4042 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4043 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4044 | .write_string = cgroup_write_event_control, |
| 4045 | .mode = S_IWUGO, |
| 4046 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4047 | { |
| 4048 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4049 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4050 | .read_u64 = cgroup_clone_children_read, |
| 4051 | .write_u64 = cgroup_clone_children_write, |
| 4052 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4053 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4054 | .name = "cgroup.sane_behavior", |
| 4055 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4056 | .read_seq_string = cgroup_sane_behavior_show, |
| 4057 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4058 | |
| 4059 | /* |
| 4060 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4061 | * don't exist if sane_behavior. If you're depending on these, be |
| 4062 | * prepared to be burned. |
| 4063 | */ |
| 4064 | { |
| 4065 | .name = "tasks", |
| 4066 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4067 | .open = cgroup_tasks_open, |
| 4068 | .write_u64 = cgroup_tasks_write, |
| 4069 | .release = cgroup_pidlist_release, |
| 4070 | .mode = S_IRUGO | S_IWUSR, |
| 4071 | }, |
| 4072 | { |
| 4073 | .name = "notify_on_release", |
| 4074 | .flags = CFTYPE_INSANE, |
| 4075 | .read_u64 = cgroup_read_notify_on_release, |
| 4076 | .write_u64 = cgroup_write_notify_on_release, |
| 4077 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4078 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4079 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4080 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4081 | .read_seq_string = cgroup_release_agent_show, |
| 4082 | .write_string = cgroup_release_agent_write, |
| 4083 | .max_write_len = PATH_MAX, |
| 4084 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4085 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4086 | }; |
| 4087 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4088 | /** |
| 4089 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4090 | * @cgrp: target cgroup |
| 4091 | * @base_files: true if the base files should be added |
| 4092 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4093 | */ |
| 4094 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4095 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4096 | { |
| 4097 | int err; |
| 4098 | struct cgroup_subsys *ss; |
| 4099 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4100 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4101 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4102 | if (err < 0) |
| 4103 | return err; |
| 4104 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4105 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4106 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4107 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4108 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4109 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4110 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4111 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4112 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4113 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4114 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4115 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4116 | /* This cgroup is ready now */ |
| 4117 | for_each_subsys(cgrp->root, ss) { |
| 4118 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4119 | /* |
| 4120 | * Update id->css pointer and make this css visible from |
| 4121 | * CSS ID functions. This pointer will be dereferened |
| 4122 | * from RCU-read-side without locks. |
| 4123 | */ |
| 4124 | if (css->id) |
| 4125 | rcu_assign_pointer(css->id->css, css); |
| 4126 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4127 | |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4131 | static void css_dput_fn(struct work_struct *work) |
| 4132 | { |
| 4133 | struct cgroup_subsys_state *css = |
| 4134 | container_of(work, struct cgroup_subsys_state, dput_work); |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4135 | struct dentry *dentry = css->cgroup->dentry; |
| 4136 | struct super_block *sb = dentry->d_sb; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4137 | |
Tejun Heo | 5db9a4d | 2012-07-07 16:08:18 -0700 | [diff] [blame] | 4138 | atomic_inc(&sb->s_active); |
| 4139 | dput(dentry); |
| 4140 | deactivate_super(sb); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4141 | } |
| 4142 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4143 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4144 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4145 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4146 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4147 | css->cgroup = cgrp; |
Paul Menage | e7c5ec9 | 2009-01-07 18:08:38 -0800 | [diff] [blame] | 4148 | atomic_set(&css->refcnt, 1); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4149 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4150 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4151 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4152 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4153 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4154 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4155 | |
| 4156 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4157 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4158 | * css_put(). dput() requires process context, which css_put() may |
| 4159 | * be called without. @css->dput_work will be used to invoke |
| 4160 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4161 | */ |
| 4162 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4163 | } |
| 4164 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4165 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4166 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4167 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4168 | int ret = 0; |
| 4169 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4170 | lockdep_assert_held(&cgroup_mutex); |
| 4171 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4172 | if (ss->css_online) |
| 4173 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4174 | if (!ret) |
| 4175 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4176 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4180 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4181 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4182 | { |
| 4183 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4184 | |
| 4185 | lockdep_assert_held(&cgroup_mutex); |
| 4186 | |
| 4187 | if (!(css->flags & CSS_ONLINE)) |
| 4188 | return; |
| 4189 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4190 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4191 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4192 | |
| 4193 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4194 | } |
| 4195 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4196 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4197 | * cgroup_create - create a cgroup |
| 4198 | * @parent: cgroup that will be parent of the new cgroup |
| 4199 | * @dentry: dentry of the new cgroup |
| 4200 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4201 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4202 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4203 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4204 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4205 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4206 | { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4207 | static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4208 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4209 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4210 | struct cgroupfs_root *root = parent->root; |
| 4211 | int err = 0; |
| 4212 | struct cgroup_subsys *ss; |
| 4213 | struct super_block *sb = root->sb; |
| 4214 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4215 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4216 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4217 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4218 | return -ENOMEM; |
| 4219 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4220 | name = cgroup_alloc_name(dentry); |
| 4221 | if (!name) |
| 4222 | goto err_free_cgrp; |
| 4223 | rcu_assign_pointer(cgrp->name, name); |
| 4224 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4225 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4226 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4227 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4228 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4229 | /* |
| 4230 | * Only live parents can have children. Note that the liveliness |
| 4231 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4232 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4233 | * anyway so that locking is contained inside cgroup proper and we |
| 4234 | * don't get nasty surprises if we ever grow another caller. |
| 4235 | */ |
| 4236 | if (!cgroup_lock_live_group(parent)) { |
| 4237 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4238 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4239 | } |
| 4240 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4241 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4242 | * get deleted on unmount if there are child cgroups. This |
| 4243 | * can be done outside cgroup_mutex, since the sb can't |
| 4244 | * disappear while someone has an open control file on the |
| 4245 | * fs */ |
| 4246 | atomic_inc(&sb->s_active); |
| 4247 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4248 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4249 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4250 | dentry->d_fsdata = cgrp; |
| 4251 | cgrp->dentry = dentry; |
| 4252 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4253 | cgrp->parent = parent; |
| 4254 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4255 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4256 | if (notify_on_release(parent)) |
| 4257 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4258 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4259 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4260 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4261 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4262 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4263 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4264 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4265 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4266 | if (IS_ERR(css)) { |
| 4267 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4268 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4269 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4270 | init_cgroup_css(css, ss, cgrp); |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4271 | if (ss->use_id) { |
| 4272 | err = alloc_css_id(ss, parent, cgrp); |
| 4273 | if (err) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4274 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4275 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4278 | /* |
| 4279 | * Create directory. cgroup_create_file() returns with the new |
| 4280 | * directory locked on success so that it can be populated without |
| 4281 | * dropping cgroup_mutex. |
| 4282 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4283 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4284 | if (err < 0) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4285 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4286 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4287 | |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4288 | /* |
| 4289 | * Assign a monotonically increasing serial number. With the list |
| 4290 | * appending below, it guarantees that sibling cgroups are always |
| 4291 | * sorted in the ascending serial number order on the parent's |
| 4292 | * ->children. |
| 4293 | */ |
| 4294 | cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor); |
| 4295 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4296 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4297 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4298 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4299 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4300 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4301 | /* each css holds a ref to the cgroup's dentry */ |
| 4302 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4303 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4304 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4305 | /* hold a ref to the parent's dentry */ |
| 4306 | dget(parent->dentry); |
| 4307 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4308 | /* creation succeeded, notify subsystems */ |
| 4309 | for_each_subsys(root, ss) { |
| 4310 | err = online_css(ss, cgrp); |
| 4311 | if (err) |
| 4312 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4313 | |
| 4314 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4315 | parent->parent) { |
| 4316 | 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", |
| 4317 | current->comm, current->pid, ss->name); |
| 4318 | if (!strcmp(ss->name, "memory")) |
| 4319 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4320 | ss->warned_broken_hierarchy = true; |
| 4321 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4322 | } |
| 4323 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4324 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4325 | if (err) |
| 4326 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4327 | |
| 4328 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4329 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4330 | |
| 4331 | return 0; |
| 4332 | |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4333 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | for_each_subsys(root, ss) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4335 | if (cgrp->subsys[ss->subsys_id]) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4336 | ss->css_free(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4337 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4338 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4339 | /* Release the reference count that we took on the superblock */ |
| 4340 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4341 | err_free_id: |
| 4342 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4343 | err_free_name: |
| 4344 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4345 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4346 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4347 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4348 | |
| 4349 | err_destroy: |
| 4350 | cgroup_destroy_locked(cgrp); |
| 4351 | mutex_unlock(&cgroup_mutex); |
| 4352 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4353 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4356 | 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] | 4357 | { |
| 4358 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4359 | |
| 4360 | /* the vfs holds inode->i_mutex already */ |
| 4361 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4362 | } |
| 4363 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4364 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4365 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4366 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4367 | struct dentry *d = cgrp->dentry; |
| 4368 | struct cgroup *parent = cgrp->parent; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4369 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4370 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4371 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4372 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4373 | lockdep_assert_held(&cgroup_mutex); |
| 4374 | |
| 4375 | if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4376 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4377 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4378 | /* |
| 4379 | * Block new css_tryget() by deactivating refcnt and mark @cgrp |
| 4380 | * removed. This makes future css_tryget() and child creation |
| 4381 | * attempts fail thus maintaining the removal conditions verified |
| 4382 | * above. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4383 | * |
| 4384 | * Note that CGRP_REMVOED clearing is depended upon by |
| 4385 | * cgroup_next_sibling() to resume iteration after dropping RCU |
| 4386 | * read lock. See cgroup_next_sibling() for details. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4387 | */ |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4388 | for_each_subsys(cgrp->root, ss) { |
| 4389 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4390 | |
| 4391 | WARN_ON(atomic_read(&css->refcnt) < 0); |
| 4392 | atomic_add(CSS_DEACT_BIAS, &css->refcnt); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4393 | } |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4394 | set_bit(CGRP_REMOVED, &cgrp->flags); |
| 4395 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4396 | /* tell subsystems to initate destruction */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4397 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4398 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4399 | |
| 4400 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4401 | * Put all the base refs. Each css holds an extra reference to the |
| 4402 | * cgroup's dentry and cgroup removal proceeds regardless of css |
| 4403 | * refs. On the last put of each css, whenever that may be, the |
| 4404 | * extra dentry ref is put so that dentry destruction happens only |
| 4405 | * after all css's are released. |
| 4406 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4407 | for_each_subsys(cgrp->root, ss) |
| 4408 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4409 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4410 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4411 | if (!list_empty(&cgrp->release_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4412 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 4413 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4414 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4415 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4416 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4417 | list_del_init(&cgrp->allcg_node); |
| 4418 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4419 | dget(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4420 | cgroup_d_remove_dir(d); |
| 4421 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4422 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4423 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4424 | check_for_release(parent); |
| 4425 | |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4426 | /* |
| 4427 | * Unregister events and notify userspace. |
| 4428 | * Notify userspace about cgroup removing only after rmdir of cgroup |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4429 | * directory to avoid race between userspace and kernelspace. |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4430 | */ |
| 4431 | spin_lock(&cgrp->event_list_lock); |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4432 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
Greg Thelen | 9718ceb | 2012-11-28 13:50:45 -0800 | [diff] [blame] | 4433 | list_del_init(&event->list); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4434 | schedule_work(&event->remove); |
| 4435 | } |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4436 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4437 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4438 | return 0; |
| 4439 | } |
| 4440 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4441 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4442 | { |
| 4443 | int ret; |
| 4444 | |
| 4445 | mutex_lock(&cgroup_mutex); |
| 4446 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4447 | mutex_unlock(&cgroup_mutex); |
| 4448 | |
| 4449 | return ret; |
| 4450 | } |
| 4451 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4452 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4453 | { |
| 4454 | INIT_LIST_HEAD(&ss->cftsets); |
| 4455 | |
| 4456 | /* |
| 4457 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4458 | * deregistration. |
| 4459 | */ |
| 4460 | if (ss->base_cftypes) { |
| 4461 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4462 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4463 | } |
| 4464 | } |
| 4465 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4466 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4467 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4468 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4469 | |
| 4470 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4471 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4472 | mutex_lock(&cgroup_mutex); |
| 4473 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4474 | /* init base cftset */ |
| 4475 | cgroup_init_cftsets(ss); |
| 4476 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4477 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4478 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4479 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4480 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4481 | /* We don't handle early failures gracefully */ |
| 4482 | BUG_ON(IS_ERR(css)); |
| 4483 | init_cgroup_css(css, ss, dummytop); |
| 4484 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4485 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4486 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4487 | * newly registered, all tasks and hence the |
| 4488 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4489 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4490 | |
| 4491 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4492 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4493 | /* At system boot, before all subsystems have been |
| 4494 | * registered, no tasks have been forked, so we don't |
| 4495 | * need to invoke fork callbacks here. */ |
| 4496 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4497 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4498 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4499 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4500 | mutex_unlock(&cgroup_mutex); |
| 4501 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4502 | /* this function shouldn't be used with modular subsystems, since they |
| 4503 | * need to register a subsys_id, among other things */ |
| 4504 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4508 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4509 | * @ss: the subsystem to load |
| 4510 | * |
| 4511 | * 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] | 4512 | * 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] | 4513 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4514 | * simpler cgroup_init_subsys. |
| 4515 | */ |
| 4516 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4517 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4518 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4519 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4520 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4521 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4522 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4523 | |
| 4524 | /* check name and function validity */ |
| 4525 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4526 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4527 | return -EINVAL; |
| 4528 | |
| 4529 | /* |
| 4530 | * we don't support callbacks in modular subsystems. this check is |
| 4531 | * before the ss->module check for consistency; a subsystem that could |
| 4532 | * be a module should still have no callbacks even if the user isn't |
| 4533 | * compiling it as one. |
| 4534 | */ |
| 4535 | if (ss->fork || ss->exit) |
| 4536 | return -EINVAL; |
| 4537 | |
| 4538 | /* |
| 4539 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4540 | * since cgroup_init_subsys will have already taken care of it. |
| 4541 | */ |
| 4542 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4543 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4544 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4548 | /* init base cftset */ |
| 4549 | cgroup_init_cftsets(ss); |
| 4550 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4551 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4552 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4553 | |
| 4554 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4555 | * no ss->css_alloc seems to need anything important in the ss |
| 4556 | * struct, so this can happen first (i.e. before the rootnode |
| 4557 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4558 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4559 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4560 | if (IS_ERR(css)) { |
| 4561 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4562 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4563 | mutex_unlock(&cgroup_mutex); |
| 4564 | return PTR_ERR(css); |
| 4565 | } |
| 4566 | |
| 4567 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4568 | ss->root = &rootnode; |
| 4569 | |
| 4570 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4571 | init_cgroup_css(css, ss, dummytop); |
| 4572 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4573 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4574 | ret = cgroup_init_idr(ss, css); |
| 4575 | if (ret) |
| 4576 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | /* |
| 4580 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4581 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4582 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4583 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4584 | * table state, so each changed css_set will need its hash recomputed. |
| 4585 | * this is all done under the css_set_lock. |
| 4586 | */ |
| 4587 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4588 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4589 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4590 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4591 | continue; |
| 4592 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4593 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4594 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4595 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4596 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4597 | key = css_set_hash(cset->subsys); |
| 4598 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4599 | } |
| 4600 | write_unlock(&css_set_lock); |
| 4601 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4602 | ret = online_css(ss, dummytop); |
| 4603 | if (ret) |
| 4604 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4605 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4606 | /* success! */ |
| 4607 | mutex_unlock(&cgroup_mutex); |
| 4608 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4609 | |
| 4610 | err_unload: |
| 4611 | mutex_unlock(&cgroup_mutex); |
| 4612 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4613 | cgroup_unload_subsys(ss); |
| 4614 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4615 | } |
| 4616 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4617 | |
| 4618 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4619 | * cgroup_unload_subsys: unload a modular subsystem |
| 4620 | * @ss: the subsystem to unload |
| 4621 | * |
| 4622 | * This function should be called in a modular subsystem's exitcall. When this |
| 4623 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4624 | * the subsystem will not be attached to any hierarchy. |
| 4625 | */ |
| 4626 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4627 | { |
| 4628 | struct cg_cgroup_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4629 | |
| 4630 | BUG_ON(ss->module == NULL); |
| 4631 | |
| 4632 | /* |
| 4633 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4634 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4635 | * doesn't start being used while we're killing it off. |
| 4636 | */ |
| 4637 | BUG_ON(ss->root != &rootnode); |
| 4638 | |
| 4639 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4640 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4641 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4642 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4643 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4644 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4645 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4646 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4647 | subsys[ss->subsys_id] = NULL; |
| 4648 | |
| 4649 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4650 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4651 | |
| 4652 | /* |
| 4653 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4654 | * in loading, we need to pay our respects to the hashtable gods. |
| 4655 | */ |
| 4656 | write_lock(&css_set_lock); |
| 4657 | list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4658 | struct css_set *cset = link->cg; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4659 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4660 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 4661 | hash_del(&cset->hlist); |
| 4662 | cset->subsys[ss->subsys_id] = NULL; |
| 4663 | key = css_set_hash(cset->subsys); |
| 4664 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4665 | } |
| 4666 | write_unlock(&css_set_lock); |
| 4667 | |
| 4668 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4669 | * remove subsystem's css from the dummytop and free it - need to |
| 4670 | * free before marking as null because ss->css_free needs the |
| 4671 | * cgrp->subsys pointer to find their state. note that this also |
| 4672 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4673 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4674 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4675 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4676 | |
| 4677 | mutex_unlock(&cgroup_mutex); |
| 4678 | } |
| 4679 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4680 | |
| 4681 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4682 | * cgroup_init_early - cgroup initialization at system boot |
| 4683 | * |
| 4684 | * Initialize cgroups at system boot, and initialize any |
| 4685 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4686 | */ |
| 4687 | int __init cgroup_init_early(void) |
| 4688 | { |
| 4689 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4690 | atomic_set(&init_css_set.refcount, 1); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4691 | INIT_LIST_HEAD(&init_css_set.cg_links); |
| 4692 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4693 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4694 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4695 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4696 | root_count = 1; |
| 4697 | init_task.cgroups = &init_css_set; |
| 4698 | |
| 4699 | init_css_set_link.cg = &init_css_set; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4700 | init_css_set_link.cgrp = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4701 | list_add(&init_css_set_link.cgrp_link_list, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4702 | &rootnode.top_cgroup.css_sets); |
| 4703 | list_add(&init_css_set_link.cg_link_list, |
| 4704 | &init_css_set.cg_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4705 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4706 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4707 | struct cgroup_subsys *ss = subsys[i]; |
| 4708 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4709 | /* at bootup time, we don't worry about modular subsystems */ |
| 4710 | if (!ss || ss->module) |
| 4711 | continue; |
| 4712 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4713 | BUG_ON(!ss->name); |
| 4714 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4715 | BUG_ON(!ss->css_alloc); |
| 4716 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4717 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4718 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4719 | ss->name, ss->subsys_id); |
| 4720 | BUG(); |
| 4721 | } |
| 4722 | |
| 4723 | if (ss->early_init) |
| 4724 | cgroup_init_subsys(ss); |
| 4725 | } |
| 4726 | return 0; |
| 4727 | } |
| 4728 | |
| 4729 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4730 | * cgroup_init - cgroup initialization |
| 4731 | * |
| 4732 | * Register cgroup filesystem and /proc file, and initialize |
| 4733 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4734 | */ |
| 4735 | int __init cgroup_init(void) |
| 4736 | { |
| 4737 | int err; |
| 4738 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4739 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4740 | |
| 4741 | err = bdi_init(&cgroup_backing_dev_info); |
| 4742 | if (err) |
| 4743 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4744 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4745 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4746 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4747 | |
| 4748 | /* at bootup time, we don't worry about modular subsystems */ |
| 4749 | if (!ss || ss->module) |
| 4750 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4751 | if (!ss->early_init) |
| 4752 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4753 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4754 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4755 | } |
| 4756 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4757 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4758 | key = css_set_hash(init_css_set.subsys); |
| 4759 | hash_add(css_set_table, &init_css_set.hlist, key); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4760 | |
| 4761 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4762 | mutex_lock(&cgroup_mutex); |
| 4763 | mutex_lock(&cgroup_root_mutex); |
| 4764 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4765 | BUG_ON(cgroup_init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4766 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4767 | mutex_unlock(&cgroup_root_mutex); |
| 4768 | mutex_unlock(&cgroup_mutex); |
| 4769 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4770 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4771 | if (!cgroup_kobj) { |
| 4772 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4773 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | err = register_filesystem(&cgroup_fs_type); |
| 4777 | if (err < 0) { |
| 4778 | kobject_put(cgroup_kobj); |
| 4779 | goto out; |
| 4780 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4781 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4782 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4783 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4784 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4785 | if (err) |
| 4786 | bdi_destroy(&cgroup_backing_dev_info); |
| 4787 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4788 | return err; |
| 4789 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4790 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4791 | /* |
| 4792 | * proc_cgroup_show() |
| 4793 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4794 | * - Used for /proc/<pid>/cgroup. |
| 4795 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4796 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4797 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4798 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4799 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4800 | * cgroup to top_cgroup. |
| 4801 | */ |
| 4802 | |
| 4803 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4804 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4805 | { |
| 4806 | struct pid *pid; |
| 4807 | struct task_struct *tsk; |
| 4808 | char *buf; |
| 4809 | int retval; |
| 4810 | struct cgroupfs_root *root; |
| 4811 | |
| 4812 | retval = -ENOMEM; |
| 4813 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4814 | if (!buf) |
| 4815 | goto out; |
| 4816 | |
| 4817 | retval = -ESRCH; |
| 4818 | pid = m->private; |
| 4819 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4820 | if (!tsk) |
| 4821 | goto out_free; |
| 4822 | |
| 4823 | retval = 0; |
| 4824 | |
| 4825 | mutex_lock(&cgroup_mutex); |
| 4826 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4827 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4828 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4829 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4830 | int count = 0; |
| 4831 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4832 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4833 | for_each_subsys(root, ss) |
| 4834 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4835 | if (strlen(root->name)) |
| 4836 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4837 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4838 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4839 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4840 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4841 | if (retval < 0) |
| 4842 | goto out_unlock; |
| 4843 | seq_puts(m, buf); |
| 4844 | seq_putc(m, '\n'); |
| 4845 | } |
| 4846 | |
| 4847 | out_unlock: |
| 4848 | mutex_unlock(&cgroup_mutex); |
| 4849 | put_task_struct(tsk); |
| 4850 | out_free: |
| 4851 | kfree(buf); |
| 4852 | out: |
| 4853 | return retval; |
| 4854 | } |
| 4855 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4856 | /* Display information about each subsystem and each hierarchy */ |
| 4857 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4858 | { |
| 4859 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4860 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4861 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4862 | /* |
| 4863 | * ideally we don't want subsystems moving around while we do this. |
| 4864 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4865 | * subsys/hierarchy state. |
| 4866 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4867 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4868 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4869 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4870 | if (ss == NULL) |
| 4871 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4872 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 4873 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4874 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4875 | } |
| 4876 | mutex_unlock(&cgroup_mutex); |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 4881 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 4882 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 4885 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4886 | .open = cgroupstats_open, |
| 4887 | .read = seq_read, |
| 4888 | .llseek = seq_lseek, |
| 4889 | .release = single_release, |
| 4890 | }; |
| 4891 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4892 | /** |
| 4893 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4894 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4895 | * |
| 4896 | * Description: A task inherits its parent's cgroup at fork(). |
| 4897 | * |
| 4898 | * A pointer to the shared css_set was automatically copied in |
| 4899 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4900 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 4901 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 4902 | * have already changed current->cgroups, allowing the previously |
| 4903 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4904 | * |
| 4905 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 4906 | * task, and the passed argument 'child' points to the child task. |
| 4907 | */ |
| 4908 | void cgroup_fork(struct task_struct *child) |
| 4909 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4910 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4911 | child->cgroups = current->cgroups; |
| 4912 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 4913 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4914 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4918 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 4919 | * @child: the task in question |
| 4920 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4921 | * Adds the task to the list running through its css_set if necessary and |
| 4922 | * call the subsystem fork() callbacks. Has to be after the task is |
| 4923 | * visible on the task list in case we race with the first call to |
| 4924 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 4925 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4926 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4927 | void cgroup_post_fork(struct task_struct *child) |
| 4928 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4929 | int i; |
| 4930 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 4931 | /* |
| 4932 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 4933 | * under the tasklist_lock and we read it here after we added the child |
| 4934 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 4935 | * yet in the tasklist when we walked through it from |
| 4936 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 4937 | * should be visible now due to the paired locking and barriers implied |
| 4938 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 4939 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 4940 | * lock on fork. |
| 4941 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4942 | if (use_task_css_set_links) { |
| 4943 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4944 | task_lock(child); |
| 4945 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4946 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 4947 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4948 | write_unlock(&css_set_lock); |
| 4949 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4950 | |
| 4951 | /* |
| 4952 | * Call ss->fork(). This must happen after @child is linked on |
| 4953 | * css_set; otherwise, @child might change state between ->fork() |
| 4954 | * and addition to css_set. |
| 4955 | */ |
| 4956 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 4957 | /* |
| 4958 | * fork/exit callbacks are supported only for builtin |
| 4959 | * subsystems, and the builtin section of the subsys |
| 4960 | * array is immutable, so we don't need to lock the |
| 4961 | * subsys array here. On the other hand, modular section |
| 4962 | * of the array can be freed at module unload, so we |
| 4963 | * can't touch that. |
| 4964 | */ |
| 4965 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4966 | struct cgroup_subsys *ss = subsys[i]; |
| 4967 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4968 | if (ss->fork) |
| 4969 | ss->fork(child); |
| 4970 | } |
| 4971 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4972 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 4973 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4974 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4975 | * cgroup_exit - detach cgroup from exiting task |
| 4976 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4977 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4978 | * |
| 4979 | * Description: Detach cgroup from @tsk and release it. |
| 4980 | * |
| 4981 | * Note that cgroups marked notify_on_release force every task in |
| 4982 | * them to take the global cgroup_mutex mutex when exiting. |
| 4983 | * This could impact scaling on very large systems. Be reluctant to |
| 4984 | * use notify_on_release cgroups where very high task exit scaling |
| 4985 | * is required on large systems. |
| 4986 | * |
| 4987 | * the_top_cgroup_hack: |
| 4988 | * |
| 4989 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 4990 | * |
| 4991 | * We call cgroup_exit() while the task is still competent to |
| 4992 | * handle notify_on_release(), then leave the task attached to the |
| 4993 | * root cgroup in each hierarchy for the remainder of its exit. |
| 4994 | * |
| 4995 | * To do this properly, we would increment the reference count on |
| 4996 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 4997 | * code we would add a second cgroup function call, to drop that |
| 4998 | * reference. This would just create an unnecessary hot spot on |
| 4999 | * the top_cgroup reference count, to no avail. |
| 5000 | * |
| 5001 | * Normally, holding a reference to a cgroup without bumping its |
| 5002 | * count is unsafe. The cgroup could go away, or someone could |
| 5003 | * attach us to a different cgroup, decrementing the count on |
| 5004 | * the first cgroup that we never incremented. But in this case, |
| 5005 | * 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] | 5006 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5007 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5008 | */ |
| 5009 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5010 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5011 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5012 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5013 | |
| 5014 | /* |
| 5015 | * Unlink from the css_set task list if necessary. |
| 5016 | * Optimistically check cg_list before taking |
| 5017 | * css_set_lock |
| 5018 | */ |
| 5019 | if (!list_empty(&tsk->cg_list)) { |
| 5020 | write_lock(&css_set_lock); |
| 5021 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5022 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5023 | write_unlock(&css_set_lock); |
| 5024 | } |
| 5025 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5026 | /* Reassign the task to the init_css_set. */ |
| 5027 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5028 | cset = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5029 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5030 | |
| 5031 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5032 | /* |
| 5033 | * fork/exit callbacks are supported only for builtin |
| 5034 | * subsystems, see cgroup_post_fork() for details. |
| 5035 | */ |
| 5036 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5037 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5038 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5039 | if (ss->exit) { |
| 5040 | struct cgroup *old_cgrp = |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5041 | rcu_dereference_raw(cset->subsys[i])->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5042 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5043 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5044 | } |
| 5045 | } |
| 5046 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5047 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5048 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5049 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5050 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5051 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5052 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5053 | { |
| 5054 | /* All of these checks rely on RCU to keep the cgroup |
| 5055 | * structure alive */ |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5056 | if (cgroup_is_releasable(cgrp) && |
| 5057 | !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) { |
| 5058 | /* |
| 5059 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5060 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5061 | * it now |
| 5062 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5063 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5064 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5065 | raw_spin_lock(&release_list_lock); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5066 | if (!cgroup_is_removed(cgrp) && |
| 5067 | list_empty(&cgrp->release_list)) { |
| 5068 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5069 | need_schedule_work = 1; |
| 5070 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5071 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5072 | if (need_schedule_work) |
| 5073 | schedule_work(&release_agent_work); |
| 5074 | } |
| 5075 | } |
| 5076 | |
Daisuke Nishimura | d7b9fff | 2010-03-10 15:22:05 -0800 | [diff] [blame] | 5077 | /* Caller must verify that the css is not for root cgroup */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5078 | bool __css_tryget(struct cgroup_subsys_state *css) |
| 5079 | { |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5080 | while (true) { |
| 5081 | int t, v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5082 | |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5083 | v = css_refcnt(css); |
| 5084 | t = atomic_cmpxchg(&css->refcnt, v, v + 1); |
| 5085 | if (likely(t == v)) |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5086 | return true; |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5087 | else if (t < 0) |
| 5088 | return false; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5089 | cpu_relax(); |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5090 | } |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5091 | } |
| 5092 | EXPORT_SYMBOL_GPL(__css_tryget); |
| 5093 | |
| 5094 | /* Caller must verify that the css is not for root cgroup */ |
| 5095 | void __css_put(struct cgroup_subsys_state *css) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5096 | { |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5097 | int v; |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5098 | |
Salman Qazi | 8e3bbf4 | 2012-06-14 14:55:30 -0700 | [diff] [blame] | 5099 | v = css_unbias_refcnt(atomic_dec_return(&css->refcnt)); |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5100 | if (v == 0) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 5101 | schedule_work(&css->dput_work); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5102 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5103 | EXPORT_SYMBOL_GPL(__css_put); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5104 | |
| 5105 | /* |
| 5106 | * Notify userspace when a cgroup is released, by running the |
| 5107 | * configured release agent with the name of the cgroup (path |
| 5108 | * relative to the root of cgroup file system) as the argument. |
| 5109 | * |
| 5110 | * Most likely, this user command will try to rmdir this cgroup. |
| 5111 | * |
| 5112 | * This races with the possibility that some other task will be |
| 5113 | * attached to this cgroup before it is removed, or that some other |
| 5114 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5115 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5116 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5117 | * to continue to serve a useful existence. Next time it's released, |
| 5118 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5119 | * |
| 5120 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5121 | * means only wait until the task is successfully execve()'d. The |
| 5122 | * separate release agent task is forked by call_usermodehelper(), |
| 5123 | * then control in this thread returns here, without waiting for the |
| 5124 | * release agent task. We don't bother to wait because the caller of |
| 5125 | * this routine has no use for the exit status of the release agent |
| 5126 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5127 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5128 | static void cgroup_release_agent(struct work_struct *work) |
| 5129 | { |
| 5130 | BUG_ON(work != &release_agent_work); |
| 5131 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5132 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5133 | while (!list_empty(&release_list)) { |
| 5134 | char *argv[3], *envp[3]; |
| 5135 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5136 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5137 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5138 | struct cgroup, |
| 5139 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5140 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5141 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5142 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5143 | if (!pathbuf) |
| 5144 | goto continue_free; |
| 5145 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5146 | goto continue_free; |
| 5147 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5148 | if (!agentbuf) |
| 5149 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5150 | |
| 5151 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5152 | argv[i++] = agentbuf; |
| 5153 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5154 | argv[i] = NULL; |
| 5155 | |
| 5156 | i = 0; |
| 5157 | /* minimal command environment */ |
| 5158 | envp[i++] = "HOME=/"; |
| 5159 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5160 | envp[i] = NULL; |
| 5161 | |
| 5162 | /* Drop the lock while we invoke the usermode helper, |
| 5163 | * since the exec could involve hitting disk and hence |
| 5164 | * be a slow process */ |
| 5165 | mutex_unlock(&cgroup_mutex); |
| 5166 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5167 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5168 | continue_free: |
| 5169 | kfree(pathbuf); |
| 5170 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5171 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5172 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5173 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5174 | mutex_unlock(&cgroup_mutex); |
| 5175 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5176 | |
| 5177 | static int __init cgroup_disable(char *str) |
| 5178 | { |
| 5179 | int i; |
| 5180 | char *token; |
| 5181 | |
| 5182 | while ((token = strsep(&str, ",")) != NULL) { |
| 5183 | if (!*token) |
| 5184 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5185 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5186 | struct cgroup_subsys *ss = subsys[i]; |
| 5187 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5188 | /* |
| 5189 | * cgroup_disable, being at boot time, can't |
| 5190 | * know about module subsystems, so we don't |
| 5191 | * worry about them. |
| 5192 | */ |
| 5193 | if (!ss || ss->module) |
| 5194 | continue; |
| 5195 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5196 | if (!strcmp(token, ss->name)) { |
| 5197 | ss->disabled = 1; |
| 5198 | printk(KERN_INFO "Disabling %s control group" |
| 5199 | " subsystem\n", ss->name); |
| 5200 | break; |
| 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | return 1; |
| 5205 | } |
| 5206 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5207 | |
| 5208 | /* |
| 5209 | * Functons for CSS ID. |
| 5210 | */ |
| 5211 | |
| 5212 | /* |
| 5213 | *To get ID other than 0, this should be called when !cgroup_is_removed(). |
| 5214 | */ |
| 5215 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5216 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5217 | struct css_id *cssid; |
| 5218 | |
| 5219 | /* |
| 5220 | * This css_id() can return correct value when somone has refcnt |
| 5221 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5222 | * it's unchanged until freed. |
| 5223 | */ |
Tejun Heo | 28b4c27 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 5224 | cssid = rcu_dereference_check(css->id, css_refcnt(css)); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5225 | |
| 5226 | if (cssid) |
| 5227 | return cssid->id; |
| 5228 | return 0; |
| 5229 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5230 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5231 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5232 | /** |
| 5233 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5234 | * @child: the css to be tested. |
| 5235 | * @root: the css supporsed to be an ancestor of the child. |
| 5236 | * |
| 5237 | * 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] | 5238 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5239 | * But, considering usual usage, the csses should be valid objects after test. |
| 5240 | * Assuming that the caller will do some action to the child if this returns |
| 5241 | * returns true, the caller must take "child";s reference count. |
| 5242 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5243 | */ |
| 5244 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5245 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5246 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5247 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5248 | struct css_id *child_id; |
| 5249 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5250 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5251 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5252 | if (!child_id) |
| 5253 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5254 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5255 | if (!root_id) |
| 5256 | return false; |
| 5257 | if (child_id->depth < root_id->depth) |
| 5258 | return false; |
| 5259 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5260 | return false; |
| 5261 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5264 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5265 | { |
| 5266 | struct css_id *id = css->id; |
| 5267 | /* When this is called before css_id initialization, id can be NULL */ |
| 5268 | if (!id) |
| 5269 | return; |
| 5270 | |
| 5271 | BUG_ON(!ss->use_id); |
| 5272 | |
| 5273 | rcu_assign_pointer(id->css, NULL); |
| 5274 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5275 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5276 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5277 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5278 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5279 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5280 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5281 | |
| 5282 | /* |
| 5283 | * This is called by init or create(). Then, calls to this function are |
| 5284 | * always serialized (By cgroup_mutex() at create()). |
| 5285 | */ |
| 5286 | |
| 5287 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5288 | { |
| 5289 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5290 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5291 | |
| 5292 | BUG_ON(!ss->use_id); |
| 5293 | |
| 5294 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5295 | newid = kzalloc(size, GFP_KERNEL); |
| 5296 | if (!newid) |
| 5297 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5298 | |
| 5299 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5300 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5301 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5302 | 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] | 5303 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5304 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5305 | |
| 5306 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5307 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5308 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5309 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5310 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5311 | newid->depth = depth; |
| 5312 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5313 | err_out: |
| 5314 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5315 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5316 | |
| 5317 | } |
| 5318 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5319 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5320 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5321 | { |
| 5322 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5323 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5324 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5325 | idr_init(&ss->idr); |
| 5326 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5327 | newid = get_new_cssid(ss, 0); |
| 5328 | if (IS_ERR(newid)) |
| 5329 | return PTR_ERR(newid); |
| 5330 | |
| 5331 | newid->stack[0] = newid->id; |
| 5332 | newid->css = rootcss; |
| 5333 | rootcss->id = newid; |
| 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5338 | struct cgroup *child) |
| 5339 | { |
| 5340 | int subsys_id, i, depth = 0; |
| 5341 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5342 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5343 | |
| 5344 | subsys_id = ss->subsys_id; |
| 5345 | parent_css = parent->subsys[subsys_id]; |
| 5346 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5347 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5348 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5349 | |
| 5350 | child_id = get_new_cssid(ss, depth); |
| 5351 | if (IS_ERR(child_id)) |
| 5352 | return PTR_ERR(child_id); |
| 5353 | |
| 5354 | for (i = 0; i < depth; i++) |
| 5355 | child_id->stack[i] = parent_id->stack[i]; |
| 5356 | child_id->stack[depth] = child_id->id; |
| 5357 | /* |
| 5358 | * child_id->css pointer will be set after this cgroup is available |
| 5359 | * see cgroup_populate_dir() |
| 5360 | */ |
| 5361 | rcu_assign_pointer(child_css->id, child_id); |
| 5362 | |
| 5363 | return 0; |
| 5364 | } |
| 5365 | |
| 5366 | /** |
| 5367 | * css_lookup - lookup css by id |
| 5368 | * @ss: cgroup subsys to be looked into. |
| 5369 | * @id: the id |
| 5370 | * |
| 5371 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5372 | * NULL if not. Should be called under rcu_read_lock() |
| 5373 | */ |
| 5374 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5375 | { |
| 5376 | struct css_id *cssid = NULL; |
| 5377 | |
| 5378 | BUG_ON(!ss->use_id); |
| 5379 | cssid = idr_find(&ss->idr, id); |
| 5380 | |
| 5381 | if (unlikely(!cssid)) |
| 5382 | return NULL; |
| 5383 | |
| 5384 | return rcu_dereference(cssid->css); |
| 5385 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5386 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5387 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5388 | /* |
| 5389 | * get corresponding css from file open on cgroupfs directory |
| 5390 | */ |
| 5391 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5392 | { |
| 5393 | struct cgroup *cgrp; |
| 5394 | struct inode *inode; |
| 5395 | struct cgroup_subsys_state *css; |
| 5396 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5397 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5398 | /* check in cgroup filesystem dir */ |
| 5399 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5400 | return ERR_PTR(-EBADF); |
| 5401 | |
| 5402 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5403 | return ERR_PTR(-EINVAL); |
| 5404 | |
| 5405 | /* get cgroup */ |
| 5406 | cgrp = __d_cgrp(f->f_dentry); |
| 5407 | css = cgrp->subsys[id]; |
| 5408 | return css ? css : ERR_PTR(-ENOENT); |
| 5409 | } |
| 5410 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5411 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5412 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5413 | { |
| 5414 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5415 | |
| 5416 | if (!css) |
| 5417 | return ERR_PTR(-ENOMEM); |
| 5418 | |
| 5419 | return css; |
| 5420 | } |
| 5421 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5422 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5423 | { |
| 5424 | kfree(cont->subsys[debug_subsys_id]); |
| 5425 | } |
| 5426 | |
| 5427 | static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft) |
| 5428 | { |
| 5429 | return atomic_read(&cont->count); |
| 5430 | } |
| 5431 | |
| 5432 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5433 | { |
| 5434 | return cgroup_task_count(cont); |
| 5435 | } |
| 5436 | |
| 5437 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5438 | { |
| 5439 | return (u64)(unsigned long)current->cgroups; |
| 5440 | } |
| 5441 | |
| 5442 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5443 | struct cftype *cft) |
| 5444 | { |
| 5445 | u64 count; |
| 5446 | |
| 5447 | rcu_read_lock(); |
| 5448 | count = atomic_read(¤t->cgroups->refcount); |
| 5449 | rcu_read_unlock(); |
| 5450 | return count; |
| 5451 | } |
| 5452 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5453 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5454 | struct cftype *cft, |
| 5455 | struct seq_file *seq) |
| 5456 | { |
| 5457 | struct cg_cgroup_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5458 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5459 | |
| 5460 | read_lock(&css_set_lock); |
| 5461 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5462 | cset = rcu_dereference(current->cgroups); |
| 5463 | list_for_each_entry(link, &cset->cg_links, cg_link_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5464 | struct cgroup *c = link->cgrp; |
| 5465 | const char *name; |
| 5466 | |
| 5467 | if (c->dentry) |
| 5468 | name = c->dentry->d_name.name; |
| 5469 | else |
| 5470 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5471 | seq_printf(seq, "Root %d group %s\n", |
| 5472 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5473 | } |
| 5474 | rcu_read_unlock(); |
| 5475 | read_unlock(&css_set_lock); |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5480 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5481 | struct cftype *cft, |
| 5482 | struct seq_file *seq) |
| 5483 | { |
| 5484 | struct cg_cgroup_link *link; |
| 5485 | |
| 5486 | read_lock(&css_set_lock); |
| 5487 | list_for_each_entry(link, &cont->css_sets, cgrp_link_list) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5488 | struct css_set *cset = link->cg; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5489 | struct task_struct *task; |
| 5490 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame^] | 5491 | seq_printf(seq, "css_set %p\n", cset); |
| 5492 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5493 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5494 | seq_puts(seq, " ...\n"); |
| 5495 | break; |
| 5496 | } else { |
| 5497 | seq_printf(seq, " task %d\n", |
| 5498 | task_pid_vnr(task)); |
| 5499 | } |
| 5500 | } |
| 5501 | } |
| 5502 | read_unlock(&css_set_lock); |
| 5503 | return 0; |
| 5504 | } |
| 5505 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5506 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5507 | { |
| 5508 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5509 | } |
| 5510 | |
| 5511 | static struct cftype debug_files[] = { |
| 5512 | { |
| 5513 | .name = "cgroup_refcount", |
| 5514 | .read_u64 = cgroup_refcount_read, |
| 5515 | }, |
| 5516 | { |
| 5517 | .name = "taskcount", |
| 5518 | .read_u64 = debug_taskcount_read, |
| 5519 | }, |
| 5520 | |
| 5521 | { |
| 5522 | .name = "current_css_set", |
| 5523 | .read_u64 = current_css_set_read, |
| 5524 | }, |
| 5525 | |
| 5526 | { |
| 5527 | .name = "current_css_set_refcount", |
| 5528 | .read_u64 = current_css_set_refcount_read, |
| 5529 | }, |
| 5530 | |
| 5531 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5532 | .name = "current_css_set_cg_links", |
| 5533 | .read_seq_string = current_css_set_cg_links_read, |
| 5534 | }, |
| 5535 | |
| 5536 | { |
| 5537 | .name = "cgroup_css_links", |
| 5538 | .read_seq_string = cgroup_css_links_read, |
| 5539 | }, |
| 5540 | |
| 5541 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5542 | .name = "releasable", |
| 5543 | .read_u64 = releasable_read, |
| 5544 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5545 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5546 | { } /* terminate */ |
| 5547 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5548 | |
| 5549 | struct cgroup_subsys debug_subsys = { |
| 5550 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5551 | .css_alloc = debug_css_alloc, |
| 5552 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5553 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5554 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5555 | }; |
| 5556 | #endif /* CONFIG_CGROUP_DEBUG */ |