Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/magic.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 54 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 55 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 56 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 57 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 58 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 59 | #include <linux/eventfd.h> |
| 60 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 61 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 62 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* |
| 67 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 68 | * hierarchy must be performed while holding it. |
| 69 | * |
| 70 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 71 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 72 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 73 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 74 | * break the following locking order cycle. |
| 75 | * |
| 76 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 77 | * B. namespace_sem -> cgroup_mutex |
| 78 | * |
| 79 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 80 | * breaks it. |
| 81 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_RCU |
| 83 | DEFINE_MUTEX(cgroup_mutex); |
| 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 85 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 93 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | * registered after that. The mutable section of this array is protected by |
| 95 | * cgroup_mutex. |
| 96 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 97 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 98 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 99 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
| 104 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 105 | * subsystems that are otherwise unattached - it never has more than a |
| 106 | * single cgroup, and all tasks are part of that cgroup. |
| 107 | */ |
| 108 | static struct cgroupfs_root rootnode; |
| 109 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 110 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 111 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 112 | */ |
| 113 | struct cfent { |
| 114 | struct list_head node; |
| 115 | struct dentry *dentry; |
| 116 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 117 | |
| 118 | /* file xattrs */ |
| 119 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 123 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 124 | * cgroup_subsys->use_id != 0. |
| 125 | */ |
| 126 | #define CSS_ID_MAX (65535) |
| 127 | struct css_id { |
| 128 | /* |
| 129 | * The css to which this ID points. This pointer is set to valid value |
| 130 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 131 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 132 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 133 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 134 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 135 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 136 | /* |
| 137 | * ID of this css. |
| 138 | */ |
| 139 | unsigned short id; |
| 140 | /* |
| 141 | * Depth in hierarchy which this ID belongs to. |
| 142 | */ |
| 143 | unsigned short depth; |
| 144 | /* |
| 145 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 146 | */ |
| 147 | struct rcu_head rcu_head; |
| 148 | /* |
| 149 | * Hierarchy of CSS ID belongs to. |
| 150 | */ |
| 151 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 152 | }; |
| 153 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 154 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 155 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 156 | */ |
| 157 | struct cgroup_event { |
| 158 | /* |
| 159 | * Cgroup which the event belongs to. |
| 160 | */ |
| 161 | struct cgroup *cgrp; |
| 162 | /* |
| 163 | * Control file which the event associated. |
| 164 | */ |
| 165 | struct cftype *cft; |
| 166 | /* |
| 167 | * eventfd to signal userspace about the event. |
| 168 | */ |
| 169 | struct eventfd_ctx *eventfd; |
| 170 | /* |
| 171 | * Each of these stored in a list by the cgroup. |
| 172 | */ |
| 173 | struct list_head list; |
| 174 | /* |
| 175 | * All fields below needed to unregister event when |
| 176 | * userspace closes eventfd. |
| 177 | */ |
| 178 | poll_table pt; |
| 179 | wait_queue_head_t *wqh; |
| 180 | wait_queue_t wait; |
| 181 | struct work_struct remove; |
| 182 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 183 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 184 | /* The list of hierarchy roots */ |
| 185 | |
| 186 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 187 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 188 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 191 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 192 | * writes, either for reads. |
| 193 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 194 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 195 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 196 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 197 | #define dummytop (&rootnode.top_cgroup) |
| 198 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 199 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 200 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 201 | /* |
| 202 | * Assign a monotonically increasing serial number to cgroups. It |
| 203 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 204 | * numbers. Also, as cgroups are always appended to the parent's |
| 205 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 206 | * the ascending serial number order on the list. Protected by |
| 207 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 208 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 209 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 210 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 211 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 212 | * check for fork/exit handlers to call. This avoids us having to do |
| 213 | * extra work in the fork/exit path if none of the subsystems need to |
| 214 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 215 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 216 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 217 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 218 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 219 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 220 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 221 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 222 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 223 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 224 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 226 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 229 | /** |
| 230 | * cgroup_is_descendant - test ancestry |
| 231 | * @cgrp: the cgroup to be tested |
| 232 | * @ancestor: possible ancestor of @cgrp |
| 233 | * |
| 234 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 235 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 236 | * and @ancestor are accessible. |
| 237 | */ |
| 238 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 239 | { |
| 240 | while (cgrp) { |
| 241 | if (cgrp == ancestor) |
| 242 | return true; |
| 243 | cgrp = cgrp->parent; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 248 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 249 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 250 | { |
| 251 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 252 | (1 << CGRP_RELEASABLE) | |
| 253 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 254 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 257 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 258 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 259 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 262 | /* |
| 263 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 264 | * an active hierarchy |
| 265 | */ |
| 266 | #define for_each_subsys(_root, _ss) \ |
| 267 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 268 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 269 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 270 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 271 | list_for_each_entry(_root, &roots, root_list) |
| 272 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 273 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 274 | { |
| 275 | return dentry->d_fsdata; |
| 276 | } |
| 277 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 278 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 279 | { |
| 280 | return dentry->d_fsdata; |
| 281 | } |
| 282 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 283 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 284 | { |
| 285 | return __d_cfe(dentry)->type; |
| 286 | } |
| 287 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 288 | /** |
| 289 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 290 | * @cgrp: the cgroup to be checked for liveness |
| 291 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 292 | * On success, returns true; the mutex should be later unlocked. On |
| 293 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 296 | { |
| 297 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 298 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 299 | mutex_unlock(&cgroup_mutex); |
| 300 | return false; |
| 301 | } |
| 302 | return true; |
| 303 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 304 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 305 | /* the list of cgroups eligible for automatic release. Protected by |
| 306 | * release_list_lock */ |
| 307 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 308 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 309 | static void cgroup_release_agent(struct work_struct *work); |
| 310 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 311 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 312 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 313 | /* |
| 314 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 315 | * belong to different cgroups on different hierarchies. In the other |
| 316 | * direction, a css_set is naturally associated with multiple cgroups. |
| 317 | * This M:N relationship is represented by the following link structure |
| 318 | * which exists for each association and allows traversing the associations |
| 319 | * from both sides. |
| 320 | */ |
| 321 | struct cgrp_cset_link { |
| 322 | /* the cgroup and css_set this link associates */ |
| 323 | struct cgroup *cgrp; |
| 324 | struct css_set *cset; |
| 325 | |
| 326 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 327 | struct list_head cset_link; |
| 328 | |
| 329 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 330 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | /* The default css_set - used by init and its children prior to any |
| 334 | * hierarchies being mounted. It contains a pointer to the root state |
| 335 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 336 | * reference-counted, to improve performance when child cgroups |
| 337 | * haven't been created. |
| 338 | */ |
| 339 | |
| 340 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 341 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 342 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 343 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 344 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 345 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 346 | /* css_set_lock protects the list of css_set objects, and the |
| 347 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 348 | * due to cgroup_iter_start() */ |
| 349 | static DEFINE_RWLOCK(css_set_lock); |
| 350 | static int css_set_count; |
| 351 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 352 | /* |
| 353 | * hash table for cgroup groups. This improves the performance to find |
| 354 | * an existing css_set. This hash doesn't (currently) take into |
| 355 | * account cgroups in empty hierarchies. |
| 356 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 357 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 358 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 359 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 360 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 361 | { |
| 362 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 363 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 364 | |
| 365 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 366 | key += (unsigned long)css[i]; |
| 367 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 368 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 369 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 372 | /* We don't maintain the lists running through each css_set to its |
| 373 | * task until after the first call to cgroup_iter_start(). This |
| 374 | * reduces the fork()/exit() overhead for people who have cgroups |
| 375 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 376 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 377 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 378 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 379 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 380 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 381 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 382 | /* |
| 383 | * Ensure that the refcount doesn't hit zero while any readers |
| 384 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 385 | * rwlock |
| 386 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 387 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 388 | return; |
| 389 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 390 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 391 | write_unlock(&css_set_lock); |
| 392 | return; |
| 393 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 394 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 395 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 396 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 397 | css_set_count--; |
| 398 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 399 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 400 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 401 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 402 | list_del(&link->cset_link); |
| 403 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 404 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 405 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 406 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 407 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 408 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 409 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 410 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 411 | |
| 412 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 413 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 414 | |
| 415 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 416 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * refcounted get/put for css_set objects |
| 421 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 423 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 429 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 433 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 434 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 437 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 438 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 439 | * @cset: candidate css_set being tested |
| 440 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 441 | * @new_cgrp: cgroup that's being entered by the task |
| 442 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 443 | * |
| 444 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 445 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 446 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | static bool compare_css_sets(struct css_set *cset, |
| 448 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 449 | struct cgroup *new_cgrp, |
| 450 | struct cgroup_subsys_state *template[]) |
| 451 | { |
| 452 | struct list_head *l1, *l2; |
| 453 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 454 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 455 | /* Not all subsystems matched */ |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Compare cgroup pointers in order to distinguish between |
| 461 | * different cgroups in heirarchies with no subsystems. We |
| 462 | * could get by with just this check alone (and skip the |
| 463 | * memcmp above) but on most setups the memcmp check will |
| 464 | * avoid the need for this more expensive check on almost all |
| 465 | * candidates. |
| 466 | */ |
| 467 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 468 | l1 = &cset->cgrp_links; |
| 469 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 470 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 471 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 472 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 473 | |
| 474 | l1 = l1->next; |
| 475 | l2 = l2->next; |
| 476 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 477 | if (l1 == &cset->cgrp_links) { |
| 478 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 479 | break; |
| 480 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 481 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 482 | } |
| 483 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 484 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 485 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 486 | cgrp1 = link1->cgrp; |
| 487 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 488 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 489 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * If this hierarchy is the hierarchy of the cgroup |
| 493 | * that's changing, then we need to check that this |
| 494 | * css_set points to the new cgroup; if it's any other |
| 495 | * hierarchy, then this css_set should point to the |
| 496 | * same cgroup as the old css_set. |
| 497 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 498 | if (cgrp1->root == new_cgrp->root) { |
| 499 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 502 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 510 | * find_existing_css_set() is a helper for |
| 511 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 512 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 513 | * |
| 514 | * oldcg: the cgroup group that we're using before the cgroup |
| 515 | * transition |
| 516 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 517 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 518 | * |
| 519 | * template: location in which to build the desired set of subsystem |
| 520 | * state objects for the new cgroup group |
| 521 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 522 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 523 | struct cgroup *cgrp, |
| 524 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 525 | { |
| 526 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 527 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 528 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 529 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 530 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 531 | /* |
| 532 | * Build the set of subsystem state objects that we want to see in the |
| 533 | * new css_set. while subsystems can change globally, the entries here |
| 534 | * won't change, so no need for locking. |
| 535 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 536 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 537 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 538 | /* Subsystem is in this hierarchy. So we want |
| 539 | * the subsystem state from the new |
| 540 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 541 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 542 | } else { |
| 543 | /* Subsystem is not in this hierarchy, so we |
| 544 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 545 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 549 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 550 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 551 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 552 | continue; |
| 553 | |
| 554 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 555 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 556 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | |
| 558 | /* No existing cgroup group matched */ |
| 559 | return NULL; |
| 560 | } |
| 561 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 562 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 563 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 564 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 565 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 566 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 567 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 568 | kfree(link); |
| 569 | } |
| 570 | } |
| 571 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 572 | /** |
| 573 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 574 | * @count: the number of links to allocate |
| 575 | * @tmp_links: list_head the allocated links are put on |
| 576 | * |
| 577 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 578 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 579 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 580 | static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 581 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 582 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 583 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 584 | |
| 585 | INIT_LIST_HEAD(tmp_links); |
| 586 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 587 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 588 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 589 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 590 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 591 | return -ENOMEM; |
| 592 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 593 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 594 | } |
| 595 | return 0; |
| 596 | } |
| 597 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 598 | /** |
| 599 | * link_css_set - a helper function to link a css_set to a cgroup |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 600 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 601 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 602 | * @cgrp: the destination cgroup |
| 603 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 604 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 605 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 606 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 608 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 609 | BUG_ON(list_empty(tmp_links)); |
| 610 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 611 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 612 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 613 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 614 | /* |
| 615 | * Always add links to the tail of the list so that the list |
| 616 | * is sorted by order of hierarchy creation |
| 617 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 618 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 621 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 622 | * find_css_set() takes an existing cgroup group and a |
| 623 | * cgroup object, and returns a css_set object that's |
| 624 | * equivalent to the old group, but with the given cgroup |
| 625 | * substituted into the appropriate hierarchy. Must be called with |
| 626 | * cgroup_mutex held |
| 627 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 628 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 629 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 630 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 631 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 632 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 633 | struct list_head tmp_links; |
| 634 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 635 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 636 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 637 | /* First see if we already have a cgroup group that matches |
| 638 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 639 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 640 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 641 | if (cset) |
| 642 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 643 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 644 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | if (cset) |
| 646 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 648 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 649 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | return NULL; |
| 651 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 652 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
| 653 | if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 654 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 655 | return NULL; |
| 656 | } |
| 657 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 658 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 659 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 660 | INIT_LIST_HEAD(&cset->tasks); |
| 661 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 662 | |
| 663 | /* Copy the set of subsystem state objects generated in |
| 664 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 665 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
| 667 | write_lock(&css_set_lock); |
| 668 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 669 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 670 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 671 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 672 | if (c->root == cgrp->root) |
| 673 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 675 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 677 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 678 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 679 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 680 | |
| 681 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | key = css_set_hash(cset->subsys); |
| 683 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 684 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 685 | write_unlock(&css_set_lock); |
| 686 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 690 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 691 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 692 | * called with cgroup_mutex held. |
| 693 | */ |
| 694 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 695 | struct cgroupfs_root *root) |
| 696 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 697 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 698 | struct cgroup *res = NULL; |
| 699 | |
| 700 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 701 | read_lock(&css_set_lock); |
| 702 | /* |
| 703 | * No need to lock the task - since we hold cgroup_mutex the |
| 704 | * task can't change groups, so the only thing that can happen |
| 705 | * is that it exits and its css is set back to init_css_set. |
| 706 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 707 | cset = task->cgroups; |
| 708 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 709 | res = &root->top_cgroup; |
| 710 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 711 | struct cgrp_cset_link *link; |
| 712 | |
| 713 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 714 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 715 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 716 | if (c->root == root) { |
| 717 | res = c; |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | read_unlock(&css_set_lock); |
| 723 | BUG_ON(!res); |
| 724 | return res; |
| 725 | } |
| 726 | |
| 727 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 728 | * There is one global cgroup mutex. We also require taking |
| 729 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 730 | * See "The task_lock() exception", at the end of this comment. |
| 731 | * |
| 732 | * A task must hold cgroup_mutex to modify cgroups. |
| 733 | * |
| 734 | * Any task can increment and decrement the count field without lock. |
| 735 | * So in general, code holding cgroup_mutex can't rely on the count |
| 736 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 737 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 738 | * means that no tasks are currently attached, therefore there is no |
| 739 | * way a task attached to that cgroup can fork (the other way to |
| 740 | * increment the count). So code holding cgroup_mutex can safely |
| 741 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 742 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 743 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 744 | * needs that mutex. |
| 745 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 746 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 747 | * (usually) take cgroup_mutex. These are the two most performance |
| 748 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 749 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 750 | * is taken, and if the cgroup count is zero, a usermode call made |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 751 | * to the release agent with the name of the cgroup (path relative to |
| 752 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 753 | * |
| 754 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 755 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 756 | * tasks in the system use _some_ cgroup, and since there is always at |
| 757 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 758 | * always has either children cgroups and/or using tasks. So we don't |
| 759 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 760 | * |
| 761 | * The task_lock() exception |
| 762 | * |
| 763 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 764 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 765 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 766 | * several performance critical places that need to reference |
| 767 | * task->cgroup without the expense of grabbing a system global |
| 768 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 769 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 770 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 771 | * the task_struct routinely used for such matters. |
| 772 | * |
| 773 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 774 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 | */ |
| 776 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 777 | /* |
| 778 | * A couple of forward declarations required, due to cyclic reference loop: |
| 779 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 780 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 781 | * -> cgroup_mkdir. |
| 782 | */ |
| 783 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 784 | static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 785 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 786 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 787 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 788 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 789 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 790 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 791 | |
| 792 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 793 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 794 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 795 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 796 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 797 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 798 | struct cgroup *parent, struct cgroup *child); |
| 799 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 800 | static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 801 | { |
| 802 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | |
| 804 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 805 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 806 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 807 | inode->i_uid = current_fsuid(); |
| 808 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 809 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 810 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 811 | } |
| 812 | return inode; |
| 813 | } |
| 814 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 815 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 816 | { |
| 817 | struct cgroup_name *name; |
| 818 | |
| 819 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 820 | if (!name) |
| 821 | return NULL; |
| 822 | strcpy(name->name, dentry->d_name.name); |
| 823 | return name; |
| 824 | } |
| 825 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 826 | static void cgroup_free_fn(struct work_struct *work) |
| 827 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 828 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 829 | struct cgroup_subsys *ss; |
| 830 | |
| 831 | mutex_lock(&cgroup_mutex); |
| 832 | /* |
| 833 | * Release the subsystem state objects. |
| 834 | */ |
| 835 | for_each_subsys(cgrp->root, ss) |
| 836 | ss->css_free(cgrp); |
| 837 | |
| 838 | cgrp->root->number_of_cgroups--; |
| 839 | mutex_unlock(&cgroup_mutex); |
| 840 | |
| 841 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 842 | * We get a ref to the parent's dentry, and put the ref when |
| 843 | * this cgroup is being freed, so it's guaranteed that the |
| 844 | * parent won't be destroyed before its children. |
| 845 | */ |
| 846 | dput(cgrp->parent->dentry); |
| 847 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 848 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 849 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 850 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 851 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 852 | * created the cgroup. This will free cgrp->root, if we are |
| 853 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 854 | */ |
| 855 | deactivate_super(cgrp->root->sb); |
| 856 | |
| 857 | /* |
| 858 | * if we're getting rid of the cgroup, refcount should ensure |
| 859 | * that there are no pidlists left. |
| 860 | */ |
| 861 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 862 | |
| 863 | simple_xattrs_free(&cgrp->xattrs); |
| 864 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 865 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 866 | kfree(cgrp); |
| 867 | } |
| 868 | |
| 869 | static void cgroup_free_rcu(struct rcu_head *head) |
| 870 | { |
| 871 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 872 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 873 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 874 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 875 | } |
| 876 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 877 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 878 | { |
| 879 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 880 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 881 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 882 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 883 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 884 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 885 | } else { |
| 886 | struct cfent *cfe = __d_cfe(dentry); |
| 887 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 888 | |
| 889 | WARN_ONCE(!list_empty(&cfe->node) && |
| 890 | cgrp != &cgrp->root->top_cgroup, |
| 891 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 892 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 893 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 894 | } |
| 895 | iput(inode); |
| 896 | } |
| 897 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 898 | static int cgroup_delete(const struct dentry *d) |
| 899 | { |
| 900 | return 1; |
| 901 | } |
| 902 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 903 | static void remove_dir(struct dentry *d) |
| 904 | { |
| 905 | struct dentry *parent = dget(d->d_parent); |
| 906 | |
| 907 | d_delete(d); |
| 908 | simple_rmdir(parent->d_inode, d); |
| 909 | dput(parent); |
| 910 | } |
| 911 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 912 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 913 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 914 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 915 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 916 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 917 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 918 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 919 | /* |
| 920 | * If we're doing cleanup due to failure of cgroup_create(), |
| 921 | * the corresponding @cfe may not exist. |
| 922 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 923 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 924 | struct dentry *d = cfe->dentry; |
| 925 | |
| 926 | if (cft && cfe->type != cft) |
| 927 | continue; |
| 928 | |
| 929 | dget(d); |
| 930 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 931 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 932 | list_del_init(&cfe->node); |
| 933 | dput(d); |
| 934 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 935 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 936 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 939 | /** |
| 940 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 941 | * @dir: directory containing the files |
| 942 | * @base_files: true if the base files should be removed |
| 943 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 944 | */ |
| 945 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 946 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 947 | { |
| 948 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 949 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 950 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 951 | for_each_subsys(cgrp->root, ss) { |
| 952 | struct cftype_set *set; |
| 953 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 954 | continue; |
| 955 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 956 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 957 | } |
| 958 | if (base_files) { |
| 959 | while (!list_empty(&cgrp->files)) |
| 960 | cgroup_rm_file(cgrp, NULL); |
| 961 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
| 965 | * NOTE : the dentry must have been dget()'ed |
| 966 | */ |
| 967 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 968 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 969 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 970 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 971 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 972 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 974 | parent = dentry->d_parent; |
| 975 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 976 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 977 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 978 | spin_unlock(&dentry->d_lock); |
| 979 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 980 | remove_dir(dentry); |
| 981 | } |
| 982 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 983 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 984 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 985 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 986 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 987 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 989 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 990 | { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 991 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 992 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | int i; |
| 994 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 995 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 996 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 997 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 998 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 999 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1000 | /* Check that any added subsystems are currently free */ |
| 1001 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1002 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1003 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1004 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1005 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1006 | /* |
| 1007 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1008 | * parse_cgroupfs_options should catch that case and refcounts |
| 1009 | * ensure that subsystems won't disappear once selected. |
| 1010 | */ |
| 1011 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1012 | if (ss->root != &rootnode) { |
| 1013 | /* Subsystem isn't free */ |
| 1014 | return -EBUSY; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /* Currently we don't handle adding/removing subsystems when |
| 1019 | * any child cgroups exist. This is theoretically supportable |
| 1020 | * but involves complex error handling, so it's being left until |
| 1021 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1022 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1023 | return -EBUSY; |
| 1024 | |
| 1025 | /* Process each subsystem */ |
| 1026 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1027 | struct cgroup_subsys *ss = subsys[i]; |
| 1028 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1029 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1030 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1031 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1032 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | BUG_ON(!dummytop->subsys[i]); |
| 1034 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1035 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1036 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1037 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1038 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1040 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1041 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1042 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1043 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1044 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1045 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1046 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1047 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1048 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1049 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1050 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1051 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1052 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1053 | /* subsystem is now free - drop reference on module */ |
| 1054 | module_put(ss->module); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1055 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1056 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1057 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1058 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1059 | /* |
| 1060 | * a refcount was taken, but we already had one, so |
| 1061 | * drop the extra reference. |
| 1062 | */ |
| 1063 | module_put(ss->module); |
| 1064 | #ifdef CONFIG_MODULE_UNLOAD |
| 1065 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1066 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1067 | } else { |
| 1068 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1069 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1072 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1077 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1079 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1080 | struct cgroup_subsys *ss; |
| 1081 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1082 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1083 | for_each_subsys(root, ss) |
| 1084 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1085 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1086 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1087 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1088 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1089 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1090 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1091 | if (strlen(root->release_agent_path)) |
| 1092 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1093 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1094 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1095 | if (strlen(root->name)) |
| 1096 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1097 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1102 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1103 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1104 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1105 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1106 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1107 | /* User explicitly requested empty subsystem */ |
| 1108 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1109 | |
| 1110 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1111 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1112 | }; |
| 1113 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1114 | /* |
| 1115 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1116 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1117 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1118 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1119 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1120 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1121 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1122 | char *token, *o = data; |
| 1123 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1124 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1125 | int i; |
| 1126 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1127 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1128 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1129 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1130 | #ifdef CONFIG_CPUSETS |
| 1131 | mask = ~(1UL << cpuset_subsys_id); |
| 1132 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1133 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1134 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1135 | |
| 1136 | while ((token = strsep(&o, ",")) != NULL) { |
| 1137 | if (!*token) |
| 1138 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1139 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1140 | /* Explicitly have no subsystems */ |
| 1141 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1142 | continue; |
| 1143 | } |
| 1144 | if (!strcmp(token, "all")) { |
| 1145 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1146 | if (one_ss) |
| 1147 | return -EINVAL; |
| 1148 | all_ss = true; |
| 1149 | continue; |
| 1150 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1151 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1152 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1153 | continue; |
| 1154 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1155 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1156 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1157 | continue; |
| 1158 | } |
| 1159 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1160 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1163 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1164 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1165 | continue; |
| 1166 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1167 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1168 | /* Specifying two release agents is forbidden */ |
| 1169 | if (opts->release_agent) |
| 1170 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1171 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1172 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1173 | if (!opts->release_agent) |
| 1174 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1175 | continue; |
| 1176 | } |
| 1177 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1178 | const char *name = token + 5; |
| 1179 | /* Can't specify an empty name */ |
| 1180 | if (!strlen(name)) |
| 1181 | return -EINVAL; |
| 1182 | /* Must match [\w.-]+ */ |
| 1183 | for (i = 0; i < strlen(name); i++) { |
| 1184 | char c = name[i]; |
| 1185 | if (isalnum(c)) |
| 1186 | continue; |
| 1187 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1188 | continue; |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | /* Specifying two names is forbidden */ |
| 1192 | if (opts->name) |
| 1193 | return -EINVAL; |
| 1194 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1195 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1196 | GFP_KERNEL); |
| 1197 | if (!opts->name) |
| 1198 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1199 | |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1204 | struct cgroup_subsys *ss = subsys[i]; |
| 1205 | if (ss == NULL) |
| 1206 | continue; |
| 1207 | if (strcmp(token, ss->name)) |
| 1208 | continue; |
| 1209 | if (ss->disabled) |
| 1210 | continue; |
| 1211 | |
| 1212 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1213 | if (all_ss) |
| 1214 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1215 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1216 | one_ss = true; |
| 1217 | |
| 1218 | break; |
| 1219 | } |
| 1220 | if (i == CGROUP_SUBSYS_COUNT) |
| 1221 | return -ENOENT; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1226 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1227 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1228 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1229 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1230 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1231 | struct cgroup_subsys *ss = subsys[i]; |
| 1232 | if (ss == NULL) |
| 1233 | continue; |
| 1234 | if (ss->disabled) |
| 1235 | continue; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1236 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1240 | /* Consistency checks */ |
| 1241 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1242 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1243 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1244 | |
| 1245 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1246 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | |
| 1250 | if (opts->cpuset_clone_children) { |
| 1251 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1256 | /* |
| 1257 | * Option noprefix was introduced just for backward compatibility |
| 1258 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1259 | * the cpuset subsystem. |
| 1260 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1261 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1262 | return -EINVAL; |
| 1263 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1264 | |
| 1265 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1266 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1267 | return -EINVAL; |
| 1268 | |
| 1269 | /* |
| 1270 | * We either have to specify by name or by subsystems. (So all |
| 1271 | * empty hierarchies must have a name). |
| 1272 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1273 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1274 | return -EINVAL; |
| 1275 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1276 | /* |
| 1277 | * Grab references on all the modules we'll need, so the subsystems |
| 1278 | * don't dance around before rebind_subsystems attaches them. This may |
| 1279 | * take duplicate reference counts on a subsystem that's already used, |
| 1280 | * but rebind_subsystems handles this case. |
| 1281 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1282 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1283 | unsigned long bit = 1UL << i; |
| 1284 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1285 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1286 | continue; |
| 1287 | if (!try_module_get(subsys[i]->module)) { |
| 1288 | module_pin_failed = true; |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | if (module_pin_failed) { |
| 1293 | /* |
| 1294 | * oops, one of the modules was going away. this means that we |
| 1295 | * raced with a module_delete call, and to the user this is |
| 1296 | * essentially a "subsystem doesn't exist" case. |
| 1297 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1298 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1299 | /* drop refcounts only on the ones we took */ |
| 1300 | unsigned long bit = 1UL << i; |
| 1301 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1302 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1303 | continue; |
| 1304 | module_put(subsys[i]->module); |
| 1305 | } |
| 1306 | return -ENOENT; |
| 1307 | } |
| 1308 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1312 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1313 | { |
| 1314 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1315 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1316 | unsigned long bit = 1UL << i; |
| 1317 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1318 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1319 | continue; |
| 1320 | module_put(subsys[i]->module); |
| 1321 | } |
| 1322 | } |
| 1323 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1324 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1325 | { |
| 1326 | int ret = 0; |
| 1327 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1328 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1329 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1330 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1331 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1332 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1333 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1334 | return -EINVAL; |
| 1335 | } |
| 1336 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1337 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1338 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1339 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1340 | |
| 1341 | /* See what subsystems are wanted */ |
| 1342 | ret = parse_cgroupfs_options(data, &opts); |
| 1343 | if (ret) |
| 1344 | goto out_unlock; |
| 1345 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1346 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1347 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1348 | task_tgid_nr(current), current->comm); |
| 1349 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1350 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1351 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1352 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1353 | /* Don't allow flags or name to change at remount */ |
| 1354 | if (opts.flags != root->flags || |
| 1355 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1356 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1357 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1358 | goto out_unlock; |
| 1359 | } |
| 1360 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1361 | /* |
| 1362 | * Clear out the files of subsystems that should be removed, do |
| 1363 | * this before rebind_subsystems, since rebind_subsystems may |
| 1364 | * change this hierarchy's subsys_list. |
| 1365 | */ |
| 1366 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1367 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1368 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1369 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1370 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1371 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1372 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1373 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1374 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1375 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1376 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1377 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1378 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1379 | if (opts.release_agent) |
| 1380 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1382 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1383 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1384 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1385 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1386 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1387 | return ret; |
| 1388 | } |
| 1389 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1390 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1391 | .statfs = simple_statfs, |
| 1392 | .drop_inode = generic_delete_inode, |
| 1393 | .show_options = cgroup_show_options, |
| 1394 | .remount_fs = cgroup_remount, |
| 1395 | }; |
| 1396 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1397 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1398 | { |
| 1399 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1400 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1401 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1402 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1403 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1404 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1405 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1406 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1407 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1408 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1409 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1410 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1411 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1412 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1413 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1414 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1415 | INIT_LIST_HEAD(&root->subsys_list); |
| 1416 | INIT_LIST_HEAD(&root->root_list); |
| 1417 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1418 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1419 | cgrp->name = &root_cgroup_name; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1420 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1423 | static int cgroup_init_root_id(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1424 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1425 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1426 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1427 | lockdep_assert_held(&cgroup_mutex); |
| 1428 | lockdep_assert_held(&cgroup_root_mutex); |
| 1429 | |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1430 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL); |
| 1431 | if (id < 0) |
| 1432 | return id; |
| 1433 | |
| 1434 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1439 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1440 | lockdep_assert_held(&cgroup_mutex); |
| 1441 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1442 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1443 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1444 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1445 | root->hierarchy_id = 0; |
| 1446 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1449 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1450 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1451 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1452 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1453 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1454 | /* If we asked for a name then it must match */ |
| 1455 | if (opts->name && strcmp(opts->name, root->name)) |
| 1456 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1457 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * If we asked for subsystems (or explicitly for no |
| 1460 | * subsystems) then they must match |
| 1461 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1462 | if ((opts->subsys_mask || opts->none) |
| 1463 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1464 | return 0; |
| 1465 | |
| 1466 | return 1; |
| 1467 | } |
| 1468 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1469 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1470 | { |
| 1471 | struct cgroupfs_root *root; |
| 1472 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1473 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1474 | return NULL; |
| 1475 | |
| 1476 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1477 | if (!root) |
| 1478 | return ERR_PTR(-ENOMEM); |
| 1479 | |
| 1480 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1481 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1482 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1483 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1484 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1485 | if (opts->release_agent) |
| 1486 | strcpy(root->release_agent_path, opts->release_agent); |
| 1487 | if (opts->name) |
| 1488 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1489 | if (opts->cpuset_clone_children) |
| 1490 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1491 | return root; |
| 1492 | } |
| 1493 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1494 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1495 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1496 | if (root) { |
| 1497 | /* hierarhcy ID shoulid already have been released */ |
| 1498 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1499 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1500 | ida_destroy(&root->cgroup_ida); |
| 1501 | kfree(root); |
| 1502 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1505 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1506 | { |
| 1507 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1508 | struct cgroup_sb_opts *opts = data; |
| 1509 | |
| 1510 | /* If we don't have a new root, we can't set up a new sb */ |
| 1511 | if (!opts->new_root) |
| 1512 | return -EINVAL; |
| 1513 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1514 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1515 | |
| 1516 | ret = set_anon_super(sb, NULL); |
| 1517 | if (ret) |
| 1518 | return ret; |
| 1519 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1520 | sb->s_fs_info = opts->new_root; |
| 1521 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1522 | |
| 1523 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1524 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1525 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1526 | sb->s_op = &cgroup_ops; |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1532 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1533 | static const struct dentry_operations cgroup_dops = { |
| 1534 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1535 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1536 | }; |
| 1537 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1538 | struct inode *inode = |
| 1539 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (!inode) |
| 1542 | return -ENOMEM; |
| 1543 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1544 | inode->i_fop = &simple_dir_operations; |
| 1545 | inode->i_op = &cgroup_dir_inode_operations; |
| 1546 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1547 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1548 | sb->s_root = d_make_root(inode); |
| 1549 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1550 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1551 | /* for everything else we want ->d_op set */ |
| 1552 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1556 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1558 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | { |
| 1560 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1561 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1562 | int ret = 0; |
| 1563 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1564 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1565 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1566 | |
| 1567 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1568 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1569 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1570 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1571 | if (ret) |
| 1572 | goto out_err; |
| 1573 | |
| 1574 | /* |
| 1575 | * Allocate a new cgroup root. We may not need it if we're |
| 1576 | * reusing an existing hierarchy. |
| 1577 | */ |
| 1578 | new_root = cgroup_root_from_opts(&opts); |
| 1579 | if (IS_ERR(new_root)) { |
| 1580 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1581 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1582 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1583 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1584 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1585 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1586 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1587 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1588 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1589 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1590 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1593 | root = sb->s_fs_info; |
| 1594 | BUG_ON(!root); |
| 1595 | if (root == opts.new_root) { |
| 1596 | /* We used the new root structure, so this is a new hierarchy */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1597 | struct list_head tmp_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1598 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1599 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1600 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1601 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1602 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1603 | |
| 1604 | BUG_ON(sb->s_root != NULL); |
| 1605 | |
| 1606 | ret = cgroup_get_rootdir(sb); |
| 1607 | if (ret) |
| 1608 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1609 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1611 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1613 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1614 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1615 | /* Check for name clashes with existing mounts */ |
| 1616 | ret = -EBUSY; |
| 1617 | if (strlen(root->name)) |
| 1618 | for_each_active_root(existing_root) |
| 1619 | if (!strcmp(existing_root->name, root->name)) |
| 1620 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1621 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1622 | /* |
| 1623 | * We're accessing css_set_count without locking |
| 1624 | * css_set_lock here, but that's OK - it can only be |
| 1625 | * increased by someone holding cgroup_lock, and |
| 1626 | * that's us. The worst that can happen is that we |
| 1627 | * have some link structures left over |
| 1628 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1629 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1630 | if (ret) |
| 1631 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1632 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1633 | ret = cgroup_init_root_id(root); |
| 1634 | if (ret) |
| 1635 | goto unlock_drop; |
| 1636 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1637 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1638 | if (ret == -EBUSY) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1639 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1640 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1641 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1642 | /* |
| 1643 | * There must be no failure case after here, since rebinding |
| 1644 | * takes care of subsystems' refcounts, which are explicitly |
| 1645 | * dropped in the failure exit path. |
| 1646 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* EBUSY should be the only error here */ |
| 1649 | BUG_ON(ret); |
| 1650 | |
| 1651 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1652 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1654 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | root->top_cgroup.dentry = sb->s_root; |
| 1656 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1657 | /* Link the top cgroup in this hierarchy into all |
| 1658 | * the css_set objects */ |
| 1659 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1660 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1661 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1662 | write_unlock(&css_set_lock); |
| 1663 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1664 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1665 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1666 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1667 | BUG_ON(root->number_of_cgroups != 1); |
| 1668 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1669 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1670 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1671 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1672 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1673 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1674 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1675 | } else { |
| 1676 | /* |
| 1677 | * We re-used an existing hierarchy - the new root (if |
| 1678 | * any) is not needed |
| 1679 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1680 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1681 | |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1682 | if (root->flags != opts.flags) { |
| 1683 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1684 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1685 | ret = -EINVAL; |
| 1686 | goto drop_new_super; |
| 1687 | } else { |
| 1688 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1689 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1692 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1693 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1696 | kfree(opts.release_agent); |
| 1697 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1698 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1699 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1700 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1701 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1702 | mutex_unlock(&cgroup_root_mutex); |
| 1703 | mutex_unlock(&cgroup_mutex); |
| 1704 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1705 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1706 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1707 | drop_modules: |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1708 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1709 | out_err: |
| 1710 | kfree(opts.release_agent); |
| 1711 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1712 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1716 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1717 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1718 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1719 | int ret; |
| 1720 | |
| 1721 | BUG_ON(!root); |
| 1722 | |
| 1723 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1724 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | |
| 1726 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1727 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1728 | |
| 1729 | /* Rebind all subsystems back to the default hierarchy */ |
| 1730 | ret = rebind_subsystems(root, 0); |
| 1731 | /* Shouldn't be able to fail ... */ |
| 1732 | BUG_ON(ret); |
| 1733 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1734 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1735 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1736 | * root cgroup |
| 1737 | */ |
| 1738 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1739 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1740 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1741 | list_del(&link->cset_link); |
| 1742 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1743 | kfree(link); |
| 1744 | } |
| 1745 | write_unlock(&css_set_lock); |
| 1746 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1747 | if (!list_empty(&root->root_list)) { |
| 1748 | list_del(&root->root_list); |
| 1749 | root_count--; |
| 1750 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1751 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1752 | cgroup_exit_root_id(root); |
| 1753 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1754 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1755 | mutex_unlock(&cgroup_mutex); |
| 1756 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1757 | simple_xattrs_free(&cgrp->xattrs); |
| 1758 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1760 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | static struct file_system_type cgroup_fs_type = { |
| 1764 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1765 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1766 | .kill_sb = cgroup_kill_sb, |
| 1767 | }; |
| 1768 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1769 | static struct kobject *cgroup_kobj; |
| 1770 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1771 | /** |
| 1772 | * cgroup_path - generate the path of a cgroup |
| 1773 | * @cgrp: the cgroup in question |
| 1774 | * @buf: the buffer to write the path into |
| 1775 | * @buflen: the length of the buffer |
| 1776 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1777 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1778 | * |
| 1779 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1780 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1781 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1782 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1784 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1786 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1787 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1788 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1789 | if (!cgrp->parent) { |
| 1790 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1791 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1792 | return 0; |
| 1793 | } |
| 1794 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1795 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1796 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1797 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1798 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1799 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1800 | const char *name = cgroup_name(cgrp); |
| 1801 | int len; |
| 1802 | |
| 1803 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1804 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1805 | goto out; |
| 1806 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1807 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1810 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1811 | |
| 1812 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1813 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1814 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1815 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1816 | out: |
| 1817 | rcu_read_unlock(); |
| 1818 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1819 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1820 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1821 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1822 | /** |
| 1823 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1824 | * @task: target task |
| 1825 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1826 | * @buf: the buffer to write the path into |
| 1827 | * @buflen: the length of the buffer |
| 1828 | * |
| 1829 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1830 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1831 | * be used inside locks used by cgroup controller callbacks. |
| 1832 | */ |
| 1833 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1834 | char *buf, size_t buflen) |
| 1835 | { |
| 1836 | struct cgroupfs_root *root; |
| 1837 | struct cgroup *cgrp = NULL; |
| 1838 | int ret = -ENOENT; |
| 1839 | |
| 1840 | mutex_lock(&cgroup_mutex); |
| 1841 | |
| 1842 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1843 | if (root) { |
| 1844 | cgrp = task_cgroup_from_root(task, root); |
| 1845 | ret = cgroup_path(cgrp, buf, buflen); |
| 1846 | } |
| 1847 | |
| 1848 | mutex_unlock(&cgroup_mutex); |
| 1849 | |
| 1850 | return ret; |
| 1851 | } |
| 1852 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1853 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1854 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | * Control Group taskset |
| 1856 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1857 | struct task_and_cgroup { |
| 1858 | struct task_struct *task; |
| 1859 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1860 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1861 | }; |
| 1862 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1863 | struct cgroup_taskset { |
| 1864 | struct task_and_cgroup single; |
| 1865 | struct flex_array *tc_array; |
| 1866 | int tc_array_len; |
| 1867 | int idx; |
| 1868 | struct cgroup *cur_cgrp; |
| 1869 | }; |
| 1870 | |
| 1871 | /** |
| 1872 | * cgroup_taskset_first - reset taskset and return the first task |
| 1873 | * @tset: taskset of interest |
| 1874 | * |
| 1875 | * @tset iteration is initialized and the first task is returned. |
| 1876 | */ |
| 1877 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1878 | { |
| 1879 | if (tset->tc_array) { |
| 1880 | tset->idx = 0; |
| 1881 | return cgroup_taskset_next(tset); |
| 1882 | } else { |
| 1883 | tset->cur_cgrp = tset->single.cgrp; |
| 1884 | return tset->single.task; |
| 1885 | } |
| 1886 | } |
| 1887 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1888 | |
| 1889 | /** |
| 1890 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1891 | * @tset: taskset of interest |
| 1892 | * |
| 1893 | * Return the next task in @tset. Iteration must have been initialized |
| 1894 | * with cgroup_taskset_first(). |
| 1895 | */ |
| 1896 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1897 | { |
| 1898 | struct task_and_cgroup *tc; |
| 1899 | |
| 1900 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1901 | return NULL; |
| 1902 | |
| 1903 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1904 | tset->cur_cgrp = tc->cgrp; |
| 1905 | return tc->task; |
| 1906 | } |
| 1907 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1908 | |
| 1909 | /** |
| 1910 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1911 | * @tset: taskset of interest |
| 1912 | * |
| 1913 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1914 | * function must be preceded by either cgroup_taskset_first() or |
| 1915 | * cgroup_taskset_next(). |
| 1916 | */ |
| 1917 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1918 | { |
| 1919 | return tset->cur_cgrp; |
| 1920 | } |
| 1921 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1922 | |
| 1923 | /** |
| 1924 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1925 | * @tset: taskset of interest |
| 1926 | */ |
| 1927 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1928 | { |
| 1929 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1930 | } |
| 1931 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1932 | |
| 1933 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | /* |
| 1935 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1936 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1937 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1938 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1939 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1940 | struct task_struct *tsk, |
| 1941 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1943 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | |
| 1945 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1946 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1947 | * setting such that we can't race against cgroup_exit() changing the |
| 1948 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1949 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1950 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1951 | old_cset = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1952 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1954 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | task_unlock(tsk); |
| 1956 | |
| 1957 | /* Update the css_set linked lists if we're using them */ |
| 1958 | write_lock(&css_set_lock); |
| 1959 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1960 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | write_unlock(&css_set_lock); |
| 1962 | |
| 1963 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1964 | * We just gained a reference on old_cset by taking it from the |
| 1965 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1966 | * 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] | 1967 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1968 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1969 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1972 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1973 | * 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] | 1974 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1975 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1976 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1977 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1978 | * 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] | 1979 | * 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] | 1980 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1981 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1982 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1983 | { |
| 1984 | int retval, i, group_size; |
| 1985 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1986 | struct cgroupfs_root *root = cgrp->root; |
| 1987 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1988 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1989 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1990 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1991 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* |
| 1994 | * step 0: in order to do expensive, possibly blocking operations for |
| 1995 | * every thread, we cannot iterate the thread group list, since it needs |
| 1996 | * 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] | 1997 | * group - group_rwsem prevents new threads from appearing, and if |
| 1998 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1999 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2000 | if (threadgroup) |
| 2001 | group_size = get_nr_threads(tsk); |
| 2002 | else |
| 2003 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2004 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2005 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | if (!group) |
| 2007 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2008 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2009 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2010 | if (retval) |
| 2011 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2014 | /* |
| 2015 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2016 | * already PF_EXITING could be freed from underneath us unless we |
| 2017 | * take an rcu_read_lock. |
| 2018 | */ |
| 2019 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2020 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2021 | struct task_and_cgroup ent; |
| 2022 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2023 | /* @tsk either already exited or can't exit until the end */ |
| 2024 | if (tsk->flags & PF_EXITING) |
| 2025 | continue; |
| 2026 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2027 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2028 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2029 | ent.task = tsk; |
| 2030 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2031 | /* nothing to do if this task is already in the cgroup */ |
| 2032 | if (ent.cgrp == cgrp) |
| 2033 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2034 | /* |
| 2035 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2036 | * earlier, but it's good form to communicate our expectations. |
| 2037 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2038 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2039 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2040 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2041 | |
| 2042 | if (!threadgroup) |
| 2043 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2045 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2046 | /* remember the number of threads in the array for later. */ |
| 2047 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2048 | tset.tc_array = group; |
| 2049 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2050 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2051 | /* methods shouldn't be called if no task is actually migrating */ |
| 2052 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2053 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2054 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2055 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2056 | /* |
| 2057 | * step 1: check that we can legitimately attach to the cgroup. |
| 2058 | */ |
| 2059 | for_each_subsys(root, ss) { |
| 2060 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2061 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2062 | if (retval) { |
| 2063 | failed_ss = ss; |
| 2064 | goto out_cancel_attach; |
| 2065 | } |
| 2066 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2071 | * we use find_css_set, which allocates a new one if necessary. |
| 2072 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2073 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2074 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2075 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2076 | if (!tc->cg) { |
| 2077 | retval = -ENOMEM; |
| 2078 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2083 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2084 | * proceed to move all tasks to the new cgroup. There are no |
| 2085 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2086 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2087 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2088 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2089 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2090 | } |
| 2091 | /* nothing is sensitive to fork() after this point. */ |
| 2092 | |
| 2093 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2094 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2095 | */ |
| 2096 | for_each_subsys(root, ss) { |
| 2097 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2098 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * step 5: success! and cleanup |
| 2103 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2105 | out_put_css_set_refs: |
| 2106 | if (retval) { |
| 2107 | for (i = 0; i < group_size; i++) { |
| 2108 | tc = flex_array_get(group, i); |
| 2109 | if (!tc->cg) |
| 2110 | break; |
| 2111 | put_css_set(tc->cg); |
| 2112 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2113 | } |
| 2114 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2115 | if (retval) { |
| 2116 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2117 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2118 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2119 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2120 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2121 | } |
| 2122 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2123 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2124 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2125 | return retval; |
| 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * 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] | 2130 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2131 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2132 | */ |
| 2133 | 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] | 2134 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2135 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2136 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2137 | int ret; |
| 2138 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2139 | if (!cgroup_lock_live_group(cgrp)) |
| 2140 | return -ENODEV; |
| 2141 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2142 | retry_find_task: |
| 2143 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2144 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2145 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2146 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2147 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2148 | ret= -ESRCH; |
| 2149 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2150 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2151 | /* |
| 2152 | * even if we're attaching all tasks in the thread group, we |
| 2153 | * only need to check permissions on one of them. |
| 2154 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2155 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2156 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2157 | !uid_eq(cred->euid, tcred->uid) && |
| 2158 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2159 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2160 | ret = -EACCES; |
| 2161 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2162 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2163 | } else |
| 2164 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2165 | |
| 2166 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2167 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2168 | |
| 2169 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2170 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2171 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2172 | * with no rt_runtime allocated. Just say no. |
| 2173 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2174 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2175 | ret = -EINVAL; |
| 2176 | rcu_read_unlock(); |
| 2177 | goto out_unlock_cgroup; |
| 2178 | } |
| 2179 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2180 | get_task_struct(tsk); |
| 2181 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2182 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2183 | threadgroup_lock(tsk); |
| 2184 | if (threadgroup) { |
| 2185 | if (!thread_group_leader(tsk)) { |
| 2186 | /* |
| 2187 | * a race with de_thread from another thread's exec() |
| 2188 | * may strip us of our leadership, if this happens, |
| 2189 | * there is no choice but to throw this task away and |
| 2190 | * try again; this is |
| 2191 | * "double-double-toil-and-trouble-check locking". |
| 2192 | */ |
| 2193 | threadgroup_unlock(tsk); |
| 2194 | put_task_struct(tsk); |
| 2195 | goto retry_find_task; |
| 2196 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2200 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2201 | threadgroup_unlock(tsk); |
| 2202 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2203 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2204 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2205 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2206 | return ret; |
| 2207 | } |
| 2208 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2209 | /** |
| 2210 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2211 | * @from: attach to all cgroups of a given task |
| 2212 | * @tsk: the task to be attached |
| 2213 | */ |
| 2214 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2215 | { |
| 2216 | struct cgroupfs_root *root; |
| 2217 | int retval = 0; |
| 2218 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2219 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2220 | for_each_active_root(root) { |
| 2221 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2222 | |
| 2223 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2224 | if (retval) |
| 2225 | break; |
| 2226 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2227 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2228 | |
| 2229 | return retval; |
| 2230 | } |
| 2231 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2232 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2233 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2234 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2235 | return attach_task_by_pid(cgrp, pid, false); |
| 2236 | } |
| 2237 | |
| 2238 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2239 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2240 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2243 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2244 | const char *buffer) |
| 2245 | { |
| 2246 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2247 | if (strlen(buffer) >= PATH_MAX) |
| 2248 | return -EINVAL; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2249 | if (!cgroup_lock_live_group(cgrp)) |
| 2250 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2251 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2252 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2253 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2254 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2259 | struct seq_file *seq) |
| 2260 | { |
| 2261 | if (!cgroup_lock_live_group(cgrp)) |
| 2262 | return -ENODEV; |
| 2263 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2264 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2265 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2269 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2270 | struct seq_file *seq) |
| 2271 | { |
| 2272 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2273 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2276 | /* A buffer size big enough for numbers or short strings */ |
| 2277 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2278 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2279 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2280 | struct file *file, |
| 2281 | const char __user *userbuf, |
| 2282 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2283 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2284 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2285 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2286 | char *end; |
| 2287 | |
| 2288 | if (!nbytes) |
| 2289 | return -EINVAL; |
| 2290 | if (nbytes >= sizeof(buffer)) |
| 2291 | return -E2BIG; |
| 2292 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2293 | return -EFAULT; |
| 2294 | |
| 2295 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2296 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2297 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2298 | if (*end) |
| 2299 | return -EINVAL; |
| 2300 | retval = cft->write_u64(cgrp, cft, val); |
| 2301 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2302 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2303 | if (*end) |
| 2304 | return -EINVAL; |
| 2305 | retval = cft->write_s64(cgrp, cft, val); |
| 2306 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2307 | if (!retval) |
| 2308 | retval = nbytes; |
| 2309 | return retval; |
| 2310 | } |
| 2311 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2312 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2313 | struct file *file, |
| 2314 | const char __user *userbuf, |
| 2315 | size_t nbytes, loff_t *unused_ppos) |
| 2316 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2317 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2318 | int retval = 0; |
| 2319 | size_t max_bytes = cft->max_write_len; |
| 2320 | char *buffer = local_buffer; |
| 2321 | |
| 2322 | if (!max_bytes) |
| 2323 | max_bytes = sizeof(local_buffer) - 1; |
| 2324 | if (nbytes >= max_bytes) |
| 2325 | return -E2BIG; |
| 2326 | /* Allocate a dynamic buffer if we need one */ |
| 2327 | if (nbytes >= sizeof(local_buffer)) { |
| 2328 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2329 | if (buffer == NULL) |
| 2330 | return -ENOMEM; |
| 2331 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2332 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2333 | retval = -EFAULT; |
| 2334 | goto out; |
| 2335 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2336 | |
| 2337 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2338 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2339 | if (!retval) |
| 2340 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2341 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2342 | if (buffer != local_buffer) |
| 2343 | kfree(buffer); |
| 2344 | return retval; |
| 2345 | } |
| 2346 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2347 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2348 | size_t nbytes, loff_t *ppos) |
| 2349 | { |
| 2350 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2351 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2352 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2353 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2354 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2355 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2356 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2357 | if (cft->write_u64 || cft->write_s64) |
| 2358 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2359 | if (cft->write_string) |
| 2360 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2361 | if (cft->trigger) { |
| 2362 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2363 | return ret ? ret : nbytes; |
| 2364 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2365 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2368 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2369 | struct file *file, |
| 2370 | char __user *buf, size_t nbytes, |
| 2371 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2372 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2373 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2374 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2375 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2376 | |
| 2377 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2378 | } |
| 2379 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2380 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2381 | struct file *file, |
| 2382 | char __user *buf, size_t nbytes, |
| 2383 | loff_t *ppos) |
| 2384 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2385 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2386 | s64 val = cft->read_s64(cgrp, cft); |
| 2387 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2388 | |
| 2389 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2390 | } |
| 2391 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2392 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2393 | size_t nbytes, loff_t *ppos) |
| 2394 | { |
| 2395 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2396 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2397 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2398 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2399 | return -ENODEV; |
| 2400 | |
| 2401 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2402 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2403 | if (cft->read_u64) |
| 2404 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2405 | if (cft->read_s64) |
| 2406 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2407 | return -EINVAL; |
| 2408 | } |
| 2409 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2410 | /* |
| 2411 | * seqfile ops/methods for returning structured data. Currently just |
| 2412 | * supports string->u64 maps, but can be extended in future. |
| 2413 | */ |
| 2414 | |
| 2415 | struct cgroup_seqfile_state { |
| 2416 | struct cftype *cft; |
| 2417 | struct cgroup *cgroup; |
| 2418 | }; |
| 2419 | |
| 2420 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2421 | { |
| 2422 | struct seq_file *sf = cb->state; |
| 2423 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2424 | } |
| 2425 | |
| 2426 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2427 | { |
| 2428 | struct cgroup_seqfile_state *state = m->private; |
| 2429 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2430 | if (cft->read_map) { |
| 2431 | struct cgroup_map_cb cb = { |
| 2432 | .fill = cgroup_map_add, |
| 2433 | .state = m, |
| 2434 | }; |
| 2435 | return cft->read_map(state->cgroup, cft, &cb); |
| 2436 | } |
| 2437 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2440 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2441 | { |
| 2442 | struct seq_file *seq = file->private_data; |
| 2443 | kfree(seq->private); |
| 2444 | return single_release(inode, file); |
| 2445 | } |
| 2446 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2447 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2448 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2449 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2450 | .llseek = seq_lseek, |
| 2451 | .release = cgroup_seqfile_release, |
| 2452 | }; |
| 2453 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2454 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2455 | { |
| 2456 | int err; |
| 2457 | struct cftype *cft; |
| 2458 | |
| 2459 | err = generic_file_open(inode, file); |
| 2460 | if (err) |
| 2461 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2462 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2463 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2464 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2465 | struct cgroup_seqfile_state *state; |
| 2466 | |
| 2467 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2468 | if (!state) |
| 2469 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2470 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2471 | state->cft = cft; |
| 2472 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2473 | file->f_op = &cgroup_seqfile_operations; |
| 2474 | err = single_open(file, cgroup_seqfile_show, state); |
| 2475 | if (err < 0) |
| 2476 | kfree(state); |
| 2477 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2478 | err = cft->open(inode, file); |
| 2479 | else |
| 2480 | err = 0; |
| 2481 | |
| 2482 | return err; |
| 2483 | } |
| 2484 | |
| 2485 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2486 | { |
| 2487 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2488 | if (cft->release) |
| 2489 | return cft->release(inode, file); |
| 2490 | return 0; |
| 2491 | } |
| 2492 | |
| 2493 | /* |
| 2494 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2495 | */ |
| 2496 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2497 | struct inode *new_dir, struct dentry *new_dentry) |
| 2498 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2499 | int ret; |
| 2500 | struct cgroup_name *name, *old_name; |
| 2501 | struct cgroup *cgrp; |
| 2502 | |
| 2503 | /* |
| 2504 | * It's convinient to use parent dir's i_mutex to protected |
| 2505 | * cgrp->name. |
| 2506 | */ |
| 2507 | lockdep_assert_held(&old_dir->i_mutex); |
| 2508 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2509 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2510 | return -ENOTDIR; |
| 2511 | if (new_dentry->d_inode) |
| 2512 | return -EEXIST; |
| 2513 | if (old_dir != new_dir) |
| 2514 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2515 | |
| 2516 | cgrp = __d_cgrp(old_dentry); |
| 2517 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2518 | /* |
| 2519 | * This isn't a proper migration and its usefulness is very |
| 2520 | * limited. Disallow if sane_behavior. |
| 2521 | */ |
| 2522 | if (cgroup_sane_behavior(cgrp)) |
| 2523 | return -EPERM; |
| 2524 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 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 void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2787 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2788 | { |
| 2789 | /* |
| 2790 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2791 | * the existing cgroups under cgroup_mutex and create files. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2792 | * Instead, we use cgroup_for_each_descendant_pre() and drop RCU |
| 2793 | * read lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2794 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2795 | mutex_lock(&cgroup_mutex); |
| 2796 | } |
| 2797 | |
| 2798 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2799 | struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2800 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2801 | { |
| 2802 | LIST_HEAD(pending); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2803 | struct cgroup *cgrp, *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2804 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2805 | struct dentry *prev = NULL; |
| 2806 | struct inode *inode; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 2807 | u64 update_before; |
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 */ |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2810 | if (!cfts || ss->root == &rootnode || |
| 2811 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2812 | mutex_unlock(&cgroup_mutex); |
| 2813 | return; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2816 | /* |
| 2817 | * All cgroups which are created after we drop cgroup_mutex will |
| 2818 | * have the updated set of files, so we only need to update the |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 2819 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2820 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 2821 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2822 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2823 | mutex_unlock(&cgroup_mutex); |
| 2824 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2825 | /* @root always needs to be updated */ |
| 2826 | inode = root->dentry->d_inode; |
| 2827 | mutex_lock(&inode->i_mutex); |
| 2828 | mutex_lock(&cgroup_mutex); |
| 2829 | cgroup_addrm_files(root, ss, cfts, is_add); |
| 2830 | mutex_unlock(&cgroup_mutex); |
| 2831 | mutex_unlock(&inode->i_mutex); |
| 2832 | |
| 2833 | /* add/rm files for all cgroups created before */ |
| 2834 | rcu_read_lock(); |
| 2835 | cgroup_for_each_descendant_pre(cgrp, root) { |
| 2836 | if (cgroup_is_dead(cgrp)) |
| 2837 | continue; |
| 2838 | |
| 2839 | inode = cgrp->dentry->d_inode; |
| 2840 | dget(cgrp->dentry); |
| 2841 | rcu_read_unlock(); |
| 2842 | |
| 2843 | dput(prev); |
| 2844 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2845 | |
| 2846 | mutex_lock(&inode->i_mutex); |
| 2847 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 2848 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2849 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2850 | mutex_unlock(&cgroup_mutex); |
| 2851 | mutex_unlock(&inode->i_mutex); |
| 2852 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | rcu_read_lock(); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2854 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2855 | rcu_read_unlock(); |
| 2856 | dput(prev); |
| 2857 | deactivate_super(sb); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | /** |
| 2861 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2862 | * @ss: target cgroup subsystem |
| 2863 | * @cfts: zero-length name terminated array of cftypes |
| 2864 | * |
| 2865 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2866 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2867 | * have them too. This function can be called anytime whether @ss is |
| 2868 | * attached or not. |
| 2869 | * |
| 2870 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2871 | * function currently returns 0 as long as @cfts registration is successful |
| 2872 | * even if some file creation attempts on existing cgroups fail. |
| 2873 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2874 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2875 | { |
| 2876 | struct cftype_set *set; |
| 2877 | |
| 2878 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2879 | if (!set) |
| 2880 | return -ENOMEM; |
| 2881 | |
| 2882 | cgroup_cfts_prepare(); |
| 2883 | set->cfts = cfts; |
| 2884 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2885 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2886 | |
| 2887 | return 0; |
| 2888 | } |
| 2889 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2890 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2891 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2892 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2893 | * @ss: target cgroup subsystem |
| 2894 | * @cfts: zero-length name terminated array of cftypes |
| 2895 | * |
| 2896 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2897 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2898 | * won't have them either. This function can be called anytime whether @ss |
| 2899 | * is attached or not. |
| 2900 | * |
| 2901 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2902 | * registered with @ss. |
| 2903 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2904 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2905 | { |
| 2906 | struct cftype_set *set; |
| 2907 | |
| 2908 | cgroup_cfts_prepare(); |
| 2909 | |
| 2910 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2911 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2912 | list_del(&set->node); |
| 2913 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2914 | cgroup_cfts_commit(ss, cfts, false); |
| 2915 | return 0; |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | cgroup_cfts_commit(ss, NULL, false); |
| 2920 | return -ENOENT; |
| 2921 | } |
| 2922 | |
| 2923 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2924 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2925 | * @cgrp: the cgroup in question |
| 2926 | * |
| 2927 | * Return the number of tasks in the cgroup. |
| 2928 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2929 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2930 | { |
| 2931 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2932 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2933 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2934 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2935 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2936 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2937 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2938 | return count; |
| 2939 | } |
| 2940 | |
| 2941 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2942 | * Advance a list_head iterator. The iterator should be positioned at |
| 2943 | * the start of a css_set |
| 2944 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2945 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2946 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2947 | struct list_head *l = it->cset_link; |
| 2948 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2949 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2950 | |
| 2951 | /* Advance to the next non-empty css_set */ |
| 2952 | do { |
| 2953 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2954 | if (l == &cgrp->cset_links) { |
| 2955 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2956 | return; |
| 2957 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2958 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2959 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2960 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2961 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2962 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2965 | /* |
| 2966 | * To reduce the fork() overhead for systems that are not actually |
| 2967 | * using their cgroups capability, we don't maintain the lists running |
| 2968 | * through each css_set to its tasks until we see the list actually |
| 2969 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2970 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2971 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2972 | { |
| 2973 | struct task_struct *p, *g; |
| 2974 | write_lock(&css_set_lock); |
| 2975 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2976 | /* |
| 2977 | * We need tasklist_lock because RCU is not safe against |
| 2978 | * while_each_thread(). Besides, a forking task that has passed |
| 2979 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2980 | * is not guaranteed to have its child immediately visible in the |
| 2981 | * tasklist if we walk through it with RCU. |
| 2982 | */ |
| 2983 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2984 | do_each_thread(g, p) { |
| 2985 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2986 | /* |
| 2987 | * We should check if the process is exiting, otherwise |
| 2988 | * it will race with cgroup_exit() in that the list |
| 2989 | * entry won't be deleted though the process has exited. |
| 2990 | */ |
| 2991 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2992 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2993 | task_unlock(p); |
| 2994 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2995 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2996 | write_unlock(&css_set_lock); |
| 2997 | } |
| 2998 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2999 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3000 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 3001 | * @pos: the current cgroup |
| 3002 | * |
| 3003 | * This function returns the next sibling of @pos and should be called |
| 3004 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 3005 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 3006 | * state. |
| 3007 | */ |
| 3008 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 3009 | { |
| 3010 | struct cgroup *next; |
| 3011 | |
| 3012 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3013 | |
| 3014 | /* |
| 3015 | * @pos could already have been removed. Once a cgroup is removed, |
| 3016 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3017 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3018 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3019 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3020 | * finished its grace period even if it's already removed, and thus |
| 3021 | * safe to dereference from this RCU critical section. If |
| 3022 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3023 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3024 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3025 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3026 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3027 | if (&next->sibling != &pos->parent->children) |
| 3028 | return next; |
| 3029 | return NULL; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * Can't dereference the next pointer. Each cgroup is given a |
| 3034 | * monotonically increasing unique serial number and always |
| 3035 | * appended to the sibling list, so the next one can be found by |
| 3036 | * walking the parent's children until we see a cgroup with higher |
| 3037 | * serial number than @pos's. |
| 3038 | * |
| 3039 | * While this path can be slow, it's taken only when either the |
| 3040 | * current cgroup is removed or iteration and removal race. |
| 3041 | */ |
| 3042 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3043 | if (next->serial_nr > pos->serial_nr) |
| 3044 | return next; |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3048 | |
| 3049 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3050 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3051 | * @pos: the current position (%NULL to initiate traversal) |
| 3052 | * @cgroup: cgroup whose descendants to walk |
| 3053 | * |
| 3054 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3055 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3056 | * |
| 3057 | * While this function requires RCU read locking, it doesn't require the |
| 3058 | * whole traversal to be contained in a single RCU critical section. This |
| 3059 | * function will return the correct next descendant as long as both @pos |
| 3060 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3061 | */ |
| 3062 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3063 | struct cgroup *cgroup) |
| 3064 | { |
| 3065 | struct cgroup *next; |
| 3066 | |
| 3067 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3068 | |
| 3069 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3070 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3071 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3072 | |
| 3073 | /* visit the first child if exists */ |
| 3074 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3075 | if (next) |
| 3076 | return next; |
| 3077 | |
| 3078 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3079 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3080 | next = cgroup_next_sibling(pos); |
| 3081 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3082 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3083 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3084 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3085 | |
| 3086 | return NULL; |
| 3087 | } |
| 3088 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3089 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3090 | /** |
| 3091 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3092 | * @pos: cgroup of interest |
| 3093 | * |
| 3094 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3095 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3096 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3097 | * |
| 3098 | * While this function requires RCU read locking, it doesn't require the |
| 3099 | * whole traversal to be contained in a single RCU critical section. This |
| 3100 | * function will return the correct rightmost descendant as long as @pos is |
| 3101 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3102 | */ |
| 3103 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3104 | { |
| 3105 | struct cgroup *last, *tmp; |
| 3106 | |
| 3107 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3108 | |
| 3109 | do { |
| 3110 | last = pos; |
| 3111 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3112 | pos = NULL; |
| 3113 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3114 | pos = tmp; |
| 3115 | } while (pos); |
| 3116 | |
| 3117 | return last; |
| 3118 | } |
| 3119 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3120 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3121 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3122 | { |
| 3123 | struct cgroup *last; |
| 3124 | |
| 3125 | do { |
| 3126 | last = pos; |
| 3127 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3128 | sibling); |
| 3129 | } while (pos); |
| 3130 | |
| 3131 | return last; |
| 3132 | } |
| 3133 | |
| 3134 | /** |
| 3135 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3136 | * @pos: the current position (%NULL to initiate traversal) |
| 3137 | * @cgroup: cgroup whose descendants to walk |
| 3138 | * |
| 3139 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3140 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3141 | * |
| 3142 | * While this function requires RCU read locking, it doesn't require the |
| 3143 | * whole traversal to be contained in a single RCU critical section. This |
| 3144 | * function will return the correct next descendant as long as both @pos |
| 3145 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3146 | */ |
| 3147 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3148 | struct cgroup *cgroup) |
| 3149 | { |
| 3150 | struct cgroup *next; |
| 3151 | |
| 3152 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3153 | |
| 3154 | /* if first iteration, visit the leftmost descendant */ |
| 3155 | if (!pos) { |
| 3156 | next = cgroup_leftmost_descendant(cgroup); |
| 3157 | return next != cgroup ? next : NULL; |
| 3158 | } |
| 3159 | |
| 3160 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3161 | next = cgroup_next_sibling(pos); |
| 3162 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3163 | return cgroup_leftmost_descendant(next); |
| 3164 | |
| 3165 | /* no sibling left, visit parent */ |
| 3166 | next = pos->parent; |
| 3167 | return next != cgroup ? next : NULL; |
| 3168 | } |
| 3169 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3170 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3171 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3172 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3173 | { |
| 3174 | /* |
| 3175 | * The first time anyone tries to iterate across a cgroup, |
| 3176 | * we need to enable the list linking each css_set to its |
| 3177 | * tasks, and fix up all existing tasks. |
| 3178 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3179 | if (!use_task_css_set_links) |
| 3180 | cgroup_enable_task_cg_lists(); |
| 3181 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3182 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3183 | it->cset_link = &cgrp->cset_links; |
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 | } |
| 3186 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3187 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3188 | struct cgroup_iter *it) |
| 3189 | { |
| 3190 | struct task_struct *res; |
| 3191 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3192 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3193 | |
| 3194 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3195 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3196 | return NULL; |
| 3197 | res = list_entry(l, struct task_struct, cg_list); |
| 3198 | /* Advance iterator to find next entry */ |
| 3199 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3200 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3201 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3202 | /* We reached the end of this task list - move on to |
| 3203 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3204 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3205 | } else { |
| 3206 | it->task = l; |
| 3207 | } |
| 3208 | return res; |
| 3209 | } |
| 3210 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3211 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3212 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3213 | { |
| 3214 | read_unlock(&css_set_lock); |
| 3215 | } |
| 3216 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3217 | static inline int started_after_time(struct task_struct *t1, |
| 3218 | struct timespec *time, |
| 3219 | struct task_struct *t2) |
| 3220 | { |
| 3221 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3222 | if (start_diff > 0) { |
| 3223 | return 1; |
| 3224 | } else if (start_diff < 0) { |
| 3225 | return 0; |
| 3226 | } else { |
| 3227 | /* |
| 3228 | * Arbitrarily, if two processes started at the same |
| 3229 | * time, we'll say that the lower pointer value |
| 3230 | * started first. Note that t2 may have exited by now |
| 3231 | * so this may not be a valid pointer any longer, but |
| 3232 | * that's fine - it still serves to distinguish |
| 3233 | * between two tasks started (effectively) simultaneously. |
| 3234 | */ |
| 3235 | return t1 > t2; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | /* |
| 3240 | * This function is a callback from heap_insert() and is used to order |
| 3241 | * the heap. |
| 3242 | * In this case we order the heap in descending task start time. |
| 3243 | */ |
| 3244 | static inline int started_after(void *p1, void *p2) |
| 3245 | { |
| 3246 | struct task_struct *t1 = p1; |
| 3247 | struct task_struct *t2 = p2; |
| 3248 | return started_after_time(t1, &t2->start_time, t2); |
| 3249 | } |
| 3250 | |
| 3251 | /** |
| 3252 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3253 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3254 | * |
| 3255 | * Arguments include pointers to callback functions test_task() and |
| 3256 | * process_task(). |
| 3257 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3258 | * and if it returns true, call process_task() for it also. |
| 3259 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3260 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3261 | * but does not lock css_set_lock for the call to process_task(). |
| 3262 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3263 | * creation. |
| 3264 | * It is guaranteed that process_task() will act on every task that |
| 3265 | * is a member of the cgroup for the duration of this call. This |
| 3266 | * function may or may not call process_task() for tasks that exit |
| 3267 | * or move to a different cgroup during the call, or are forked or |
| 3268 | * move into the cgroup during the call. |
| 3269 | * |
| 3270 | * Note that test_task() may be called with locks held, and may in some |
| 3271 | * situations be called multiple times for the same task, so it should |
| 3272 | * be cheap. |
| 3273 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3274 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3275 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3276 | * may cause this function to fail). |
| 3277 | */ |
| 3278 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3279 | { |
| 3280 | int retval, i; |
| 3281 | struct cgroup_iter it; |
| 3282 | struct task_struct *p, *dropped; |
| 3283 | /* Never dereference latest_task, since it's not refcounted */ |
| 3284 | struct task_struct *latest_task = NULL; |
| 3285 | struct ptr_heap tmp_heap; |
| 3286 | struct ptr_heap *heap; |
| 3287 | struct timespec latest_time = { 0, 0 }; |
| 3288 | |
| 3289 | if (scan->heap) { |
| 3290 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3291 | heap = scan->heap; |
| 3292 | heap->gt = &started_after; |
| 3293 | } else { |
| 3294 | /* We need to allocate our own heap memory */ |
| 3295 | heap = &tmp_heap; |
| 3296 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3297 | if (retval) |
| 3298 | /* cannot allocate the heap */ |
| 3299 | return retval; |
| 3300 | } |
| 3301 | |
| 3302 | again: |
| 3303 | /* |
| 3304 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3305 | * to determine which are of interest, and using the scanner's |
| 3306 | * "process_task" callback to process any of them that need an update. |
| 3307 | * Since we don't want to hold any locks during the task updates, |
| 3308 | * gather tasks to be processed in a heap structure. |
| 3309 | * The heap is sorted by descending task start time. |
| 3310 | * If the statically-sized heap fills up, we overflow tasks that |
| 3311 | * started later, and in future iterations only consider tasks that |
| 3312 | * started after the latest task in the previous pass. This |
| 3313 | * guarantees forward progress and that we don't miss any tasks. |
| 3314 | */ |
| 3315 | heap->size = 0; |
| 3316 | cgroup_iter_start(scan->cg, &it); |
| 3317 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3318 | /* |
| 3319 | * Only affect tasks that qualify per the caller's callback, |
| 3320 | * if he provided one |
| 3321 | */ |
| 3322 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3323 | continue; |
| 3324 | /* |
| 3325 | * Only process tasks that started after the last task |
| 3326 | * we processed |
| 3327 | */ |
| 3328 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3329 | continue; |
| 3330 | dropped = heap_insert(heap, p); |
| 3331 | if (dropped == NULL) { |
| 3332 | /* |
| 3333 | * The new task was inserted; the heap wasn't |
| 3334 | * previously full |
| 3335 | */ |
| 3336 | get_task_struct(p); |
| 3337 | } else if (dropped != p) { |
| 3338 | /* |
| 3339 | * The new task was inserted, and pushed out a |
| 3340 | * different task |
| 3341 | */ |
| 3342 | get_task_struct(p); |
| 3343 | put_task_struct(dropped); |
| 3344 | } |
| 3345 | /* |
| 3346 | * Else the new task was newer than anything already in |
| 3347 | * the heap and wasn't inserted |
| 3348 | */ |
| 3349 | } |
| 3350 | cgroup_iter_end(scan->cg, &it); |
| 3351 | |
| 3352 | if (heap->size) { |
| 3353 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3354 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3355 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3356 | latest_time = q->start_time; |
| 3357 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3358 | } |
| 3359 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3360 | scan->process_task(q, scan); |
| 3361 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3362 | } |
| 3363 | /* |
| 3364 | * If we had to process any tasks at all, scan again |
| 3365 | * in case some of them were in the middle of forking |
| 3366 | * children that didn't get processed. |
| 3367 | * Not the most efficient way to do it, but it avoids |
| 3368 | * having to take callback_mutex in the fork path |
| 3369 | */ |
| 3370 | goto again; |
| 3371 | } |
| 3372 | if (heap == &tmp_heap) |
| 3373 | heap_free(&tmp_heap); |
| 3374 | return 0; |
| 3375 | } |
| 3376 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3377 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3378 | struct cgroup_scanner *scan) |
| 3379 | { |
| 3380 | struct cgroup *new_cgroup = scan->data; |
| 3381 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3382 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3383 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3384 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | /** |
| 3388 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3389 | * @to: cgroup to which the tasks will be moved |
| 3390 | * @from: cgroup in which the tasks currently reside |
| 3391 | */ |
| 3392 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3393 | { |
| 3394 | struct cgroup_scanner scan; |
| 3395 | |
| 3396 | scan.cg = from; |
| 3397 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3398 | scan.process_task = cgroup_transfer_one_task; |
| 3399 | scan.heap = NULL; |
| 3400 | scan.data = to; |
| 3401 | |
| 3402 | return cgroup_scan_tasks(&scan); |
| 3403 | } |
| 3404 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3405 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3406 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3407 | * |
| 3408 | * Reading this file can return large amounts of data if a cgroup has |
| 3409 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3410 | * but we cannot guarantee that the information we produce is correct |
| 3411 | * unless we produce it entirely atomically. |
| 3412 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3413 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3414 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3415 | /* which pidlist file are we talking about? */ |
| 3416 | enum cgroup_filetype { |
| 3417 | CGROUP_FILE_PROCS, |
| 3418 | CGROUP_FILE_TASKS, |
| 3419 | }; |
| 3420 | |
| 3421 | /* |
| 3422 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3423 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3424 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3425 | * to the cgroup. |
| 3426 | */ |
| 3427 | struct cgroup_pidlist { |
| 3428 | /* |
| 3429 | * used to find which pidlist is wanted. doesn't change as long as |
| 3430 | * this particular list stays in the list. |
| 3431 | */ |
| 3432 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3433 | /* array of xids */ |
| 3434 | pid_t *list; |
| 3435 | /* how many elements the above list has */ |
| 3436 | int length; |
| 3437 | /* how many files are using the current array */ |
| 3438 | int use_count; |
| 3439 | /* each of these stored in a list by its cgroup */ |
| 3440 | struct list_head links; |
| 3441 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3442 | struct cgroup *owner; |
| 3443 | /* protects the other fields */ |
| 3444 | struct rw_semaphore mutex; |
| 3445 | }; |
| 3446 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3447 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3448 | * The following two functions "fix" the issue where there are more pids |
| 3449 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3450 | * TODO: replace with a kernel-wide solution to this problem |
| 3451 | */ |
| 3452 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3453 | static void *pidlist_allocate(int count) |
| 3454 | { |
| 3455 | if (PIDLIST_TOO_LARGE(count)) |
| 3456 | return vmalloc(count * sizeof(pid_t)); |
| 3457 | else |
| 3458 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3459 | } |
| 3460 | static void pidlist_free(void *p) |
| 3461 | { |
| 3462 | if (is_vmalloc_addr(p)) |
| 3463 | vfree(p); |
| 3464 | else |
| 3465 | kfree(p); |
| 3466 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3467 | |
| 3468 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3469 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3470 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3471 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3472 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3473 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3474 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3475 | |
| 3476 | /* |
| 3477 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3478 | * edge cases first; no work needs to be done for either |
| 3479 | */ |
| 3480 | if (length == 0 || length == 1) |
| 3481 | return length; |
| 3482 | /* src and dest walk down the list; dest counts unique elements */ |
| 3483 | for (src = 1; src < length; src++) { |
| 3484 | /* find next unique element */ |
| 3485 | while (list[src] == list[src-1]) { |
| 3486 | src++; |
| 3487 | if (src == length) |
| 3488 | goto after; |
| 3489 | } |
| 3490 | /* dest always points to where the next unique element goes */ |
| 3491 | list[dest] = list[src]; |
| 3492 | dest++; |
| 3493 | } |
| 3494 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3495 | return dest; |
| 3496 | } |
| 3497 | |
| 3498 | static int cmppid(const void *a, const void *b) |
| 3499 | { |
| 3500 | return *(pid_t *)a - *(pid_t *)b; |
| 3501 | } |
| 3502 | |
| 3503 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3504 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3505 | * returns with the lock on that pidlist already held, and takes care |
| 3506 | * of the use count, or returns NULL with no locks held if we're out of |
| 3507 | * memory. |
| 3508 | */ |
| 3509 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3510 | enum cgroup_filetype type) |
| 3511 | { |
| 3512 | struct cgroup_pidlist *l; |
| 3513 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3514 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3515 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3516 | /* |
| 3517 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3518 | * the last ref-holder is trying to remove l from the list at the same |
| 3519 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3520 | * list we find out from under us - compare release_pid_array(). |
| 3521 | */ |
| 3522 | mutex_lock(&cgrp->pidlist_mutex); |
| 3523 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3524 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3525 | /* make sure l doesn't vanish out from under us */ |
| 3526 | down_write(&l->mutex); |
| 3527 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3528 | return l; |
| 3529 | } |
| 3530 | } |
| 3531 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3532 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3533 | if (!l) { |
| 3534 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3535 | return l; |
| 3536 | } |
| 3537 | init_rwsem(&l->mutex); |
| 3538 | down_write(&l->mutex); |
| 3539 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3540 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3541 | l->owner = cgrp; |
| 3542 | list_add(&l->links, &cgrp->pidlists); |
| 3543 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3544 | return l; |
| 3545 | } |
| 3546 | |
| 3547 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3548 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3549 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3550 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3551 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3552 | { |
| 3553 | pid_t *array; |
| 3554 | int length; |
| 3555 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3556 | struct cgroup_iter it; |
| 3557 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3558 | struct cgroup_pidlist *l; |
| 3559 | |
| 3560 | /* |
| 3561 | * If cgroup gets more users after we read count, we won't have |
| 3562 | * enough space - tough. This race is indistinguishable to the |
| 3563 | * caller from the case that the additional cgroup users didn't |
| 3564 | * show up until sometime later on. |
| 3565 | */ |
| 3566 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3567 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3568 | if (!array) |
| 3569 | return -ENOMEM; |
| 3570 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3571 | cgroup_iter_start(cgrp, &it); |
| 3572 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3573 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3574 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3576 | if (type == CGROUP_FILE_PROCS) |
| 3577 | pid = task_tgid_vnr(tsk); |
| 3578 | else |
| 3579 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3580 | if (pid > 0) /* make sure to only use valid results */ |
| 3581 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3582 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3583 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3584 | length = n; |
| 3585 | /* now sort & (if procs) strip out duplicates */ |
| 3586 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3587 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3588 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3589 | l = cgroup_pidlist_find(cgrp, type); |
| 3590 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3591 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3592 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3593 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3594 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3595 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3596 | l->list = array; |
| 3597 | l->length = length; |
| 3598 | l->use_count++; |
| 3599 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3600 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3601 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3604 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3605 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3606 | * @stats: cgroupstats to fill information into |
| 3607 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3608 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3609 | * |
| 3610 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3611 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3612 | */ |
| 3613 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3614 | { |
| 3615 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3616 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3617 | struct cgroup_iter it; |
| 3618 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3619 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3620 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3621 | * Validate dentry by checking the superblock operations, |
| 3622 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3623 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3624 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3625 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3626 | goto err; |
| 3627 | |
| 3628 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3629 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3630 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3631 | cgroup_iter_start(cgrp, &it); |
| 3632 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3633 | switch (tsk->state) { |
| 3634 | case TASK_RUNNING: |
| 3635 | stats->nr_running++; |
| 3636 | break; |
| 3637 | case TASK_INTERRUPTIBLE: |
| 3638 | stats->nr_sleeping++; |
| 3639 | break; |
| 3640 | case TASK_UNINTERRUPTIBLE: |
| 3641 | stats->nr_uninterruptible++; |
| 3642 | break; |
| 3643 | case TASK_STOPPED: |
| 3644 | stats->nr_stopped++; |
| 3645 | break; |
| 3646 | default: |
| 3647 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3648 | stats->nr_io_wait++; |
| 3649 | break; |
| 3650 | } |
| 3651 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3652 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3653 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3654 | err: |
| 3655 | return ret; |
| 3656 | } |
| 3657 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3658 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3659 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3660 | * 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] | 3661 | * 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] | 3662 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | */ |
| 3664 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3665 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3666 | { |
| 3667 | /* |
| 3668 | * Initially we receive a position value that corresponds to |
| 3669 | * one more than the last pid shown (or 0 on the first call or |
| 3670 | * after a seek to the start). Use a binary-search to find the |
| 3671 | * next pid to display, if any |
| 3672 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3673 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3674 | int index = 0, pid = *pos; |
| 3675 | int *iter; |
| 3676 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3677 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3678 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3679 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3680 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3681 | while (index < end) { |
| 3682 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3683 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3684 | index = mid; |
| 3685 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3686 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3687 | index = mid + 1; |
| 3688 | else |
| 3689 | end = mid; |
| 3690 | } |
| 3691 | } |
| 3692 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3693 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3694 | return NULL; |
| 3695 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3696 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3697 | *pos = *iter; |
| 3698 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3699 | } |
| 3700 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3701 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3702 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3703 | struct cgroup_pidlist *l = s->private; |
| 3704 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3705 | } |
| 3706 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3707 | 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] | 3708 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | struct cgroup_pidlist *l = s->private; |
| 3710 | pid_t *p = v; |
| 3711 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3712 | /* |
| 3713 | * Advance to the next pid in the array. If this goes off the |
| 3714 | * end, we're done |
| 3715 | */ |
| 3716 | p++; |
| 3717 | if (p >= end) { |
| 3718 | return NULL; |
| 3719 | } else { |
| 3720 | *pos = *p; |
| 3721 | return p; |
| 3722 | } |
| 3723 | } |
| 3724 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3725 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3726 | { |
| 3727 | return seq_printf(s, "%d\n", *(int *)v); |
| 3728 | } |
| 3729 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3730 | /* |
| 3731 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3732 | * independent of whether it's tasks or procs |
| 3733 | */ |
| 3734 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3735 | .start = cgroup_pidlist_start, |
| 3736 | .stop = cgroup_pidlist_stop, |
| 3737 | .next = cgroup_pidlist_next, |
| 3738 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3739 | }; |
| 3740 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3741 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3742 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3743 | /* |
| 3744 | * the case where we're the last user of this particular pidlist will |
| 3745 | * have us remove it from the cgroup's list, which entails taking the |
| 3746 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3747 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3748 | */ |
| 3749 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3750 | down_write(&l->mutex); |
| 3751 | BUG_ON(!l->use_count); |
| 3752 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3753 | /* we're the last user if refcount is 0; remove and free */ |
| 3754 | list_del(&l->links); |
| 3755 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3756 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3757 | put_pid_ns(l->key.ns); |
| 3758 | up_write(&l->mutex); |
| 3759 | kfree(l); |
| 3760 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3761 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3762 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3763 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3766 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3767 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3768 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3769 | if (!(file->f_mode & FMODE_READ)) |
| 3770 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3771 | /* |
| 3772 | * the seq_file will only be initialized if the file was opened for |
| 3773 | * reading; hence we check if it's not null only in that case. |
| 3774 | */ |
| 3775 | l = ((struct seq_file *)file->private_data)->private; |
| 3776 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3777 | return seq_release(inode, file); |
| 3778 | } |
| 3779 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3780 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3781 | .read = seq_read, |
| 3782 | .llseek = seq_lseek, |
| 3783 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3784 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3785 | }; |
| 3786 | |
| 3787 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3788 | * The following functions handle opens on a file that displays a pidlist |
| 3789 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3790 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3791 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3792 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3793 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3794 | { |
| 3795 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3796 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3797 | int retval; |
| 3798 | |
| 3799 | /* Nothing to do for write-only files */ |
| 3800 | if (!(file->f_mode & FMODE_READ)) |
| 3801 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3802 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3803 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3804 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3805 | if (retval) |
| 3806 | return retval; |
| 3807 | /* configure file information */ |
| 3808 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3809 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3810 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3811 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3812 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3813 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3814 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3815 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3816 | return 0; |
| 3817 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3818 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3819 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3820 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3821 | } |
| 3822 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3823 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3824 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3825 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3826 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3827 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3828 | struct cftype *cft) |
| 3829 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3830 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3833 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3834 | struct cftype *cft, |
| 3835 | u64 val) |
| 3836 | { |
| 3837 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3838 | if (val) |
| 3839 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3840 | else |
| 3841 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3842 | return 0; |
| 3843 | } |
| 3844 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3845 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3846 | * When dput() is called asynchronously, if umount has been done and |
| 3847 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3848 | * there's a small window that vfs will see the root dentry with non-zero |
| 3849 | * refcnt and trigger BUG(). |
| 3850 | * |
| 3851 | * That's why we hold a reference before dput() and drop it right after. |
| 3852 | */ |
| 3853 | static void cgroup_dput(struct cgroup *cgrp) |
| 3854 | { |
| 3855 | struct super_block *sb = cgrp->root->sb; |
| 3856 | |
| 3857 | atomic_inc(&sb->s_active); |
| 3858 | dput(cgrp->dentry); |
| 3859 | deactivate_super(sb); |
| 3860 | } |
| 3861 | |
| 3862 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3863 | * Unregister event and free resources. |
| 3864 | * |
| 3865 | * Gets called from workqueue. |
| 3866 | */ |
| 3867 | static void cgroup_event_remove(struct work_struct *work) |
| 3868 | { |
| 3869 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3870 | remove); |
| 3871 | struct cgroup *cgrp = event->cgrp; |
| 3872 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3873 | remove_wait_queue(event->wqh, &event->wait); |
| 3874 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3875 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3876 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3877 | /* Notify userspace the event is going away. */ |
| 3878 | eventfd_signal(event->eventfd, 1); |
| 3879 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3880 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3881 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3882 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | /* |
| 3886 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3887 | * |
| 3888 | * Called with wqh->lock held and interrupts disabled. |
| 3889 | */ |
| 3890 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3891 | int sync, void *key) |
| 3892 | { |
| 3893 | struct cgroup_event *event = container_of(wait, |
| 3894 | struct cgroup_event, wait); |
| 3895 | struct cgroup *cgrp = event->cgrp; |
| 3896 | unsigned long flags = (unsigned long)key; |
| 3897 | |
| 3898 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3899 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3900 | * If the event has been detached at cgroup removal, we |
| 3901 | * can simply return knowing the other side will cleanup |
| 3902 | * for us. |
| 3903 | * |
| 3904 | * We can't race against event freeing since the other |
| 3905 | * side will require wqh->lock via remove_wait_queue(), |
| 3906 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3907 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3908 | spin_lock(&cgrp->event_list_lock); |
| 3909 | if (!list_empty(&event->list)) { |
| 3910 | list_del_init(&event->list); |
| 3911 | /* |
| 3912 | * We are in atomic context, but cgroup_event_remove() |
| 3913 | * may sleep, so we have to call it in workqueue. |
| 3914 | */ |
| 3915 | schedule_work(&event->remove); |
| 3916 | } |
| 3917 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
| 3923 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3924 | wait_queue_head_t *wqh, poll_table *pt) |
| 3925 | { |
| 3926 | struct cgroup_event *event = container_of(pt, |
| 3927 | struct cgroup_event, pt); |
| 3928 | |
| 3929 | event->wqh = wqh; |
| 3930 | add_wait_queue(wqh, &event->wait); |
| 3931 | } |
| 3932 | |
| 3933 | /* |
| 3934 | * Parse input and register new cgroup event handler. |
| 3935 | * |
| 3936 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3937 | * Interpretation of args is defined by control file implementation. |
| 3938 | */ |
| 3939 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3940 | const char *buffer) |
| 3941 | { |
| 3942 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3943 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3944 | unsigned int efd, cfd; |
| 3945 | struct file *efile = NULL; |
| 3946 | struct file *cfile = NULL; |
| 3947 | char *endp; |
| 3948 | int ret; |
| 3949 | |
| 3950 | efd = simple_strtoul(buffer, &endp, 10); |
| 3951 | if (*endp != ' ') |
| 3952 | return -EINVAL; |
| 3953 | buffer = endp + 1; |
| 3954 | |
| 3955 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3956 | if ((*endp != ' ') && (*endp != '\0')) |
| 3957 | return -EINVAL; |
| 3958 | buffer = endp + 1; |
| 3959 | |
| 3960 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3961 | if (!event) |
| 3962 | return -ENOMEM; |
| 3963 | event->cgrp = cgrp; |
| 3964 | INIT_LIST_HEAD(&event->list); |
| 3965 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3966 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3967 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3968 | |
| 3969 | efile = eventfd_fget(efd); |
| 3970 | if (IS_ERR(efile)) { |
| 3971 | ret = PTR_ERR(efile); |
| 3972 | goto fail; |
| 3973 | } |
| 3974 | |
| 3975 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3976 | if (IS_ERR(event->eventfd)) { |
| 3977 | ret = PTR_ERR(event->eventfd); |
| 3978 | goto fail; |
| 3979 | } |
| 3980 | |
| 3981 | cfile = fget(cfd); |
| 3982 | if (!cfile) { |
| 3983 | ret = -EBADF; |
| 3984 | goto fail; |
| 3985 | } |
| 3986 | |
| 3987 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3988 | /* 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] | 3989 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3990 | if (ret < 0) |
| 3991 | goto fail; |
| 3992 | |
| 3993 | event->cft = __file_cft(cfile); |
| 3994 | if (IS_ERR(event->cft)) { |
| 3995 | ret = PTR_ERR(event->cft); |
| 3996 | goto fail; |
| 3997 | } |
| 3998 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3999 | /* |
| 4000 | * The file to be monitored must be in the same cgroup as |
| 4001 | * cgroup.event_control is. |
| 4002 | */ |
| 4003 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4004 | if (cgrp_cfile != cgrp) { |
| 4005 | ret = -EINVAL; |
| 4006 | goto fail; |
| 4007 | } |
| 4008 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4009 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4010 | ret = -EINVAL; |
| 4011 | goto fail; |
| 4012 | } |
| 4013 | |
| 4014 | ret = event->cft->register_event(cgrp, event->cft, |
| 4015 | event->eventfd, buffer); |
| 4016 | if (ret) |
| 4017 | goto fail; |
| 4018 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4019 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4020 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4021 | /* |
| 4022 | * Events should be removed after rmdir of cgroup directory, but before |
| 4023 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4024 | * directory dentry to do that. |
| 4025 | */ |
| 4026 | dget(cgrp->dentry); |
| 4027 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4028 | spin_lock(&cgrp->event_list_lock); |
| 4029 | list_add(&event->list, &cgrp->event_list); |
| 4030 | spin_unlock(&cgrp->event_list_lock); |
| 4031 | |
| 4032 | fput(cfile); |
| 4033 | fput(efile); |
| 4034 | |
| 4035 | return 0; |
| 4036 | |
| 4037 | fail: |
| 4038 | if (cfile) |
| 4039 | fput(cfile); |
| 4040 | |
| 4041 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4042 | eventfd_ctx_put(event->eventfd); |
| 4043 | |
| 4044 | if (!IS_ERR_OR_NULL(efile)) |
| 4045 | fput(efile); |
| 4046 | |
| 4047 | kfree(event); |
| 4048 | |
| 4049 | return ret; |
| 4050 | } |
| 4051 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4052 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4053 | struct cftype *cft) |
| 4054 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4055 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4059 | struct cftype *cft, |
| 4060 | u64 val) |
| 4061 | { |
| 4062 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4063 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4064 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4065 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4066 | return 0; |
| 4067 | } |
| 4068 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4069 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4070 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4071 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4072 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4073 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4074 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4075 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4076 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4077 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4078 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4079 | .write_string = cgroup_write_event_control, |
| 4080 | .mode = S_IWUGO, |
| 4081 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4082 | { |
| 4083 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4084 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4085 | .read_u64 = cgroup_clone_children_read, |
| 4086 | .write_u64 = cgroup_clone_children_write, |
| 4087 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4088 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4089 | .name = "cgroup.sane_behavior", |
| 4090 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4091 | .read_seq_string = cgroup_sane_behavior_show, |
| 4092 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4093 | |
| 4094 | /* |
| 4095 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4096 | * don't exist if sane_behavior. If you're depending on these, be |
| 4097 | * prepared to be burned. |
| 4098 | */ |
| 4099 | { |
| 4100 | .name = "tasks", |
| 4101 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4102 | .open = cgroup_tasks_open, |
| 4103 | .write_u64 = cgroup_tasks_write, |
| 4104 | .release = cgroup_pidlist_release, |
| 4105 | .mode = S_IRUGO | S_IWUSR, |
| 4106 | }, |
| 4107 | { |
| 4108 | .name = "notify_on_release", |
| 4109 | .flags = CFTYPE_INSANE, |
| 4110 | .read_u64 = cgroup_read_notify_on_release, |
| 4111 | .write_u64 = cgroup_write_notify_on_release, |
| 4112 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4113 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4114 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4115 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4116 | .read_seq_string = cgroup_release_agent_show, |
| 4117 | .write_string = cgroup_release_agent_write, |
| 4118 | .max_write_len = PATH_MAX, |
| 4119 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4120 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4121 | }; |
| 4122 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4123 | /** |
| 4124 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4125 | * @cgrp: target cgroup |
| 4126 | * @base_files: true if the base files should be added |
| 4127 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4128 | */ |
| 4129 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4130 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4131 | { |
| 4132 | int err; |
| 4133 | struct cgroup_subsys *ss; |
| 4134 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4135 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4136 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4137 | if (err < 0) |
| 4138 | return err; |
| 4139 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4140 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4141 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4142 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4143 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4144 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4145 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4146 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4147 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4148 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4149 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4150 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4151 | /* This cgroup is ready now */ |
| 4152 | for_each_subsys(cgrp->root, ss) { |
| 4153 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4154 | /* |
| 4155 | * Update id->css pointer and make this css visible from |
| 4156 | * CSS ID functions. This pointer will be dereferened |
| 4157 | * from RCU-read-side without locks. |
| 4158 | */ |
| 4159 | if (css->id) |
| 4160 | rcu_assign_pointer(css->id->css, css); |
| 4161 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4162 | |
| 4163 | return 0; |
| 4164 | } |
| 4165 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4166 | static void css_dput_fn(struct work_struct *work) |
| 4167 | { |
| 4168 | struct cgroup_subsys_state *css = |
| 4169 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4170 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4171 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4174 | static void css_release(struct percpu_ref *ref) |
| 4175 | { |
| 4176 | struct cgroup_subsys_state *css = |
| 4177 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4178 | |
| 4179 | schedule_work(&css->dput_work); |
| 4180 | } |
| 4181 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4182 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4183 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4184 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4185 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4186 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4187 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4188 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4189 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4190 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4191 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4192 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4193 | |
| 4194 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4195 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4196 | * css_put(). dput() requires process context, which css_put() may |
| 4197 | * be called without. @css->dput_work will be used to invoke |
| 4198 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4199 | */ |
| 4200 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4201 | } |
| 4202 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4203 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4204 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4205 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4206 | int ret = 0; |
| 4207 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4208 | lockdep_assert_held(&cgroup_mutex); |
| 4209 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4210 | if (ss->css_online) |
| 4211 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4212 | if (!ret) |
| 4213 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4214 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4218 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4219 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4220 | { |
| 4221 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4222 | |
| 4223 | lockdep_assert_held(&cgroup_mutex); |
| 4224 | |
| 4225 | if (!(css->flags & CSS_ONLINE)) |
| 4226 | return; |
| 4227 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4228 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4229 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4230 | |
| 4231 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4232 | } |
| 4233 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4234 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4235 | * cgroup_create - create a cgroup |
| 4236 | * @parent: cgroup that will be parent of the new cgroup |
| 4237 | * @dentry: dentry of the new cgroup |
| 4238 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4239 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4240 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4241 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4242 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4243 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4244 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4245 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4246 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4247 | struct cgroupfs_root *root = parent->root; |
| 4248 | int err = 0; |
| 4249 | struct cgroup_subsys *ss; |
| 4250 | struct super_block *sb = root->sb; |
| 4251 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4252 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4253 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4254 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4255 | return -ENOMEM; |
| 4256 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4257 | name = cgroup_alloc_name(dentry); |
| 4258 | if (!name) |
| 4259 | goto err_free_cgrp; |
| 4260 | rcu_assign_pointer(cgrp->name, name); |
| 4261 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4262 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4263 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4264 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4265 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4266 | /* |
| 4267 | * Only live parents can have children. Note that the liveliness |
| 4268 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4269 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4270 | * anyway so that locking is contained inside cgroup proper and we |
| 4271 | * don't get nasty surprises if we ever grow another caller. |
| 4272 | */ |
| 4273 | if (!cgroup_lock_live_group(parent)) { |
| 4274 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4275 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4276 | } |
| 4277 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4278 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4279 | * get deleted on unmount if there are child cgroups. This |
| 4280 | * can be done outside cgroup_mutex, since the sb can't |
| 4281 | * disappear while someone has an open control file on the |
| 4282 | * fs */ |
| 4283 | atomic_inc(&sb->s_active); |
| 4284 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4285 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4286 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4287 | dentry->d_fsdata = cgrp; |
| 4288 | cgrp->dentry = dentry; |
| 4289 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4290 | cgrp->parent = parent; |
| 4291 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4292 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4293 | if (notify_on_release(parent)) |
| 4294 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4295 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4296 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4297 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4298 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4299 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4300 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4301 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4302 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4303 | if (IS_ERR(css)) { |
| 4304 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4305 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4306 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4307 | |
| 4308 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4309 | if (err) |
| 4310 | goto err_free_all; |
| 4311 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4312 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4313 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4314 | if (ss->use_id) { |
| 4315 | err = alloc_css_id(ss, parent, cgrp); |
| 4316 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4317 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4318 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4321 | /* |
| 4322 | * Create directory. cgroup_create_file() returns with the new |
| 4323 | * directory locked on success so that it can be populated without |
| 4324 | * dropping cgroup_mutex. |
| 4325 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4326 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4327 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4328 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4329 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4330 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame^] | 4331 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4332 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4333 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4334 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4335 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4336 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4337 | /* each css holds a ref to the cgroup's dentry */ |
| 4338 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4339 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4340 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4341 | /* hold a ref to the parent's dentry */ |
| 4342 | dget(parent->dentry); |
| 4343 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4344 | /* creation succeeded, notify subsystems */ |
| 4345 | for_each_subsys(root, ss) { |
| 4346 | err = online_css(ss, cgrp); |
| 4347 | if (err) |
| 4348 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4349 | |
| 4350 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4351 | parent->parent) { |
| 4352 | 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", |
| 4353 | current->comm, current->pid, ss->name); |
| 4354 | if (!strcmp(ss->name, "memory")) |
| 4355 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4356 | ss->warned_broken_hierarchy = true; |
| 4357 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4358 | } |
| 4359 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4360 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4361 | if (err) |
| 4362 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4363 | |
| 4364 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4365 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4366 | |
| 4367 | return 0; |
| 4368 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4369 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4370 | for_each_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4371 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4372 | |
| 4373 | if (css) { |
| 4374 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4375 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4376 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4377 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4378 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4379 | /* Release the reference count that we took on the superblock */ |
| 4380 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4381 | err_free_id: |
| 4382 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4383 | err_free_name: |
| 4384 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4385 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4386 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4387 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4388 | |
| 4389 | err_destroy: |
| 4390 | cgroup_destroy_locked(cgrp); |
| 4391 | mutex_unlock(&cgroup_mutex); |
| 4392 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4393 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4394 | } |
| 4395 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4396 | 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] | 4397 | { |
| 4398 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4399 | |
| 4400 | /* the vfs holds inode->i_mutex already */ |
| 4401 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4402 | } |
| 4403 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4404 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4405 | { |
| 4406 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4407 | return; |
| 4408 | |
| 4409 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4410 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4411 | schedule_work(&cgrp->destroy_work); |
| 4412 | } |
| 4413 | |
| 4414 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4415 | { |
| 4416 | struct cgroup_subsys_state *css = |
| 4417 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4418 | |
| 4419 | cgroup_css_killed(css->cgroup); |
| 4420 | } |
| 4421 | |
| 4422 | /** |
| 4423 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4424 | * @cgrp: cgroup to be destroyed |
| 4425 | * |
| 4426 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4427 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4428 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4429 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4430 | * the following two steps. |
| 4431 | * |
| 4432 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4433 | * userland visible parts and start killing the percpu refcnts of |
| 4434 | * css's. Set up so that the next stage will be kicked off once all |
| 4435 | * the percpu refcnts are confirmed to be killed. |
| 4436 | * |
| 4437 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4438 | * rest of destruction. Once all cgroup references are gone, the |
| 4439 | * cgroup is RCU-freed. |
| 4440 | * |
| 4441 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4442 | * the userland is concerned and a new cgroup with the same name may be |
| 4443 | * created. As cgroup doesn't care about the names internally, this |
| 4444 | * doesn't cause any problem. |
| 4445 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4446 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4447 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4448 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4449 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4450 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4451 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4452 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4453 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4454 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4455 | lockdep_assert_held(&cgroup_mutex); |
| 4456 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4457 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4458 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4459 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4460 | */ |
| 4461 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4462 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4463 | read_unlock(&css_set_lock); |
| 4464 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4465 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4466 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4467 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4468 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4469 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4470 | * css reference will be given out via css_tryget(). We can't |
| 4471 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4472 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4473 | * as killed on all CPUs on return. |
| 4474 | * |
| 4475 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4476 | * css is confirmed to be seen as killed on all CPUs. The |
| 4477 | * notification callback keeps track of the number of css's to be |
| 4478 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4479 | * destruction once the percpu refs of all css's are confirmed to |
| 4480 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4481 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4482 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4483 | for_each_subsys(cgrp->root, ss) { |
| 4484 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4485 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4486 | /* |
| 4487 | * Killing would put the base ref, but we need to keep it |
| 4488 | * alive until after ->css_offline. |
| 4489 | */ |
| 4490 | percpu_ref_get(&css->refcnt); |
| 4491 | |
| 4492 | atomic_inc(&cgrp->css_kill_cnt); |
| 4493 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4494 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4495 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4496 | |
| 4497 | /* |
| 4498 | * Mark @cgrp dead. This prevents further task migration and child |
| 4499 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4500 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4501 | * resume iteration after dropping RCU read lock. See |
| 4502 | * cgroup_next_sibling() for details. |
| 4503 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4504 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4505 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4506 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4507 | raw_spin_lock(&release_list_lock); |
| 4508 | if (!list_empty(&cgrp->release_list)) |
| 4509 | list_del_init(&cgrp->release_list); |
| 4510 | raw_spin_unlock(&release_list_lock); |
| 4511 | |
| 4512 | /* |
| 4513 | * Remove @cgrp directory. The removal puts the base ref but we |
| 4514 | * aren't quite done with @cgrp yet, so hold onto it. |
| 4515 | */ |
| 4516 | dget(d); |
| 4517 | cgroup_d_remove_dir(d); |
| 4518 | |
| 4519 | /* |
| 4520 | * Unregister events and notify userspace. |
| 4521 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4522 | * directory to avoid race between userspace and kernelspace. |
| 4523 | */ |
| 4524 | spin_lock(&cgrp->event_list_lock); |
| 4525 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4526 | list_del_init(&event->list); |
| 4527 | schedule_work(&event->remove); |
| 4528 | } |
| 4529 | spin_unlock(&cgrp->event_list_lock); |
| 4530 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4531 | return 0; |
| 4532 | }; |
| 4533 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4534 | /** |
| 4535 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4536 | * @work: cgroup->destroy_free_work |
| 4537 | * |
| 4538 | * This function is invoked from a work item for a cgroup which is being |
| 4539 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4540 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4541 | * is the second step of destruction described in the comment above |
| 4542 | * cgroup_destroy_locked(). |
| 4543 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4544 | static void cgroup_offline_fn(struct work_struct *work) |
| 4545 | { |
| 4546 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4547 | struct cgroup *parent = cgrp->parent; |
| 4548 | struct dentry *d = cgrp->dentry; |
| 4549 | struct cgroup_subsys *ss; |
| 4550 | |
| 4551 | mutex_lock(&cgroup_mutex); |
| 4552 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4553 | /* |
| 4554 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4555 | * initate destruction. |
| 4556 | */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4557 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4558 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4559 | |
| 4560 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4561 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4562 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4563 | * proceeds regardless of css refs. On the last put of each css, |
| 4564 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4565 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4566 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4567 | for_each_subsys(cgrp->root, ss) |
| 4568 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4569 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4570 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4571 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4572 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4573 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4574 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4575 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4576 | check_for_release(parent); |
| 4577 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4578 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4579 | } |
| 4580 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4581 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4582 | { |
| 4583 | int ret; |
| 4584 | |
| 4585 | mutex_lock(&cgroup_mutex); |
| 4586 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4587 | mutex_unlock(&cgroup_mutex); |
| 4588 | |
| 4589 | return ret; |
| 4590 | } |
| 4591 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4592 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4593 | { |
| 4594 | INIT_LIST_HEAD(&ss->cftsets); |
| 4595 | |
| 4596 | /* |
| 4597 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4598 | * deregistration. |
| 4599 | */ |
| 4600 | if (ss->base_cftypes) { |
| 4601 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4602 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4603 | } |
| 4604 | } |
| 4605 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4606 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4607 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4608 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4609 | |
| 4610 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4611 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4612 | mutex_lock(&cgroup_mutex); |
| 4613 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4614 | /* init base cftset */ |
| 4615 | cgroup_init_cftsets(ss); |
| 4616 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4617 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4618 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4619 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4620 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4621 | /* We don't handle early failures gracefully */ |
| 4622 | BUG_ON(IS_ERR(css)); |
| 4623 | init_cgroup_css(css, ss, dummytop); |
| 4624 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4625 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4626 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4627 | * newly registered, all tasks and hence the |
| 4628 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4629 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4630 | |
| 4631 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4632 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4633 | /* At system boot, before all subsystems have been |
| 4634 | * registered, no tasks have been forked, so we don't |
| 4635 | * need to invoke fork callbacks here. */ |
| 4636 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4637 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4638 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4639 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4640 | mutex_unlock(&cgroup_mutex); |
| 4641 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4642 | /* this function shouldn't be used with modular subsystems, since they |
| 4643 | * need to register a subsys_id, among other things */ |
| 4644 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4648 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4649 | * @ss: the subsystem to load |
| 4650 | * |
| 4651 | * 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] | 4652 | * 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] | 4653 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4654 | * simpler cgroup_init_subsys. |
| 4655 | */ |
| 4656 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4657 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4658 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4659 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4660 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4661 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4662 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4663 | |
| 4664 | /* check name and function validity */ |
| 4665 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4666 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4667 | return -EINVAL; |
| 4668 | |
| 4669 | /* |
| 4670 | * we don't support callbacks in modular subsystems. this check is |
| 4671 | * before the ss->module check for consistency; a subsystem that could |
| 4672 | * be a module should still have no callbacks even if the user isn't |
| 4673 | * compiling it as one. |
| 4674 | */ |
| 4675 | if (ss->fork || ss->exit) |
| 4676 | return -EINVAL; |
| 4677 | |
| 4678 | /* |
| 4679 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4680 | * since cgroup_init_subsys will have already taken care of it. |
| 4681 | */ |
| 4682 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4683 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4684 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4685 | return 0; |
| 4686 | } |
| 4687 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4688 | /* init base cftset */ |
| 4689 | cgroup_init_cftsets(ss); |
| 4690 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4691 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4692 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4693 | |
| 4694 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4695 | * no ss->css_alloc seems to need anything important in the ss |
| 4696 | * struct, so this can happen first (i.e. before the rootnode |
| 4697 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4698 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4699 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4700 | if (IS_ERR(css)) { |
| 4701 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4702 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4703 | mutex_unlock(&cgroup_mutex); |
| 4704 | return PTR_ERR(css); |
| 4705 | } |
| 4706 | |
| 4707 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4708 | ss->root = &rootnode; |
| 4709 | |
| 4710 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4711 | init_cgroup_css(css, ss, dummytop); |
| 4712 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4713 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4714 | ret = cgroup_init_idr(ss, css); |
| 4715 | if (ret) |
| 4716 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | /* |
| 4720 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4721 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4722 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4723 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4724 | * table state, so each changed css_set will need its hash recomputed. |
| 4725 | * this is all done under the css_set_lock. |
| 4726 | */ |
| 4727 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4728 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4729 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4730 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4731 | continue; |
| 4732 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4733 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4734 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4735 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4736 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4737 | key = css_set_hash(cset->subsys); |
| 4738 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4739 | } |
| 4740 | write_unlock(&css_set_lock); |
| 4741 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4742 | ret = online_css(ss, dummytop); |
| 4743 | if (ret) |
| 4744 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4745 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4746 | /* success! */ |
| 4747 | mutex_unlock(&cgroup_mutex); |
| 4748 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4749 | |
| 4750 | err_unload: |
| 4751 | mutex_unlock(&cgroup_mutex); |
| 4752 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4753 | cgroup_unload_subsys(ss); |
| 4754 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4755 | } |
| 4756 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4757 | |
| 4758 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4759 | * cgroup_unload_subsys: unload a modular subsystem |
| 4760 | * @ss: the subsystem to unload |
| 4761 | * |
| 4762 | * This function should be called in a modular subsystem's exitcall. When this |
| 4763 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4764 | * the subsystem will not be attached to any hierarchy. |
| 4765 | */ |
| 4766 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4767 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4768 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4769 | |
| 4770 | BUG_ON(ss->module == NULL); |
| 4771 | |
| 4772 | /* |
| 4773 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4774 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4775 | * doesn't start being used while we're killing it off. |
| 4776 | */ |
| 4777 | BUG_ON(ss->root != &rootnode); |
| 4778 | |
| 4779 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4780 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4781 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4782 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4783 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4784 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4785 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4786 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4787 | subsys[ss->subsys_id] = NULL; |
| 4788 | |
| 4789 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4790 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4791 | |
| 4792 | /* |
| 4793 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4794 | * in loading, we need to pay our respects to the hashtable gods. |
| 4795 | */ |
| 4796 | write_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4797 | list_for_each_entry(link, &dummytop->cset_links, cset_link) { |
| 4798 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4799 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4800 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4801 | hash_del(&cset->hlist); |
| 4802 | cset->subsys[ss->subsys_id] = NULL; |
| 4803 | key = css_set_hash(cset->subsys); |
| 4804 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4805 | } |
| 4806 | write_unlock(&css_set_lock); |
| 4807 | |
| 4808 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4809 | * remove subsystem's css from the dummytop and free it - need to |
| 4810 | * free before marking as null because ss->css_free needs the |
| 4811 | * cgrp->subsys pointer to find their state. note that this also |
| 4812 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4813 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4814 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4815 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4816 | |
| 4817 | mutex_unlock(&cgroup_mutex); |
| 4818 | } |
| 4819 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4820 | |
| 4821 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4822 | * cgroup_init_early - cgroup initialization at system boot |
| 4823 | * |
| 4824 | * Initialize cgroups at system boot, and initialize any |
| 4825 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4826 | */ |
| 4827 | int __init cgroup_init_early(void) |
| 4828 | { |
| 4829 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4830 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4831 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4832 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4833 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4834 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4835 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4836 | root_count = 1; |
| 4837 | init_task.cgroups = &init_css_set; |
| 4838 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4839 | init_cgrp_cset_link.cset = &init_css_set; |
| 4840 | init_cgrp_cset_link.cgrp = dummytop; |
| 4841 | list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links); |
| 4842 | list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4843 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4844 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4845 | struct cgroup_subsys *ss = subsys[i]; |
| 4846 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4847 | /* at bootup time, we don't worry about modular subsystems */ |
| 4848 | if (!ss || ss->module) |
| 4849 | continue; |
| 4850 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4851 | BUG_ON(!ss->name); |
| 4852 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4853 | BUG_ON(!ss->css_alloc); |
| 4854 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4855 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4856 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4857 | ss->name, ss->subsys_id); |
| 4858 | BUG(); |
| 4859 | } |
| 4860 | |
| 4861 | if (ss->early_init) |
| 4862 | cgroup_init_subsys(ss); |
| 4863 | } |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
| 4867 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4868 | * cgroup_init - cgroup initialization |
| 4869 | * |
| 4870 | * Register cgroup filesystem and /proc file, and initialize |
| 4871 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4872 | */ |
| 4873 | int __init cgroup_init(void) |
| 4874 | { |
| 4875 | int err; |
| 4876 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4877 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4878 | |
| 4879 | err = bdi_init(&cgroup_backing_dev_info); |
| 4880 | if (err) |
| 4881 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4882 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4883 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4884 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4885 | |
| 4886 | /* at bootup time, we don't worry about modular subsystems */ |
| 4887 | if (!ss || ss->module) |
| 4888 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4889 | if (!ss->early_init) |
| 4890 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4891 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4892 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4895 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4896 | key = css_set_hash(init_css_set.subsys); |
| 4897 | hash_add(css_set_table, &init_css_set.hlist, key); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4898 | |
| 4899 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4900 | mutex_lock(&cgroup_mutex); |
| 4901 | mutex_lock(&cgroup_root_mutex); |
| 4902 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4903 | BUG_ON(cgroup_init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4904 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4905 | mutex_unlock(&cgroup_root_mutex); |
| 4906 | mutex_unlock(&cgroup_mutex); |
| 4907 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4908 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4909 | if (!cgroup_kobj) { |
| 4910 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4911 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | err = register_filesystem(&cgroup_fs_type); |
| 4915 | if (err < 0) { |
| 4916 | kobject_put(cgroup_kobj); |
| 4917 | goto out; |
| 4918 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4919 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4920 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4921 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4922 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4923 | if (err) |
| 4924 | bdi_destroy(&cgroup_backing_dev_info); |
| 4925 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4926 | return err; |
| 4927 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4928 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4929 | /* |
| 4930 | * proc_cgroup_show() |
| 4931 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4932 | * - Used for /proc/<pid>/cgroup. |
| 4933 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4934 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4935 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4936 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4937 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4938 | * cgroup to top_cgroup. |
| 4939 | */ |
| 4940 | |
| 4941 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4942 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4943 | { |
| 4944 | struct pid *pid; |
| 4945 | struct task_struct *tsk; |
| 4946 | char *buf; |
| 4947 | int retval; |
| 4948 | struct cgroupfs_root *root; |
| 4949 | |
| 4950 | retval = -ENOMEM; |
| 4951 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4952 | if (!buf) |
| 4953 | goto out; |
| 4954 | |
| 4955 | retval = -ESRCH; |
| 4956 | pid = m->private; |
| 4957 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4958 | if (!tsk) |
| 4959 | goto out_free; |
| 4960 | |
| 4961 | retval = 0; |
| 4962 | |
| 4963 | mutex_lock(&cgroup_mutex); |
| 4964 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4965 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4966 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4967 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4968 | int count = 0; |
| 4969 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4970 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4971 | for_each_subsys(root, ss) |
| 4972 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4973 | if (strlen(root->name)) |
| 4974 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4975 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4976 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4977 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4978 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4979 | if (retval < 0) |
| 4980 | goto out_unlock; |
| 4981 | seq_puts(m, buf); |
| 4982 | seq_putc(m, '\n'); |
| 4983 | } |
| 4984 | |
| 4985 | out_unlock: |
| 4986 | mutex_unlock(&cgroup_mutex); |
| 4987 | put_task_struct(tsk); |
| 4988 | out_free: |
| 4989 | kfree(buf); |
| 4990 | out: |
| 4991 | return retval; |
| 4992 | } |
| 4993 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4994 | /* Display information about each subsystem and each hierarchy */ |
| 4995 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4996 | { |
| 4997 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4998 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4999 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5000 | /* |
| 5001 | * ideally we don't want subsystems moving around while we do this. |
| 5002 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5003 | * subsys/hierarchy state. |
| 5004 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5005 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5006 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 5007 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5008 | if (ss == NULL) |
| 5009 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5010 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5011 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5012 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5013 | } |
| 5014 | mutex_unlock(&cgroup_mutex); |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
| 5018 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5019 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5020 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5021 | } |
| 5022 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5023 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5024 | .open = cgroupstats_open, |
| 5025 | .read = seq_read, |
| 5026 | .llseek = seq_lseek, |
| 5027 | .release = single_release, |
| 5028 | }; |
| 5029 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5030 | /** |
| 5031 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5032 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5033 | * |
| 5034 | * Description: A task inherits its parent's cgroup at fork(). |
| 5035 | * |
| 5036 | * A pointer to the shared css_set was automatically copied in |
| 5037 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5038 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5039 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5040 | * have already changed current->cgroups, allowing the previously |
| 5041 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5042 | * |
| 5043 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5044 | * task, and the passed argument 'child' points to the child task. |
| 5045 | */ |
| 5046 | void cgroup_fork(struct task_struct *child) |
| 5047 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5048 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5049 | child->cgroups = current->cgroups; |
| 5050 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5051 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5052 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5056 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5057 | * @child: the task in question |
| 5058 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5059 | * Adds the task to the list running through its css_set if necessary and |
| 5060 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5061 | * visible on the task list in case we race with the first call to |
| 5062 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5063 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5064 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5065 | void cgroup_post_fork(struct task_struct *child) |
| 5066 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5067 | int i; |
| 5068 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5069 | /* |
| 5070 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5071 | * under the tasklist_lock and we read it here after we added the child |
| 5072 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5073 | * yet in the tasklist when we walked through it from |
| 5074 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5075 | * should be visible now due to the paired locking and barriers implied |
| 5076 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5077 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5078 | * lock on fork. |
| 5079 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5080 | if (use_task_css_set_links) { |
| 5081 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5082 | task_lock(child); |
| 5083 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5084 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5085 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5086 | write_unlock(&css_set_lock); |
| 5087 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5088 | |
| 5089 | /* |
| 5090 | * Call ss->fork(). This must happen after @child is linked on |
| 5091 | * css_set; otherwise, @child might change state between ->fork() |
| 5092 | * and addition to css_set. |
| 5093 | */ |
| 5094 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5095 | /* |
| 5096 | * fork/exit callbacks are supported only for builtin |
| 5097 | * subsystems, and the builtin section of the subsys |
| 5098 | * array is immutable, so we don't need to lock the |
| 5099 | * subsys array here. On the other hand, modular section |
| 5100 | * of the array can be freed at module unload, so we |
| 5101 | * can't touch that. |
| 5102 | */ |
| 5103 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5104 | struct cgroup_subsys *ss = subsys[i]; |
| 5105 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5106 | if (ss->fork) |
| 5107 | ss->fork(child); |
| 5108 | } |
| 5109 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5110 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5111 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5112 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5113 | * cgroup_exit - detach cgroup from exiting task |
| 5114 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5115 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5116 | * |
| 5117 | * Description: Detach cgroup from @tsk and release it. |
| 5118 | * |
| 5119 | * Note that cgroups marked notify_on_release force every task in |
| 5120 | * them to take the global cgroup_mutex mutex when exiting. |
| 5121 | * This could impact scaling on very large systems. Be reluctant to |
| 5122 | * use notify_on_release cgroups where very high task exit scaling |
| 5123 | * is required on large systems. |
| 5124 | * |
| 5125 | * the_top_cgroup_hack: |
| 5126 | * |
| 5127 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5128 | * |
| 5129 | * We call cgroup_exit() while the task is still competent to |
| 5130 | * handle notify_on_release(), then leave the task attached to the |
| 5131 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5132 | * |
| 5133 | * To do this properly, we would increment the reference count on |
| 5134 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5135 | * code we would add a second cgroup function call, to drop that |
| 5136 | * reference. This would just create an unnecessary hot spot on |
| 5137 | * the top_cgroup reference count, to no avail. |
| 5138 | * |
| 5139 | * Normally, holding a reference to a cgroup without bumping its |
| 5140 | * count is unsafe. The cgroup could go away, or someone could |
| 5141 | * attach us to a different cgroup, decrementing the count on |
| 5142 | * the first cgroup that we never incremented. But in this case, |
| 5143 | * 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] | 5144 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5145 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5146 | */ |
| 5147 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5148 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5149 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5150 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5151 | |
| 5152 | /* |
| 5153 | * Unlink from the css_set task list if necessary. |
| 5154 | * Optimistically check cg_list before taking |
| 5155 | * css_set_lock |
| 5156 | */ |
| 5157 | if (!list_empty(&tsk->cg_list)) { |
| 5158 | write_lock(&css_set_lock); |
| 5159 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5160 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5161 | write_unlock(&css_set_lock); |
| 5162 | } |
| 5163 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5164 | /* Reassign the task to the init_css_set. */ |
| 5165 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5166 | cset = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5167 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5168 | |
| 5169 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5170 | /* |
| 5171 | * fork/exit callbacks are supported only for builtin |
| 5172 | * subsystems, see cgroup_post_fork() for details. |
| 5173 | */ |
| 5174 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5175 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5176 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5177 | if (ss->exit) { |
| 5178 | struct cgroup *old_cgrp = |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5179 | rcu_dereference_raw(cset->subsys[i])->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5180 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5181 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5182 | } |
| 5183 | } |
| 5184 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5185 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5186 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5187 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5188 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5189 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5190 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5191 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5192 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5193 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5194 | /* |
| 5195 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5196 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5197 | * it now |
| 5198 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5199 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5200 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5201 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5202 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5203 | list_empty(&cgrp->release_list)) { |
| 5204 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5205 | need_schedule_work = 1; |
| 5206 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5207 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5208 | if (need_schedule_work) |
| 5209 | schedule_work(&release_agent_work); |
| 5210 | } |
| 5211 | } |
| 5212 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5213 | /* |
| 5214 | * Notify userspace when a cgroup is released, by running the |
| 5215 | * configured release agent with the name of the cgroup (path |
| 5216 | * relative to the root of cgroup file system) as the argument. |
| 5217 | * |
| 5218 | * Most likely, this user command will try to rmdir this cgroup. |
| 5219 | * |
| 5220 | * This races with the possibility that some other task will be |
| 5221 | * attached to this cgroup before it is removed, or that some other |
| 5222 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5223 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5224 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5225 | * to continue to serve a useful existence. Next time it's released, |
| 5226 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5227 | * |
| 5228 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5229 | * means only wait until the task is successfully execve()'d. The |
| 5230 | * separate release agent task is forked by call_usermodehelper(), |
| 5231 | * then control in this thread returns here, without waiting for the |
| 5232 | * release agent task. We don't bother to wait because the caller of |
| 5233 | * this routine has no use for the exit status of the release agent |
| 5234 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5235 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5236 | static void cgroup_release_agent(struct work_struct *work) |
| 5237 | { |
| 5238 | BUG_ON(work != &release_agent_work); |
| 5239 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5240 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5241 | while (!list_empty(&release_list)) { |
| 5242 | char *argv[3], *envp[3]; |
| 5243 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5244 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5245 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5246 | struct cgroup, |
| 5247 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5248 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5249 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5250 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5251 | if (!pathbuf) |
| 5252 | goto continue_free; |
| 5253 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5254 | goto continue_free; |
| 5255 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5256 | if (!agentbuf) |
| 5257 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5258 | |
| 5259 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5260 | argv[i++] = agentbuf; |
| 5261 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5262 | argv[i] = NULL; |
| 5263 | |
| 5264 | i = 0; |
| 5265 | /* minimal command environment */ |
| 5266 | envp[i++] = "HOME=/"; |
| 5267 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5268 | envp[i] = NULL; |
| 5269 | |
| 5270 | /* Drop the lock while we invoke the usermode helper, |
| 5271 | * since the exec could involve hitting disk and hence |
| 5272 | * be a slow process */ |
| 5273 | mutex_unlock(&cgroup_mutex); |
| 5274 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5275 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5276 | continue_free: |
| 5277 | kfree(pathbuf); |
| 5278 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5279 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5280 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5281 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5282 | mutex_unlock(&cgroup_mutex); |
| 5283 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5284 | |
| 5285 | static int __init cgroup_disable(char *str) |
| 5286 | { |
| 5287 | int i; |
| 5288 | char *token; |
| 5289 | |
| 5290 | while ((token = strsep(&str, ",")) != NULL) { |
| 5291 | if (!*token) |
| 5292 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5293 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5294 | struct cgroup_subsys *ss = subsys[i]; |
| 5295 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5296 | /* |
| 5297 | * cgroup_disable, being at boot time, can't |
| 5298 | * know about module subsystems, so we don't |
| 5299 | * worry about them. |
| 5300 | */ |
| 5301 | if (!ss || ss->module) |
| 5302 | continue; |
| 5303 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5304 | if (!strcmp(token, ss->name)) { |
| 5305 | ss->disabled = 1; |
| 5306 | printk(KERN_INFO "Disabling %s control group" |
| 5307 | " subsystem\n", ss->name); |
| 5308 | break; |
| 5309 | } |
| 5310 | } |
| 5311 | } |
| 5312 | return 1; |
| 5313 | } |
| 5314 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5315 | |
| 5316 | /* |
| 5317 | * Functons for CSS ID. |
| 5318 | */ |
| 5319 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5320 | /* to get ID other than 0, this should be called when !cgroup_is_dead() */ |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5321 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5322 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5323 | struct css_id *cssid; |
| 5324 | |
| 5325 | /* |
| 5326 | * This css_id() can return correct value when somone has refcnt |
| 5327 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5328 | * it's unchanged until freed. |
| 5329 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5330 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5331 | |
| 5332 | if (cssid) |
| 5333 | return cssid->id; |
| 5334 | return 0; |
| 5335 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5336 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5337 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5338 | /** |
| 5339 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5340 | * @child: the css to be tested. |
| 5341 | * @root: the css supporsed to be an ancestor of the child. |
| 5342 | * |
| 5343 | * 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] | 5344 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5345 | * But, considering usual usage, the csses should be valid objects after test. |
| 5346 | * Assuming that the caller will do some action to the child if this returns |
| 5347 | * returns true, the caller must take "child";s reference count. |
| 5348 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5349 | */ |
| 5350 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5351 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5352 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5353 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5354 | struct css_id *child_id; |
| 5355 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5356 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5357 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5358 | if (!child_id) |
| 5359 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5360 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5361 | if (!root_id) |
| 5362 | return false; |
| 5363 | if (child_id->depth < root_id->depth) |
| 5364 | return false; |
| 5365 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5366 | return false; |
| 5367 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5368 | } |
| 5369 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5370 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5371 | { |
| 5372 | struct css_id *id = css->id; |
| 5373 | /* When this is called before css_id initialization, id can be NULL */ |
| 5374 | if (!id) |
| 5375 | return; |
| 5376 | |
| 5377 | BUG_ON(!ss->use_id); |
| 5378 | |
| 5379 | rcu_assign_pointer(id->css, NULL); |
| 5380 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5381 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5382 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5383 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5384 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5385 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5386 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5387 | |
| 5388 | /* |
| 5389 | * This is called by init or create(). Then, calls to this function are |
| 5390 | * always serialized (By cgroup_mutex() at create()). |
| 5391 | */ |
| 5392 | |
| 5393 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5394 | { |
| 5395 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5396 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5397 | |
| 5398 | BUG_ON(!ss->use_id); |
| 5399 | |
| 5400 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5401 | newid = kzalloc(size, GFP_KERNEL); |
| 5402 | if (!newid) |
| 5403 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5404 | |
| 5405 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5406 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5407 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5408 | 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] | 5409 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5410 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5411 | |
| 5412 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5413 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5414 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5415 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5416 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5417 | newid->depth = depth; |
| 5418 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5419 | err_out: |
| 5420 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5421 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5422 | |
| 5423 | } |
| 5424 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5425 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5426 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5427 | { |
| 5428 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5429 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5430 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5431 | idr_init(&ss->idr); |
| 5432 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5433 | newid = get_new_cssid(ss, 0); |
| 5434 | if (IS_ERR(newid)) |
| 5435 | return PTR_ERR(newid); |
| 5436 | |
| 5437 | newid->stack[0] = newid->id; |
| 5438 | newid->css = rootcss; |
| 5439 | rootcss->id = newid; |
| 5440 | return 0; |
| 5441 | } |
| 5442 | |
| 5443 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5444 | struct cgroup *child) |
| 5445 | { |
| 5446 | int subsys_id, i, depth = 0; |
| 5447 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5448 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5449 | |
| 5450 | subsys_id = ss->subsys_id; |
| 5451 | parent_css = parent->subsys[subsys_id]; |
| 5452 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5453 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5454 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5455 | |
| 5456 | child_id = get_new_cssid(ss, depth); |
| 5457 | if (IS_ERR(child_id)) |
| 5458 | return PTR_ERR(child_id); |
| 5459 | |
| 5460 | for (i = 0; i < depth; i++) |
| 5461 | child_id->stack[i] = parent_id->stack[i]; |
| 5462 | child_id->stack[depth] = child_id->id; |
| 5463 | /* |
| 5464 | * child_id->css pointer will be set after this cgroup is available |
| 5465 | * see cgroup_populate_dir() |
| 5466 | */ |
| 5467 | rcu_assign_pointer(child_css->id, child_id); |
| 5468 | |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | /** |
| 5473 | * css_lookup - lookup css by id |
| 5474 | * @ss: cgroup subsys to be looked into. |
| 5475 | * @id: the id |
| 5476 | * |
| 5477 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5478 | * NULL if not. Should be called under rcu_read_lock() |
| 5479 | */ |
| 5480 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5481 | { |
| 5482 | struct css_id *cssid = NULL; |
| 5483 | |
| 5484 | BUG_ON(!ss->use_id); |
| 5485 | cssid = idr_find(&ss->idr, id); |
| 5486 | |
| 5487 | if (unlikely(!cssid)) |
| 5488 | return NULL; |
| 5489 | |
| 5490 | return rcu_dereference(cssid->css); |
| 5491 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5492 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5493 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5494 | /* |
| 5495 | * get corresponding css from file open on cgroupfs directory |
| 5496 | */ |
| 5497 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5498 | { |
| 5499 | struct cgroup *cgrp; |
| 5500 | struct inode *inode; |
| 5501 | struct cgroup_subsys_state *css; |
| 5502 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5503 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5504 | /* check in cgroup filesystem dir */ |
| 5505 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5506 | return ERR_PTR(-EBADF); |
| 5507 | |
| 5508 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5509 | return ERR_PTR(-EINVAL); |
| 5510 | |
| 5511 | /* get cgroup */ |
| 5512 | cgrp = __d_cgrp(f->f_dentry); |
| 5513 | css = cgrp->subsys[id]; |
| 5514 | return css ? css : ERR_PTR(-ENOENT); |
| 5515 | } |
| 5516 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5517 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5518 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5519 | { |
| 5520 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5521 | |
| 5522 | if (!css) |
| 5523 | return ERR_PTR(-ENOMEM); |
| 5524 | |
| 5525 | return css; |
| 5526 | } |
| 5527 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5528 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5529 | { |
| 5530 | kfree(cont->subsys[debug_subsys_id]); |
| 5531 | } |
| 5532 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5533 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5534 | { |
| 5535 | return cgroup_task_count(cont); |
| 5536 | } |
| 5537 | |
| 5538 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5539 | { |
| 5540 | return (u64)(unsigned long)current->cgroups; |
| 5541 | } |
| 5542 | |
| 5543 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5544 | struct cftype *cft) |
| 5545 | { |
| 5546 | u64 count; |
| 5547 | |
| 5548 | rcu_read_lock(); |
| 5549 | count = atomic_read(¤t->cgroups->refcount); |
| 5550 | rcu_read_unlock(); |
| 5551 | return count; |
| 5552 | } |
| 5553 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5554 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5555 | struct cftype *cft, |
| 5556 | struct seq_file *seq) |
| 5557 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5558 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5559 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5560 | |
| 5561 | read_lock(&css_set_lock); |
| 5562 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5563 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5564 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5565 | struct cgroup *c = link->cgrp; |
| 5566 | const char *name; |
| 5567 | |
| 5568 | if (c->dentry) |
| 5569 | name = c->dentry->d_name.name; |
| 5570 | else |
| 5571 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5572 | seq_printf(seq, "Root %d group %s\n", |
| 5573 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5574 | } |
| 5575 | rcu_read_unlock(); |
| 5576 | read_unlock(&css_set_lock); |
| 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5581 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5582 | struct cftype *cft, |
| 5583 | struct seq_file *seq) |
| 5584 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5585 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5586 | |
| 5587 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5588 | list_for_each_entry(link, &cont->cset_links, cset_link) { |
| 5589 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5590 | struct task_struct *task; |
| 5591 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5592 | seq_printf(seq, "css_set %p\n", cset); |
| 5593 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5594 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5595 | seq_puts(seq, " ...\n"); |
| 5596 | break; |
| 5597 | } else { |
| 5598 | seq_printf(seq, " task %d\n", |
| 5599 | task_pid_vnr(task)); |
| 5600 | } |
| 5601 | } |
| 5602 | } |
| 5603 | read_unlock(&css_set_lock); |
| 5604 | return 0; |
| 5605 | } |
| 5606 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5607 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5608 | { |
| 5609 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5610 | } |
| 5611 | |
| 5612 | static struct cftype debug_files[] = { |
| 5613 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5614 | .name = "taskcount", |
| 5615 | .read_u64 = debug_taskcount_read, |
| 5616 | }, |
| 5617 | |
| 5618 | { |
| 5619 | .name = "current_css_set", |
| 5620 | .read_u64 = current_css_set_read, |
| 5621 | }, |
| 5622 | |
| 5623 | { |
| 5624 | .name = "current_css_set_refcount", |
| 5625 | .read_u64 = current_css_set_refcount_read, |
| 5626 | }, |
| 5627 | |
| 5628 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5629 | .name = "current_css_set_cg_links", |
| 5630 | .read_seq_string = current_css_set_cg_links_read, |
| 5631 | }, |
| 5632 | |
| 5633 | { |
| 5634 | .name = "cgroup_css_links", |
| 5635 | .read_seq_string = cgroup_css_links_read, |
| 5636 | }, |
| 5637 | |
| 5638 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5639 | .name = "releasable", |
| 5640 | .read_u64 = releasable_read, |
| 5641 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5642 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5643 | { } /* terminate */ |
| 5644 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5645 | |
| 5646 | struct cgroup_subsys debug_subsys = { |
| 5647 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5648 | .css_alloc = debug_css_alloc, |
| 5649 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5650 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5651 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5652 | }; |
| 5653 | #endif /* CONFIG_CGROUP_DEBUG */ |