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); |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame^] | 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 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) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 99 | static struct cgroup_subsys *cgroup_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 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 104 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 105 | * unattached - it never has more than a single cgroup, and all tasks are |
| 106 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 107 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 108 | static struct cgroupfs_root cgroup_dummy_root; |
| 109 | |
| 110 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 111 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 113 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 114 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 115 | */ |
| 116 | struct cfent { |
| 117 | struct list_head node; |
| 118 | struct dentry *dentry; |
| 119 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 120 | |
| 121 | /* file xattrs */ |
| 122 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 126 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 127 | * cgroup_subsys->use_id != 0. |
| 128 | */ |
| 129 | #define CSS_ID_MAX (65535) |
| 130 | struct css_id { |
| 131 | /* |
| 132 | * The css to which this ID points. This pointer is set to valid value |
| 133 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 134 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 135 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 136 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 137 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 138 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 139 | /* |
| 140 | * ID of this css. |
| 141 | */ |
| 142 | unsigned short id; |
| 143 | /* |
| 144 | * Depth in hierarchy which this ID belongs to. |
| 145 | */ |
| 146 | unsigned short depth; |
| 147 | /* |
| 148 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 149 | */ |
| 150 | struct rcu_head rcu_head; |
| 151 | /* |
| 152 | * Hierarchy of CSS ID belongs to. |
| 153 | */ |
| 154 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 155 | }; |
| 156 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 157 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 158 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 159 | */ |
| 160 | struct cgroup_event { |
| 161 | /* |
| 162 | * Cgroup which the event belongs to. |
| 163 | */ |
| 164 | struct cgroup *cgrp; |
| 165 | /* |
| 166 | * Control file which the event associated. |
| 167 | */ |
| 168 | struct cftype *cft; |
| 169 | /* |
| 170 | * eventfd to signal userspace about the event. |
| 171 | */ |
| 172 | struct eventfd_ctx *eventfd; |
| 173 | /* |
| 174 | * Each of these stored in a list by the cgroup. |
| 175 | */ |
| 176 | struct list_head list; |
| 177 | /* |
| 178 | * All fields below needed to unregister event when |
| 179 | * userspace closes eventfd. |
| 180 | */ |
| 181 | poll_table pt; |
| 182 | wait_queue_head_t *wqh; |
| 183 | wait_queue_t wait; |
| 184 | struct work_struct remove; |
| 185 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | /* The list of hierarchy roots */ |
| 188 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 189 | static LIST_HEAD(cgroup_roots); |
| 190 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 191 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 194 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 195 | * writes, either for reads. |
| 196 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 197 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 198 | |
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 | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 218 | static struct cftype cgroup_base_files[]; |
| 219 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 220 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 221 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 222 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 223 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 224 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 226 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 228 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 231 | /** |
| 232 | * cgroup_is_descendant - test ancestry |
| 233 | * @cgrp: the cgroup to be tested |
| 234 | * @ancestor: possible ancestor of @cgrp |
| 235 | * |
| 236 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 237 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 238 | * and @ancestor are accessible. |
| 239 | */ |
| 240 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 241 | { |
| 242 | while (cgrp) { |
| 243 | if (cgrp == ancestor) |
| 244 | return true; |
| 245 | cgrp = cgrp->parent; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 250 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 251 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 252 | { |
| 253 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 254 | (1 << CGRP_RELEASABLE) | |
| 255 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 256 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 259 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 261 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 264 | /** |
| 265 | * for_each_subsys - iterate all loaded cgroup subsystems |
| 266 | * @ss: the iteration cursor |
| 267 | * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end |
| 268 | * |
| 269 | * Should be called under cgroup_mutex. |
| 270 | */ |
| 271 | #define for_each_subsys(ss, i) \ |
| 272 | for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \ |
| 273 | if (({ lockdep_assert_held(&cgroup_mutex); \ |
| 274 | !((ss) = cgroup_subsys[i]); })) { } \ |
| 275 | else |
| 276 | |
| 277 | /** |
| 278 | * for_each_builtin_subsys - iterate all built-in cgroup subsystems |
| 279 | * @ss: the iteration cursor |
| 280 | * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end |
| 281 | * |
| 282 | * Bulit-in subsystems are always present and iteration itself doesn't |
| 283 | * require any synchronization. |
| 284 | */ |
| 285 | #define for_each_builtin_subsys(ss, i) \ |
| 286 | for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \ |
| 287 | (((ss) = cgroup_subsys[i]) || true); (i)++) |
| 288 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 289 | /* iterate each subsystem attached to a hierarchy */ |
| 290 | #define for_each_root_subsys(root, ss) \ |
| 291 | list_for_each_entry((ss), &(root)->subsys_list, sibling) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 292 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 293 | /* iterate across the active hierarchies */ |
| 294 | #define for_each_active_root(root) \ |
| 295 | list_for_each_entry((root), &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 296 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 297 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 298 | { |
| 299 | return dentry->d_fsdata; |
| 300 | } |
| 301 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 302 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 303 | { |
| 304 | return dentry->d_fsdata; |
| 305 | } |
| 306 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 307 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 308 | { |
| 309 | return __d_cfe(dentry)->type; |
| 310 | } |
| 311 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 312 | /** |
| 313 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 314 | * @cgrp: the cgroup to be checked for liveness |
| 315 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 316 | * On success, returns true; the mutex should be later unlocked. On |
| 317 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 318 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 319 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 320 | { |
| 321 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 322 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 323 | mutex_unlock(&cgroup_mutex); |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 328 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 329 | /* the list of cgroups eligible for automatic release. Protected by |
| 330 | * release_list_lock */ |
| 331 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 332 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 333 | static void cgroup_release_agent(struct work_struct *work); |
| 334 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 335 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 336 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 337 | /* |
| 338 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 339 | * belong to different cgroups on different hierarchies. In the other |
| 340 | * direction, a css_set is naturally associated with multiple cgroups. |
| 341 | * This M:N relationship is represented by the following link structure |
| 342 | * which exists for each association and allows traversing the associations |
| 343 | * from both sides. |
| 344 | */ |
| 345 | struct cgrp_cset_link { |
| 346 | /* the cgroup and css_set this link associates */ |
| 347 | struct cgroup *cgrp; |
| 348 | struct css_set *cset; |
| 349 | |
| 350 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 351 | struct list_head cset_link; |
| 352 | |
| 353 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 354 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | /* The default css_set - used by init and its children prior to any |
| 358 | * hierarchies being mounted. It contains a pointer to the root state |
| 359 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 360 | * reference-counted, to improve performance when child cgroups |
| 361 | * haven't been created. |
| 362 | */ |
| 363 | |
| 364 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 365 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 366 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 367 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 368 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 369 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 370 | /* css_set_lock protects the list of css_set objects, and the |
| 371 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 372 | * due to cgroup_iter_start() */ |
| 373 | static DEFINE_RWLOCK(css_set_lock); |
| 374 | static int css_set_count; |
| 375 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 376 | /* |
| 377 | * hash table for cgroup groups. This improves the performance to find |
| 378 | * an existing css_set. This hash doesn't (currently) take into |
| 379 | * account cgroups in empty hierarchies. |
| 380 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 381 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 382 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 383 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 384 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 385 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 386 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 387 | struct cgroup_subsys *ss; |
| 388 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 389 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 390 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 391 | key += (unsigned long)css[i]; |
| 392 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 393 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 394 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 397 | /* We don't maintain the lists running through each css_set to its |
| 398 | * task until after the first call to cgroup_iter_start(). This |
| 399 | * reduces the fork()/exit() overhead for people who have cgroups |
| 400 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 401 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 402 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 403 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 404 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 405 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 406 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Ensure that the refcount doesn't hit zero while any readers |
| 409 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 410 | * rwlock |
| 411 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 412 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 413 | return; |
| 414 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 415 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 416 | write_unlock(&css_set_lock); |
| 417 | return; |
| 418 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 419 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 420 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 421 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 422 | css_set_count--; |
| 423 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 424 | 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] | 425 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 426 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 427 | list_del(&link->cset_link); |
| 428 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 429 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 430 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 431 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 432 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 433 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 434 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 435 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 436 | |
| 437 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 438 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 439 | |
| 440 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 441 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * refcounted get/put for css_set objects |
| 446 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 448 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 449 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 452 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 453 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 454 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 457 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 458 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 459 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 462 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 463 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 464 | * @cset: candidate css_set being tested |
| 465 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 466 | * @new_cgrp: cgroup that's being entered by the task |
| 467 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 468 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 469 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 470 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 471 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 472 | static bool compare_css_sets(struct css_set *cset, |
| 473 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | struct cgroup *new_cgrp, |
| 475 | struct cgroup_subsys_state *template[]) |
| 476 | { |
| 477 | struct list_head *l1, *l2; |
| 478 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 479 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 480 | /* Not all subsystems matched */ |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Compare cgroup pointers in order to distinguish between |
| 486 | * different cgroups in heirarchies with no subsystems. We |
| 487 | * could get by with just this check alone (and skip the |
| 488 | * memcmp above) but on most setups the memcmp check will |
| 489 | * avoid the need for this more expensive check on almost all |
| 490 | * candidates. |
| 491 | */ |
| 492 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 493 | l1 = &cset->cgrp_links; |
| 494 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 495 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 496 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 497 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 498 | |
| 499 | l1 = l1->next; |
| 500 | l2 = l2->next; |
| 501 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 502 | if (l1 == &cset->cgrp_links) { |
| 503 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 504 | break; |
| 505 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 506 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 507 | } |
| 508 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 509 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 510 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 511 | cgrp1 = link1->cgrp; |
| 512 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 513 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 514 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * If this hierarchy is the hierarchy of the cgroup |
| 518 | * that's changing, then we need to check that this |
| 519 | * css_set points to the new cgroup; if it's any other |
| 520 | * hierarchy, then this css_set should point to the |
| 521 | * same cgroup as the old css_set. |
| 522 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 523 | if (cgrp1->root == new_cgrp->root) { |
| 524 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | return true; |
| 532 | } |
| 533 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 534 | /** |
| 535 | * find_existing_css_set - init css array and find the matching css_set |
| 536 | * @old_cset: the css_set that we're using before the cgroup transition |
| 537 | * @cgrp: the cgroup that we're moving into |
| 538 | * @template: out param for the new set of csses, should be clear on entry |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 539 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 540 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 541 | struct cgroup *cgrp, |
| 542 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 543 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 544 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 545 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 546 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 547 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 548 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 549 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 550 | /* |
| 551 | * Build the set of subsystem state objects that we want to see in the |
| 552 | * new css_set. while subsystems can change globally, the entries here |
| 553 | * won't change, so no need for locking. |
| 554 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 555 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 556 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 557 | /* Subsystem is in this hierarchy. So we want |
| 558 | * the subsystem state from the new |
| 559 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 560 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | } else { |
| 562 | /* Subsystem is not in this hierarchy, so we |
| 563 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 564 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 568 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 569 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 570 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 571 | continue; |
| 572 | |
| 573 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 574 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 575 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* No existing cgroup group matched */ |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 582 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 584 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 586 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 587 | kfree(link); |
| 588 | } |
| 589 | } |
| 590 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 591 | /** |
| 592 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 593 | * @count: the number of links to allocate |
| 594 | * @tmp_links: list_head the allocated links are put on |
| 595 | * |
| 596 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 597 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 598 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 599 | 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] | 600 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 601 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 602 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | |
| 604 | INIT_LIST_HEAD(tmp_links); |
| 605 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 606 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 607 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 608 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 609 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 610 | return -ENOMEM; |
| 611 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 612 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 617 | /** |
| 618 | * 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] | 619 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 620 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | * @cgrp: the destination cgroup |
| 622 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 623 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 624 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 625 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 626 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 627 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 628 | BUG_ON(list_empty(tmp_links)); |
| 629 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 630 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 631 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 632 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 633 | /* |
| 634 | * Always add links to the tail of the list so that the list |
| 635 | * is sorted by order of hierarchy creation |
| 636 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 637 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 640 | /** |
| 641 | * find_css_set - return a new css_set with one cgroup updated |
| 642 | * @old_cset: the baseline css_set |
| 643 | * @cgrp: the cgroup to be updated |
| 644 | * |
| 645 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 646 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 647 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 648 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 649 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 650 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 651 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 652 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 653 | struct list_head tmp_links; |
| 654 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 655 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 656 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 657 | lockdep_assert_held(&cgroup_mutex); |
| 658 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 659 | /* First see if we already have a cgroup group that matches |
| 660 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 661 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 662 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 663 | if (cset) |
| 664 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 665 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 666 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 667 | if (cset) |
| 668 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 669 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 670 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 671 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | return NULL; |
| 673 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 674 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 675 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 676 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | return NULL; |
| 678 | } |
| 679 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 680 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 681 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 682 | INIT_LIST_HEAD(&cset->tasks); |
| 683 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 684 | |
| 685 | /* Copy the set of subsystem state objects generated in |
| 686 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 687 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 | |
| 689 | write_lock(&css_set_lock); |
| 690 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 691 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 692 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 693 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 694 | if (c->root == cgrp->root) |
| 695 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 696 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 697 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 698 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 699 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 700 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 701 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 702 | |
| 703 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 704 | key = css_set_hash(cset->subsys); |
| 705 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 706 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 707 | write_unlock(&css_set_lock); |
| 708 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 709 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 712 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 713 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 714 | * called with cgroup_mutex held. |
| 715 | */ |
| 716 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 717 | struct cgroupfs_root *root) |
| 718 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 719 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 720 | struct cgroup *res = NULL; |
| 721 | |
| 722 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 723 | read_lock(&css_set_lock); |
| 724 | /* |
| 725 | * No need to lock the task - since we hold cgroup_mutex the |
| 726 | * task can't change groups, so the only thing that can happen |
| 727 | * is that it exits and its css is set back to init_css_set. |
| 728 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 729 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 730 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 731 | res = &root->top_cgroup; |
| 732 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 733 | struct cgrp_cset_link *link; |
| 734 | |
| 735 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 736 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 737 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 738 | if (c->root == root) { |
| 739 | res = c; |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | read_unlock(&css_set_lock); |
| 745 | BUG_ON(!res); |
| 746 | return res; |
| 747 | } |
| 748 | |
| 749 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 750 | * There is one global cgroup mutex. We also require taking |
| 751 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 752 | * See "The task_lock() exception", at the end of this comment. |
| 753 | * |
| 754 | * A task must hold cgroup_mutex to modify cgroups. |
| 755 | * |
| 756 | * Any task can increment and decrement the count field without lock. |
| 757 | * So in general, code holding cgroup_mutex can't rely on the count |
| 758 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 759 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 760 | * means that no tasks are currently attached, therefore there is no |
| 761 | * way a task attached to that cgroup can fork (the other way to |
| 762 | * increment the count). So code holding cgroup_mutex can safely |
| 763 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 764 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 765 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 766 | * needs that mutex. |
| 767 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 768 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 769 | * (usually) take cgroup_mutex. These are the two most performance |
| 770 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 771 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 772 | * 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] | 773 | * to the release agent with the name of the cgroup (path relative to |
| 774 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 775 | * |
| 776 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 777 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 778 | * tasks in the system use _some_ cgroup, and since there is always at |
| 779 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 780 | * always has either children cgroups and/or using tasks. So we don't |
| 781 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 782 | * |
| 783 | * The task_lock() exception |
| 784 | * |
| 785 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 786 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 787 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 788 | * several performance critical places that need to reference |
| 789 | * task->cgroup without the expense of grabbing a system global |
| 790 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 791 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 792 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 793 | * the task_struct routinely used for such matters. |
| 794 | * |
| 795 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 796 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 797 | */ |
| 798 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | /* |
| 800 | * A couple of forward declarations required, due to cyclic reference loop: |
| 801 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 802 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 803 | * -> cgroup_mkdir. |
| 804 | */ |
| 805 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 806 | 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] | 807 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 808 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 809 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 810 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 811 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 812 | |
| 813 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 814 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 815 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 816 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 817 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 818 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 819 | struct cgroup *parent, struct cgroup *child); |
| 820 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 821 | 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] | 822 | { |
| 823 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 824 | |
| 825 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 826 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 827 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 828 | inode->i_uid = current_fsuid(); |
| 829 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 830 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 831 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 832 | } |
| 833 | return inode; |
| 834 | } |
| 835 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 836 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 837 | { |
| 838 | struct cgroup_name *name; |
| 839 | |
| 840 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 841 | if (!name) |
| 842 | return NULL; |
| 843 | strcpy(name->name, dentry->d_name.name); |
| 844 | return name; |
| 845 | } |
| 846 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 847 | static void cgroup_free_fn(struct work_struct *work) |
| 848 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 849 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 850 | struct cgroup_subsys *ss; |
| 851 | |
| 852 | mutex_lock(&cgroup_mutex); |
| 853 | /* |
| 854 | * Release the subsystem state objects. |
| 855 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 856 | for_each_root_subsys(cgrp->root, ss) |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 857 | ss->css_free(cgrp); |
| 858 | |
| 859 | cgrp->root->number_of_cgroups--; |
| 860 | mutex_unlock(&cgroup_mutex); |
| 861 | |
| 862 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 863 | * We get a ref to the parent's dentry, and put the ref when |
| 864 | * this cgroup is being freed, so it's guaranteed that the |
| 865 | * parent won't be destroyed before its children. |
| 866 | */ |
| 867 | dput(cgrp->parent->dentry); |
| 868 | |
| 869 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 870 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 871 | * created the cgroup. This will free cgrp->root, if we are |
| 872 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 873 | */ |
| 874 | deactivate_super(cgrp->root->sb); |
| 875 | |
| 876 | /* |
| 877 | * if we're getting rid of the cgroup, refcount should ensure |
| 878 | * that there are no pidlists left. |
| 879 | */ |
| 880 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 881 | |
| 882 | simple_xattrs_free(&cgrp->xattrs); |
| 883 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 884 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 885 | kfree(cgrp); |
| 886 | } |
| 887 | |
| 888 | static void cgroup_free_rcu(struct rcu_head *head) |
| 889 | { |
| 890 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 891 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 892 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 893 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 894 | } |
| 895 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 896 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 897 | { |
| 898 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 899 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 900 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 901 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 902 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 903 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 904 | } else { |
| 905 | struct cfent *cfe = __d_cfe(dentry); |
| 906 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 907 | |
| 908 | WARN_ONCE(!list_empty(&cfe->node) && |
| 909 | cgrp != &cgrp->root->top_cgroup, |
| 910 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 911 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 912 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 913 | } |
| 914 | iput(inode); |
| 915 | } |
| 916 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 917 | static int cgroup_delete(const struct dentry *d) |
| 918 | { |
| 919 | return 1; |
| 920 | } |
| 921 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 922 | static void remove_dir(struct dentry *d) |
| 923 | { |
| 924 | struct dentry *parent = dget(d->d_parent); |
| 925 | |
| 926 | d_delete(d); |
| 927 | simple_rmdir(parent->d_inode, d); |
| 928 | dput(parent); |
| 929 | } |
| 930 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 931 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 932 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 933 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 934 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 935 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 936 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 937 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 938 | /* |
| 939 | * If we're doing cleanup due to failure of cgroup_create(), |
| 940 | * the corresponding @cfe may not exist. |
| 941 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 942 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 943 | struct dentry *d = cfe->dentry; |
| 944 | |
| 945 | if (cft && cfe->type != cft) |
| 946 | continue; |
| 947 | |
| 948 | dget(d); |
| 949 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 950 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 951 | list_del_init(&cfe->node); |
| 952 | dput(d); |
| 953 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 954 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 955 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 958 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 959 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 960 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 961 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 962 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 963 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 964 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 965 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 966 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 967 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 968 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 969 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 970 | |
| 971 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 972 | continue; |
| 973 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 974 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 975 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* |
| 979 | * NOTE : the dentry must have been dget()'ed |
| 980 | */ |
| 981 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 982 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 983 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 985 | parent = dentry->d_parent; |
| 986 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 987 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 989 | spin_unlock(&dentry->d_lock); |
| 990 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 991 | remove_dir(dentry); |
| 992 | } |
| 993 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 994 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 995 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 996 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 997 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 998 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 999 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1000 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1001 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1002 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1003 | struct cgroup_subsys *ss; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1004 | unsigned long pinned = 0; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1005 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1007 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1008 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1009 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1010 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1011 | for_each_subsys(ss, i) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1012 | if (!(added_mask & (1 << i))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1014 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1015 | /* is the subsystem mounted elsewhere? */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1016 | if (ss->root != &cgroup_dummy_root) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1017 | ret = -EBUSY; |
| 1018 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1019 | } |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* pin the module */ |
| 1022 | if (!try_module_get(ss->module)) { |
| 1023 | ret = -ENOENT; |
| 1024 | goto out_put; |
| 1025 | } |
| 1026 | pinned |= 1 << i; |
| 1027 | } |
| 1028 | |
| 1029 | /* subsys could be missing if unloaded between parsing and here */ |
| 1030 | if (added_mask != pinned) { |
| 1031 | ret = -ENOENT; |
| 1032 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1035 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1036 | if (ret) |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1037 | goto out_put; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1038 | |
| 1039 | /* |
| 1040 | * Nothing can fail from this point on. Remove files for the |
| 1041 | * removed subsystems and rebind each subsystem. |
| 1042 | */ |
| 1043 | cgroup_clear_dir(cgrp, removed_mask); |
| 1044 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1045 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1046 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1047 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1048 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1049 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1050 | BUG_ON(cgrp->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1051 | BUG_ON(!cgroup_dummy_top->subsys[i]); |
| 1052 | BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1053 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1054 | cgrp->subsys[i] = cgroup_dummy_top->subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1055 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1056 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1057 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1058 | if (ss->bind) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1059 | ss->bind(cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1060 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1061 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1062 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1063 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1064 | /* We're removing this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1065 | BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1066 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1067 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1068 | if (ss->bind) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1069 | ss->bind(cgroup_dummy_top); |
| 1070 | cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1071 | cgrp->subsys[i] = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1072 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1073 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1074 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1075 | /* subsystem is now free - drop reference on module */ |
| 1076 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1077 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1080 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1081 | /* |
| 1082 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1083 | * now matches the bound subsystems. |
| 1084 | */ |
| 1085 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1086 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | return 0; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1088 | |
| 1089 | out_put: |
| 1090 | for_each_subsys(ss, i) |
| 1091 | if (pinned & (1 << i)) |
| 1092 | module_put(ss->module); |
| 1093 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1096 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1098 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1099 | struct cgroup_subsys *ss; |
| 1100 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1101 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1102 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1103 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1104 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1105 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1106 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1107 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1108 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1109 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1110 | if (strlen(root->release_agent_path)) |
| 1111 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1112 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1113 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1114 | if (strlen(root->name)) |
| 1115 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1116 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1121 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1122 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1123 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1124 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1125 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1126 | /* User explicitly requested empty subsystem */ |
| 1127 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1128 | |
| 1129 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1130 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1131 | }; |
| 1132 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1133 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1134 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1135 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1136 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1137 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1138 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1139 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1140 | { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1141 | char *token, *o = data; |
| 1142 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1143 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1144 | struct cgroup_subsys *ss; |
| 1145 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1146 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1147 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1148 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1149 | #ifdef CONFIG_CPUSETS |
| 1150 | mask = ~(1UL << cpuset_subsys_id); |
| 1151 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1152 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1153 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1154 | |
| 1155 | while ((token = strsep(&o, ",")) != NULL) { |
| 1156 | if (!*token) |
| 1157 | return -EINVAL; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1158 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1159 | /* Explicitly have no subsystems */ |
| 1160 | opts->none = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
| 1163 | if (!strcmp(token, "all")) { |
| 1164 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1165 | if (one_ss) |
| 1166 | return -EINVAL; |
| 1167 | all_ss = true; |
| 1168 | continue; |
| 1169 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1170 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1171 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1172 | continue; |
| 1173 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1174 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1175 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1176 | continue; |
| 1177 | } |
| 1178 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1179 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1180 | continue; |
| 1181 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1182 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1183 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1184 | continue; |
| 1185 | } |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1186 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1187 | /* Specifying two release agents is forbidden */ |
| 1188 | if (opts->release_agent) |
| 1189 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1190 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1191 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1192 | if (!opts->release_agent) |
| 1193 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1194 | continue; |
| 1195 | } |
| 1196 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1197 | const char *name = token + 5; |
| 1198 | /* Can't specify an empty name */ |
| 1199 | if (!strlen(name)) |
| 1200 | return -EINVAL; |
| 1201 | /* Must match [\w.-]+ */ |
| 1202 | for (i = 0; i < strlen(name); i++) { |
| 1203 | char c = name[i]; |
| 1204 | if (isalnum(c)) |
| 1205 | continue; |
| 1206 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1207 | continue; |
| 1208 | return -EINVAL; |
| 1209 | } |
| 1210 | /* Specifying two names is forbidden */ |
| 1211 | if (opts->name) |
| 1212 | return -EINVAL; |
| 1213 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1214 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1215 | GFP_KERNEL); |
| 1216 | if (!opts->name) |
| 1217 | return -ENOMEM; |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1218 | |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1222 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1223 | if (strcmp(token, ss->name)) |
| 1224 | continue; |
| 1225 | if (ss->disabled) |
| 1226 | continue; |
| 1227 | |
| 1228 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1229 | if (all_ss) |
| 1230 | return -EINVAL; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1231 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1232 | one_ss = true; |
| 1233 | |
| 1234 | break; |
| 1235 | } |
| 1236 | if (i == CGROUP_SUBSYS_COUNT) |
| 1237 | return -ENOENT; |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1242 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1243 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf2 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1244 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1245 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1246 | for_each_subsys(ss, i) |
| 1247 | if (!ss->disabled) |
| 1248 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1249 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1250 | /* Consistency checks */ |
| 1251 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1252 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1253 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1254 | |
| 1255 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1256 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1257 | return -EINVAL; |
| 1258 | } |
| 1259 | |
| 1260 | if (opts->cpuset_clone_children) { |
| 1261 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1262 | return -EINVAL; |
| 1263 | } |
| 1264 | } |
| 1265 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1266 | /* |
| 1267 | * Option noprefix was introduced just for backward compatibility |
| 1268 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1269 | * the cpuset subsystem. |
| 1270 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1271 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1272 | return -EINVAL; |
| 1273 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1274 | |
| 1275 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1276 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1277 | return -EINVAL; |
| 1278 | |
| 1279 | /* |
| 1280 | * We either have to specify by name or by subsystems. (So all |
| 1281 | * empty hierarchies must have a name). |
| 1282 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1283 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1284 | return -EINVAL; |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1290 | { |
| 1291 | int ret = 0; |
| 1292 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1293 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1294 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1295 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1296 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1297 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1298 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1299 | return -EINVAL; |
| 1300 | } |
| 1301 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1302 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1303 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1304 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1305 | |
| 1306 | /* See what subsystems are wanted */ |
| 1307 | ret = parse_cgroupfs_options(data, &opts); |
| 1308 | if (ret) |
| 1309 | goto out_unlock; |
| 1310 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1311 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1312 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1313 | task_tgid_nr(current), current->comm); |
| 1314 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1315 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1316 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1317 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1318 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1319 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1320 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1321 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1322 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1323 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1324 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1325 | goto out_unlock; |
| 1326 | } |
| 1327 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1328 | /* remounting is not allowed for populated hierarchies */ |
| 1329 | if (root->number_of_cgroups > 1) { |
| 1330 | ret = -EBUSY; |
| 1331 | goto out_unlock; |
| 1332 | } |
| 1333 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1334 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1335 | if (ret) |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1336 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1337 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1338 | if (opts.release_agent) |
| 1339 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1340 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1341 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1342 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1343 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1345 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1346 | return ret; |
| 1347 | } |
| 1348 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1349 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1350 | .statfs = simple_statfs, |
| 1351 | .drop_inode = generic_delete_inode, |
| 1352 | .show_options = cgroup_show_options, |
| 1353 | .remount_fs = cgroup_remount, |
| 1354 | }; |
| 1355 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1356 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1357 | { |
| 1358 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1359 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1360 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1361 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1362 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1363 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1364 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1365 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1366 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1367 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1368 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1369 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1370 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1371 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1372 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1373 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1374 | INIT_LIST_HEAD(&root->subsys_list); |
| 1375 | INIT_LIST_HEAD(&root->root_list); |
| 1376 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1377 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1378 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1379 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1380 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1383 | static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1384 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1385 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1386 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1387 | lockdep_assert_held(&cgroup_mutex); |
| 1388 | lockdep_assert_held(&cgroup_root_mutex); |
| 1389 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1390 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1391 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1392 | if (id < 0) |
| 1393 | return id; |
| 1394 | |
| 1395 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1400 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1401 | lockdep_assert_held(&cgroup_mutex); |
| 1402 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1403 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1404 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1405 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1406 | root->hierarchy_id = 0; |
| 1407 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1410 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1411 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1412 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1413 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1414 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1415 | /* If we asked for a name then it must match */ |
| 1416 | if (opts->name && strcmp(opts->name, root->name)) |
| 1417 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1418 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * If we asked for subsystems (or explicitly for no |
| 1421 | * subsystems) then they must match |
| 1422 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1423 | if ((opts->subsys_mask || opts->none) |
| 1424 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1425 | return 0; |
| 1426 | |
| 1427 | return 1; |
| 1428 | } |
| 1429 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1430 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1431 | { |
| 1432 | struct cgroupfs_root *root; |
| 1433 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1434 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1435 | return NULL; |
| 1436 | |
| 1437 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1438 | if (!root) |
| 1439 | return ERR_PTR(-ENOMEM); |
| 1440 | |
| 1441 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1442 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1443 | /* |
| 1444 | * We need to set @root->subsys_mask now so that @root can be |
| 1445 | * matched by cgroup_test_super() before it finishes |
| 1446 | * initialization; otherwise, competing mounts with the same |
| 1447 | * options may try to bind the same subsystems instead of waiting |
| 1448 | * for the first one leading to unexpected mount errors. |
| 1449 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1450 | */ |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1451 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1452 | root->flags = opts->flags; |
| 1453 | if (opts->release_agent) |
| 1454 | strcpy(root->release_agent_path, opts->release_agent); |
| 1455 | if (opts->name) |
| 1456 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1457 | if (opts->cpuset_clone_children) |
| 1458 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1459 | return root; |
| 1460 | } |
| 1461 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1462 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1463 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1464 | if (root) { |
| 1465 | /* hierarhcy ID shoulid already have been released */ |
| 1466 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1467 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1468 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1469 | kfree(root); |
| 1470 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1473 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1474 | { |
| 1475 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1476 | struct cgroup_sb_opts *opts = data; |
| 1477 | |
| 1478 | /* If we don't have a new root, we can't set up a new sb */ |
| 1479 | if (!opts->new_root) |
| 1480 | return -EINVAL; |
| 1481 | |
Aristeu Rozanski | a1a71b45 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1482 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1483 | |
| 1484 | ret = set_anon_super(sb, NULL); |
| 1485 | if (ret) |
| 1486 | return ret; |
| 1487 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1488 | sb->s_fs_info = opts->new_root; |
| 1489 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1490 | |
| 1491 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1492 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1493 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1494 | sb->s_op = &cgroup_ops; |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1500 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1501 | static const struct dentry_operations cgroup_dops = { |
| 1502 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1503 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1504 | }; |
| 1505 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1506 | struct inode *inode = |
| 1507 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1508 | |
| 1509 | if (!inode) |
| 1510 | return -ENOMEM; |
| 1511 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1512 | inode->i_fop = &simple_dir_operations; |
| 1513 | inode->i_op = &cgroup_dir_inode_operations; |
| 1514 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1515 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1516 | sb->s_root = d_make_root(inode); |
| 1517 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1518 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1519 | /* for everything else we want ->d_op set */ |
| 1520 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1521 | return 0; |
| 1522 | } |
| 1523 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1524 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1525 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1526 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1527 | { |
| 1528 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1529 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1530 | int ret = 0; |
| 1531 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1532 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1533 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1534 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1535 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1536 | |
| 1537 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1538 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1539 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1540 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1541 | if (ret) |
| 1542 | goto out_err; |
| 1543 | |
| 1544 | /* |
| 1545 | * Allocate a new cgroup root. We may not need it if we're |
| 1546 | * reusing an existing hierarchy. |
| 1547 | */ |
| 1548 | new_root = cgroup_root_from_opts(&opts); |
| 1549 | if (IS_ERR(new_root)) { |
| 1550 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1551 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1552 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1553 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1554 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1555 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1556 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1558 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1559 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1560 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1563 | root = sb->s_fs_info; |
| 1564 | BUG_ON(!root); |
| 1565 | if (root == opts.new_root) { |
| 1566 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1567 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1568 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1569 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1570 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1571 | |
| 1572 | BUG_ON(sb->s_root != NULL); |
| 1573 | |
| 1574 | ret = cgroup_get_rootdir(sb); |
| 1575 | if (ret) |
| 1576 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1577 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1578 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1579 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1580 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1581 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1582 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1583 | root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp, |
| 1584 | 0, 1, GFP_KERNEL); |
| 1585 | if (root_cgrp->id < 0) |
| 1586 | goto unlock_drop; |
| 1587 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1588 | /* Check for name clashes with existing mounts */ |
| 1589 | ret = -EBUSY; |
| 1590 | if (strlen(root->name)) |
| 1591 | for_each_active_root(existing_root) |
| 1592 | if (!strcmp(existing_root->name, root->name)) |
| 1593 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1594 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1595 | /* |
| 1596 | * We're accessing css_set_count without locking |
| 1597 | * css_set_lock here, but that's OK - it can only be |
| 1598 | * increased by someone holding cgroup_lock, and |
| 1599 | * that's us. The worst that can happen is that we |
| 1600 | * have some link structures left over |
| 1601 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1602 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1603 | if (ret) |
| 1604 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1605 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1606 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1607 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1608 | if (ret) |
| 1609 | goto unlock_drop; |
| 1610 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1611 | sb->s_root->d_fsdata = root_cgrp; |
| 1612 | root_cgrp->dentry = sb->s_root; |
| 1613 | |
| 1614 | /* |
| 1615 | * We're inside get_sb() and will call lookup_one_len() to |
| 1616 | * create the root files, which doesn't work if SELinux is |
| 1617 | * in use. The following cred dancing somehow works around |
| 1618 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1619 | * populating new cgroupfs mount") for more details. |
| 1620 | */ |
| 1621 | cred = override_creds(&init_cred); |
| 1622 | |
| 1623 | ret = cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true); |
| 1624 | if (ret) |
| 1625 | goto rm_base_files; |
| 1626 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1627 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1628 | if (ret) |
| 1629 | goto rm_base_files; |
| 1630 | |
| 1631 | revert_creds(cred); |
| 1632 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1633 | /* |
| 1634 | * There must be no failure case after here, since rebinding |
| 1635 | * takes care of subsystems' refcounts, which are explicitly |
| 1636 | * dropped in the failure exit path. |
| 1637 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1638 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1639 | list_add(&root->root_list, &cgroup_roots); |
| 1640 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1641 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1642 | /* Link the top cgroup in this hierarchy into all |
| 1643 | * the css_set objects */ |
| 1644 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1645 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1646 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1647 | write_unlock(&css_set_lock); |
| 1648 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1649 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1650 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1651 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1652 | BUG_ON(root->number_of_cgroups != 1); |
| 1653 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1654 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1656 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1657 | } else { |
| 1658 | /* |
| 1659 | * We re-used an existing hierarchy - the new root (if |
| 1660 | * any) is not needed |
| 1661 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1662 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1663 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1664 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1665 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1666 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1667 | ret = -EINVAL; |
| 1668 | goto drop_new_super; |
| 1669 | } else { |
| 1670 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1671 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1672 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1675 | kfree(opts.release_agent); |
| 1676 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1677 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1678 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1679 | rm_base_files: |
| 1680 | free_cgrp_cset_links(&tmp_links); |
| 1681 | cgroup_addrm_files(&root->top_cgroup, NULL, cgroup_base_files, false); |
| 1682 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1683 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1684 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1685 | mutex_unlock(&cgroup_root_mutex); |
| 1686 | mutex_unlock(&cgroup_mutex); |
| 1687 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1688 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1689 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1690 | out_err: |
| 1691 | kfree(opts.release_agent); |
| 1692 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1693 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1697 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1698 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1699 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1700 | int ret; |
| 1701 | |
| 1702 | BUG_ON(!root); |
| 1703 | |
| 1704 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1705 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1706 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1707 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1709 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1710 | |
| 1711 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1712 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1713 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1714 | /* Shouldn't be able to fail ... */ |
| 1715 | BUG_ON(ret); |
| 1716 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1718 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1719 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1720 | * root cgroup |
| 1721 | */ |
| 1722 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1723 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1724 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1725 | list_del(&link->cset_link); |
| 1726 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1727 | kfree(link); |
| 1728 | } |
| 1729 | write_unlock(&css_set_lock); |
| 1730 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1731 | if (!list_empty(&root->root_list)) { |
| 1732 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1733 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1734 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1735 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1736 | cgroup_exit_root_id(root); |
| 1737 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1738 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1739 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1740 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1741 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1742 | simple_xattrs_free(&cgrp->xattrs); |
| 1743 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1744 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1745 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | static struct file_system_type cgroup_fs_type = { |
| 1749 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1750 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1751 | .kill_sb = cgroup_kill_sb, |
| 1752 | }; |
| 1753 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1754 | static struct kobject *cgroup_kobj; |
| 1755 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1756 | /** |
| 1757 | * cgroup_path - generate the path of a cgroup |
| 1758 | * @cgrp: the cgroup in question |
| 1759 | * @buf: the buffer to write the path into |
| 1760 | * @buflen: the length of the buffer |
| 1761 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1762 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1763 | * |
| 1764 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1765 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1766 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1767 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1768 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1769 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1770 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1771 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1772 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1773 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1774 | if (!cgrp->parent) { |
| 1775 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1776 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1777 | return 0; |
| 1778 | } |
| 1779 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1780 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1781 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1782 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1783 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1784 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1785 | const char *name = cgroup_name(cgrp); |
| 1786 | int len; |
| 1787 | |
| 1788 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1789 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1790 | goto out; |
| 1791 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1792 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1793 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1794 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1795 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1796 | |
| 1797 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1798 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1799 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1800 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1801 | out: |
| 1802 | rcu_read_unlock(); |
| 1803 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1804 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1805 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1806 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1807 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1808 | * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1809 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1810 | * @buf: the buffer to write the path into |
| 1811 | * @buflen: the length of the buffer |
| 1812 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1813 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1814 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1815 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1816 | * cgroup controller callbacks. |
| 1817 | * |
| 1818 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1819 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1820 | int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1821 | { |
| 1822 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1823 | struct cgroup *cgrp; |
| 1824 | int hierarchy_id = 1, ret = 0; |
| 1825 | |
| 1826 | if (buflen < 2) |
| 1827 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1828 | |
| 1829 | mutex_lock(&cgroup_mutex); |
| 1830 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1831 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1832 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1833 | if (root) { |
| 1834 | cgrp = task_cgroup_from_root(task, root); |
| 1835 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1836 | } else { |
| 1837 | /* if no hierarchy exists, everyone is in "/" */ |
| 1838 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1842 | return ret; |
| 1843 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1844 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1845 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1846 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1847 | * Control Group taskset |
| 1848 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1849 | struct task_and_cgroup { |
| 1850 | struct task_struct *task; |
| 1851 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1852 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1853 | }; |
| 1854 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | struct cgroup_taskset { |
| 1856 | struct task_and_cgroup single; |
| 1857 | struct flex_array *tc_array; |
| 1858 | int tc_array_len; |
| 1859 | int idx; |
| 1860 | struct cgroup *cur_cgrp; |
| 1861 | }; |
| 1862 | |
| 1863 | /** |
| 1864 | * cgroup_taskset_first - reset taskset and return the first task |
| 1865 | * @tset: taskset of interest |
| 1866 | * |
| 1867 | * @tset iteration is initialized and the first task is returned. |
| 1868 | */ |
| 1869 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1870 | { |
| 1871 | if (tset->tc_array) { |
| 1872 | tset->idx = 0; |
| 1873 | return cgroup_taskset_next(tset); |
| 1874 | } else { |
| 1875 | tset->cur_cgrp = tset->single.cgrp; |
| 1876 | return tset->single.task; |
| 1877 | } |
| 1878 | } |
| 1879 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1880 | |
| 1881 | /** |
| 1882 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1883 | * @tset: taskset of interest |
| 1884 | * |
| 1885 | * Return the next task in @tset. Iteration must have been initialized |
| 1886 | * with cgroup_taskset_first(). |
| 1887 | */ |
| 1888 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1889 | { |
| 1890 | struct task_and_cgroup *tc; |
| 1891 | |
| 1892 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1893 | return NULL; |
| 1894 | |
| 1895 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1896 | tset->cur_cgrp = tc->cgrp; |
| 1897 | return tc->task; |
| 1898 | } |
| 1899 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1900 | |
| 1901 | /** |
| 1902 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1903 | * @tset: taskset of interest |
| 1904 | * |
| 1905 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1906 | * function must be preceded by either cgroup_taskset_first() or |
| 1907 | * cgroup_taskset_next(). |
| 1908 | */ |
| 1909 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1910 | { |
| 1911 | return tset->cur_cgrp; |
| 1912 | } |
| 1913 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1914 | |
| 1915 | /** |
| 1916 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1917 | * @tset: taskset of interest |
| 1918 | */ |
| 1919 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1920 | { |
| 1921 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1922 | } |
| 1923 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1924 | |
| 1925 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1926 | /* |
| 1927 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1928 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1929 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1930 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1931 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1932 | struct task_struct *tsk, |
| 1933 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1935 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | |
| 1937 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1938 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1939 | * setting such that we can't race against cgroup_exit() changing the |
| 1940 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1941 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1942 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1943 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1945 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1946 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1947 | task_unlock(tsk); |
| 1948 | |
| 1949 | /* Update the css_set linked lists if we're using them */ |
| 1950 | write_lock(&css_set_lock); |
| 1951 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1952 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | write_unlock(&css_set_lock); |
| 1954 | |
| 1955 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1956 | * We just gained a reference on old_cset by taking it from the |
| 1957 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1958 | * 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] | 1959 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1960 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1961 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1964 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1965 | * 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] | 1966 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1967 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1968 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1969 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1970 | * 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] | 1971 | * 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] | 1972 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1973 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1974 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | { |
| 1976 | int retval, i, group_size; |
| 1977 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1978 | struct cgroupfs_root *root = cgrp->root; |
| 1979 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1980 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1981 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1982 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1983 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 | |
| 1985 | /* |
| 1986 | * step 0: in order to do expensive, possibly blocking operations for |
| 1987 | * every thread, we cannot iterate the thread group list, since it needs |
| 1988 | * 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] | 1989 | * group - group_rwsem prevents new threads from appearing, and if |
| 1990 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1991 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1992 | if (threadgroup) |
| 1993 | group_size = get_nr_threads(tsk); |
| 1994 | else |
| 1995 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1996 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1997 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1998 | if (!group) |
| 1999 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2000 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2001 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2002 | if (retval) |
| 2003 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2004 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2005 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2006 | /* |
| 2007 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2008 | * already PF_EXITING could be freed from underneath us unless we |
| 2009 | * take an rcu_read_lock. |
| 2010 | */ |
| 2011 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2013 | struct task_and_cgroup ent; |
| 2014 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2015 | /* @tsk either already exited or can't exit until the end */ |
| 2016 | if (tsk->flags & PF_EXITING) |
| 2017 | continue; |
| 2018 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2019 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2020 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2021 | ent.task = tsk; |
| 2022 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2023 | /* nothing to do if this task is already in the cgroup */ |
| 2024 | if (ent.cgrp == cgrp) |
| 2025 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2026 | /* |
| 2027 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2028 | * earlier, but it's good form to communicate our expectations. |
| 2029 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2030 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2031 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2032 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2033 | |
| 2034 | if (!threadgroup) |
| 2035 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2036 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2037 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2038 | /* remember the number of threads in the array for later. */ |
| 2039 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2040 | tset.tc_array = group; |
| 2041 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2043 | /* methods shouldn't be called if no task is actually migrating */ |
| 2044 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2045 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2046 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2047 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | /* |
| 2049 | * step 1: check that we can legitimately attach to the cgroup. |
| 2050 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2051 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2052 | if (ss->can_attach) { |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2053 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2054 | if (retval) { |
| 2055 | failed_ss = ss; |
| 2056 | goto out_cancel_attach; |
| 2057 | } |
| 2058 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2063 | * we use find_css_set, which allocates a new one if necessary. |
| 2064 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2066 | struct css_set *old_cset; |
| 2067 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2068 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2069 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2070 | tc->cset = find_css_set(old_cset, cgrp); |
| 2071 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2072 | retval = -ENOMEM; |
| 2073 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2078 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2079 | * proceed to move all tasks to the new cgroup. There are no |
| 2080 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2081 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2082 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2083 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2084 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | } |
| 2086 | /* nothing is sensitive to fork() after this point. */ |
| 2087 | |
| 2088 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2089 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2090 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2091 | for_each_root_subsys(root, ss) { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | if (ss->attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2093 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * step 5: success! and cleanup |
| 2098 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2099 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2100 | out_put_css_set_refs: |
| 2101 | if (retval) { |
| 2102 | for (i = 0; i < group_size; i++) { |
| 2103 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2104 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2105 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2106 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2107 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2108 | } |
| 2109 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2110 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2111 | for_each_root_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2112 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2113 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2114 | if (ss->cancel_attach) |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2115 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2116 | } |
| 2117 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2118 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2119 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2120 | return retval; |
| 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * 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] | 2125 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2126 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2127 | */ |
| 2128 | 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] | 2129 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2130 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2131 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2132 | int ret; |
| 2133 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | if (!cgroup_lock_live_group(cgrp)) |
| 2135 | return -ENODEV; |
| 2136 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2137 | retry_find_task: |
| 2138 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2139 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2140 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2142 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2143 | ret= -ESRCH; |
| 2144 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2145 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2146 | /* |
| 2147 | * even if we're attaching all tasks in the thread group, we |
| 2148 | * only need to check permissions on one of them. |
| 2149 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2150 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2151 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2152 | !uid_eq(cred->euid, tcred->uid) && |
| 2153 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2154 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2155 | ret = -EACCES; |
| 2156 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2157 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2158 | } else |
| 2159 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2160 | |
| 2161 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2162 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2163 | |
| 2164 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2165 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2166 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2167 | * with no rt_runtime allocated. Just say no. |
| 2168 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2169 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2170 | ret = -EINVAL; |
| 2171 | rcu_read_unlock(); |
| 2172 | goto out_unlock_cgroup; |
| 2173 | } |
| 2174 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2175 | get_task_struct(tsk); |
| 2176 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2177 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2178 | threadgroup_lock(tsk); |
| 2179 | if (threadgroup) { |
| 2180 | if (!thread_group_leader(tsk)) { |
| 2181 | /* |
| 2182 | * a race with de_thread from another thread's exec() |
| 2183 | * may strip us of our leadership, if this happens, |
| 2184 | * there is no choice but to throw this task away and |
| 2185 | * try again; this is |
| 2186 | * "double-double-toil-and-trouble-check locking". |
| 2187 | */ |
| 2188 | threadgroup_unlock(tsk); |
| 2189 | put_task_struct(tsk); |
| 2190 | goto retry_find_task; |
| 2191 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2195 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2196 | threadgroup_unlock(tsk); |
| 2197 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2198 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2199 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2200 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2201 | return ret; |
| 2202 | } |
| 2203 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2204 | /** |
| 2205 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2206 | * @from: attach to all cgroups of a given task |
| 2207 | * @tsk: the task to be attached |
| 2208 | */ |
| 2209 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2210 | { |
| 2211 | struct cgroupfs_root *root; |
| 2212 | int retval = 0; |
| 2213 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2214 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2215 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2216 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2217 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2218 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2219 | if (retval) |
| 2220 | break; |
| 2221 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2222 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2223 | |
| 2224 | return retval; |
| 2225 | } |
| 2226 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2227 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2228 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2229 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2230 | return attach_task_by_pid(cgrp, pid, false); |
| 2231 | } |
| 2232 | |
| 2233 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2234 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2235 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2238 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2239 | const char *buffer) |
| 2240 | { |
| 2241 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2242 | if (strlen(buffer) >= PATH_MAX) |
| 2243 | return -EINVAL; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2244 | if (!cgroup_lock_live_group(cgrp)) |
| 2245 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2246 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2247 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2248 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2249 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2254 | struct seq_file *seq) |
| 2255 | { |
| 2256 | if (!cgroup_lock_live_group(cgrp)) |
| 2257 | return -ENODEV; |
| 2258 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2259 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2260 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2261 | return 0; |
| 2262 | } |
| 2263 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2264 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2265 | struct seq_file *seq) |
| 2266 | { |
| 2267 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2268 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2271 | /* A buffer size big enough for numbers or short strings */ |
| 2272 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2273 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2274 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2275 | struct file *file, |
| 2276 | const char __user *userbuf, |
| 2277 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2278 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2279 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2280 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2281 | char *end; |
| 2282 | |
| 2283 | if (!nbytes) |
| 2284 | return -EINVAL; |
| 2285 | if (nbytes >= sizeof(buffer)) |
| 2286 | return -E2BIG; |
| 2287 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2288 | return -EFAULT; |
| 2289 | |
| 2290 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2291 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2292 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2293 | if (*end) |
| 2294 | return -EINVAL; |
| 2295 | retval = cft->write_u64(cgrp, cft, val); |
| 2296 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2297 | s64 val = simple_strtoll(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_s64(cgrp, cft, val); |
| 2301 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2302 | if (!retval) |
| 2303 | retval = nbytes; |
| 2304 | return retval; |
| 2305 | } |
| 2306 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2307 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2308 | struct file *file, |
| 2309 | const char __user *userbuf, |
| 2310 | size_t nbytes, loff_t *unused_ppos) |
| 2311 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2312 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2313 | int retval = 0; |
| 2314 | size_t max_bytes = cft->max_write_len; |
| 2315 | char *buffer = local_buffer; |
| 2316 | |
| 2317 | if (!max_bytes) |
| 2318 | max_bytes = sizeof(local_buffer) - 1; |
| 2319 | if (nbytes >= max_bytes) |
| 2320 | return -E2BIG; |
| 2321 | /* Allocate a dynamic buffer if we need one */ |
| 2322 | if (nbytes >= sizeof(local_buffer)) { |
| 2323 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2324 | if (buffer == NULL) |
| 2325 | return -ENOMEM; |
| 2326 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2327 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2328 | retval = -EFAULT; |
| 2329 | goto out; |
| 2330 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2331 | |
| 2332 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2333 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2334 | if (!retval) |
| 2335 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2336 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2337 | if (buffer != local_buffer) |
| 2338 | kfree(buffer); |
| 2339 | return retval; |
| 2340 | } |
| 2341 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2342 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2343 | size_t nbytes, loff_t *ppos) |
| 2344 | { |
| 2345 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2346 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2347 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2348 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2349 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2350 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2351 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2352 | if (cft->write_u64 || cft->write_s64) |
| 2353 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2354 | if (cft->write_string) |
| 2355 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2356 | if (cft->trigger) { |
| 2357 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2358 | return ret ? ret : nbytes; |
| 2359 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2360 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2363 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2364 | struct file *file, |
| 2365 | char __user *buf, size_t nbytes, |
| 2366 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2367 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2368 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2369 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2370 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2371 | |
| 2372 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2373 | } |
| 2374 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2375 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2376 | struct file *file, |
| 2377 | char __user *buf, size_t nbytes, |
| 2378 | loff_t *ppos) |
| 2379 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2380 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2381 | s64 val = cft->read_s64(cgrp, cft); |
| 2382 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2383 | |
| 2384 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2385 | } |
| 2386 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2387 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2388 | size_t nbytes, loff_t *ppos) |
| 2389 | { |
| 2390 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2391 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2392 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2393 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2394 | return -ENODEV; |
| 2395 | |
| 2396 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2397 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2398 | if (cft->read_u64) |
| 2399 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2400 | if (cft->read_s64) |
| 2401 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2402 | return -EINVAL; |
| 2403 | } |
| 2404 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2405 | /* |
| 2406 | * seqfile ops/methods for returning structured data. Currently just |
| 2407 | * supports string->u64 maps, but can be extended in future. |
| 2408 | */ |
| 2409 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2410 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2411 | { |
| 2412 | struct seq_file *sf = cb->state; |
| 2413 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2414 | } |
| 2415 | |
| 2416 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2417 | { |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2418 | struct cfent *cfe = m->private; |
| 2419 | struct cftype *cft = cfe->type; |
| 2420 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2421 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2422 | if (cft->read_map) { |
| 2423 | struct cgroup_map_cb cb = { |
| 2424 | .fill = cgroup_map_add, |
| 2425 | .state = m, |
| 2426 | }; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2427 | return cft->read_map(cgrp, cft, &cb); |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2428 | } |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2429 | return cft->read_seq_string(cgrp, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2432 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2433 | .read = seq_read, |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2434 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2435 | .llseek = seq_lseek, |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2436 | .release = single_release, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2437 | }; |
| 2438 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2439 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2440 | { |
| 2441 | int err; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2442 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2443 | struct cftype *cft; |
| 2444 | |
| 2445 | err = generic_file_open(inode, file); |
| 2446 | if (err) |
| 2447 | return err; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2448 | cfe = __d_cfe(file->f_dentry); |
| 2449 | cft = cfe->type; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2450 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2451 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2452 | file->f_op = &cgroup_seqfile_operations; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2453 | err = single_open(file, cgroup_seqfile_show, cfe); |
| 2454 | } else if (cft->open) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2455 | err = cft->open(inode, file); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2456 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2457 | |
| 2458 | return err; |
| 2459 | } |
| 2460 | |
| 2461 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2462 | { |
| 2463 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2464 | if (cft->release) |
| 2465 | return cft->release(inode, file); |
| 2466 | return 0; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2471 | */ |
| 2472 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2473 | struct inode *new_dir, struct dentry *new_dentry) |
| 2474 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2475 | int ret; |
| 2476 | struct cgroup_name *name, *old_name; |
| 2477 | struct cgroup *cgrp; |
| 2478 | |
| 2479 | /* |
| 2480 | * It's convinient to use parent dir's i_mutex to protected |
| 2481 | * cgrp->name. |
| 2482 | */ |
| 2483 | lockdep_assert_held(&old_dir->i_mutex); |
| 2484 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2485 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2486 | return -ENOTDIR; |
| 2487 | if (new_dentry->d_inode) |
| 2488 | return -EEXIST; |
| 2489 | if (old_dir != new_dir) |
| 2490 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2491 | |
| 2492 | cgrp = __d_cgrp(old_dentry); |
| 2493 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2494 | /* |
| 2495 | * This isn't a proper migration and its usefulness is very |
| 2496 | * limited. Disallow if sane_behavior. |
| 2497 | */ |
| 2498 | if (cgroup_sane_behavior(cgrp)) |
| 2499 | return -EPERM; |
| 2500 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2501 | name = cgroup_alloc_name(new_dentry); |
| 2502 | if (!name) |
| 2503 | return -ENOMEM; |
| 2504 | |
| 2505 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2506 | if (ret) { |
| 2507 | kfree(name); |
| 2508 | return ret; |
| 2509 | } |
| 2510 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2511 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2512 | rcu_assign_pointer(cgrp->name, name); |
| 2513 | |
| 2514 | kfree_rcu(old_name, rcu_head); |
| 2515 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2518 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2519 | { |
| 2520 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2521 | return &__d_cgrp(dentry)->xattrs; |
| 2522 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2523 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | static inline int xattr_enabled(struct dentry *dentry) |
| 2527 | { |
| 2528 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2529 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | static bool is_valid_xattr(const char *name) |
| 2533 | { |
| 2534 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2535 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2536 | return true; |
| 2537 | return false; |
| 2538 | } |
| 2539 | |
| 2540 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2541 | const void *val, size_t size, int flags) |
| 2542 | { |
| 2543 | if (!xattr_enabled(dentry)) |
| 2544 | return -EOPNOTSUPP; |
| 2545 | if (!is_valid_xattr(name)) |
| 2546 | return -EINVAL; |
| 2547 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2548 | } |
| 2549 | |
| 2550 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2551 | { |
| 2552 | if (!xattr_enabled(dentry)) |
| 2553 | return -EOPNOTSUPP; |
| 2554 | if (!is_valid_xattr(name)) |
| 2555 | return -EINVAL; |
| 2556 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2557 | } |
| 2558 | |
| 2559 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2560 | void *buf, size_t size) |
| 2561 | { |
| 2562 | if (!xattr_enabled(dentry)) |
| 2563 | return -EOPNOTSUPP; |
| 2564 | if (!is_valid_xattr(name)) |
| 2565 | return -EINVAL; |
| 2566 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2567 | } |
| 2568 | |
| 2569 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2570 | { |
| 2571 | if (!xattr_enabled(dentry)) |
| 2572 | return -EOPNOTSUPP; |
| 2573 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2574 | } |
| 2575 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2576 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2577 | .read = cgroup_file_read, |
| 2578 | .write = cgroup_file_write, |
| 2579 | .llseek = generic_file_llseek, |
| 2580 | .open = cgroup_file_open, |
| 2581 | .release = cgroup_file_release, |
| 2582 | }; |
| 2583 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2584 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2585 | .setxattr = cgroup_setxattr, |
| 2586 | .getxattr = cgroup_getxattr, |
| 2587 | .listxattr = cgroup_listxattr, |
| 2588 | .removexattr = cgroup_removexattr, |
| 2589 | }; |
| 2590 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2591 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2592 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2593 | .mkdir = cgroup_mkdir, |
| 2594 | .rmdir = cgroup_rmdir, |
| 2595 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2596 | .setxattr = cgroup_setxattr, |
| 2597 | .getxattr = cgroup_getxattr, |
| 2598 | .listxattr = cgroup_listxattr, |
| 2599 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2600 | }; |
| 2601 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2602 | 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] | 2603 | { |
| 2604 | if (dentry->d_name.len > NAME_MAX) |
| 2605 | return ERR_PTR(-ENAMETOOLONG); |
| 2606 | d_add(dentry, NULL); |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2610 | /* |
| 2611 | * Check if a file is a control file |
| 2612 | */ |
| 2613 | static inline struct cftype *__file_cft(struct file *file) |
| 2614 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2615 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2616 | return ERR_PTR(-EINVAL); |
| 2617 | return __d_cft(file->f_dentry); |
| 2618 | } |
| 2619 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2620 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2621 | struct super_block *sb) |
| 2622 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2623 | struct inode *inode; |
| 2624 | |
| 2625 | if (!dentry) |
| 2626 | return -ENOENT; |
| 2627 | if (dentry->d_inode) |
| 2628 | return -EEXIST; |
| 2629 | |
| 2630 | inode = cgroup_new_inode(mode, sb); |
| 2631 | if (!inode) |
| 2632 | return -ENOMEM; |
| 2633 | |
| 2634 | if (S_ISDIR(mode)) { |
| 2635 | inode->i_op = &cgroup_dir_inode_operations; |
| 2636 | inode->i_fop = &simple_dir_operations; |
| 2637 | |
| 2638 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2639 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2640 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2641 | |
Tejun Heo | b8a2df6 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2642 | /* |
| 2643 | * Control reaches here with cgroup_mutex held. |
| 2644 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2645 | * want to populate it immediately without releasing |
| 2646 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2647 | * yet, trylock will always succeed without affecting |
| 2648 | * lockdep checks. |
| 2649 | */ |
| 2650 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2651 | } else if (S_ISREG(mode)) { |
| 2652 | inode->i_size = 0; |
| 2653 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2654 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2655 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2656 | d_instantiate(dentry, inode); |
| 2657 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2661 | /** |
| 2662 | * cgroup_file_mode - deduce file mode of a control file |
| 2663 | * @cft: the control file in question |
| 2664 | * |
| 2665 | * returns cft->mode if ->mode is not 0 |
| 2666 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2667 | * returns S_IRUGO if it has only a read handler |
| 2668 | * returns S_IWUSR if it has only a write hander |
| 2669 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2670 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2671 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2672 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2673 | |
| 2674 | if (cft->mode) |
| 2675 | return cft->mode; |
| 2676 | |
| 2677 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2678 | cft->read_map || cft->read_seq_string) |
| 2679 | mode |= S_IRUGO; |
| 2680 | |
| 2681 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2682 | cft->write_string || cft->trigger) |
| 2683 | mode |= S_IWUSR; |
| 2684 | |
| 2685 | return mode; |
| 2686 | } |
| 2687 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2688 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2689 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2690 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2691 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2692 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2693 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2694 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2695 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2696 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2697 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2698 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2699 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2700 | strcpy(name, subsys->name); |
| 2701 | strcat(name, "."); |
| 2702 | } |
| 2703 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2704 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2705 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2706 | |
| 2707 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2708 | if (!cfe) |
| 2709 | return -ENOMEM; |
| 2710 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2711 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2712 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2713 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2714 | goto out; |
| 2715 | } |
| 2716 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2717 | cfe->type = (void *)cft; |
| 2718 | cfe->dentry = dentry; |
| 2719 | dentry->d_fsdata = cfe; |
| 2720 | simple_xattrs_init(&cfe->xattrs); |
| 2721 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2722 | mode = cgroup_file_mode(cft); |
| 2723 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2724 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2725 | list_add_tail(&cfe->node, &parent->files); |
| 2726 | cfe = NULL; |
| 2727 | } |
| 2728 | dput(dentry); |
| 2729 | out: |
| 2730 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2731 | return error; |
| 2732 | } |
| 2733 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2734 | /** |
| 2735 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2736 | * @cgrp: the target cgroup |
| 2737 | * @subsys: the subsystem of files to be added |
| 2738 | * @cfts: array of cftypes to be added |
| 2739 | * @is_add: whether to add or remove |
| 2740 | * |
| 2741 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
| 2742 | * All @cfts should belong to @subsys. For removals, this function never |
| 2743 | * fails. If addition fails, this function doesn't remove files already |
| 2744 | * added. The caller is responsible for cleaning up. |
| 2745 | */ |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2746 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2747 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2748 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2749 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2750 | int ret; |
| 2751 | |
| 2752 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2753 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2754 | |
| 2755 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2756 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2757 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2758 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2759 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2760 | continue; |
| 2761 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2762 | continue; |
| 2763 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2764 | if (is_add) { |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2765 | ret = cgroup_add_file(cgrp, subsys, cft); |
| 2766 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2767 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2768 | cft->name, ret); |
| 2769 | return ret; |
| 2770 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2771 | } else { |
| 2772 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2773 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2774 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2775 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2778 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2779 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2780 | { |
| 2781 | /* |
| 2782 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2783 | * the existing cgroups under cgroup_mutex and create files. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2784 | * Instead, we use cgroup_for_each_descendant_pre() and drop RCU |
| 2785 | * read lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2786 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2787 | mutex_lock(&cgroup_mutex); |
| 2788 | } |
| 2789 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2790 | static int cgroup_cfts_commit(struct cgroup_subsys *ss, |
| 2791 | struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2792 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2793 | { |
| 2794 | LIST_HEAD(pending); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2795 | struct cgroup *cgrp, *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2796 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2797 | struct dentry *prev = NULL; |
| 2798 | struct inode *inode; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2799 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2800 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2801 | |
| 2802 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 2803 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2804 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2805 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2806 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2809 | /* |
| 2810 | * All cgroups which are created after we drop cgroup_mutex will |
| 2811 | * 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] | 2812 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2813 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2814 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2815 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2816 | mutex_unlock(&cgroup_mutex); |
| 2817 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2818 | /* @root always needs to be updated */ |
| 2819 | inode = root->dentry->d_inode; |
| 2820 | mutex_lock(&inode->i_mutex); |
| 2821 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2822 | ret = cgroup_addrm_files(root, ss, cfts, is_add); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2823 | mutex_unlock(&cgroup_mutex); |
| 2824 | mutex_unlock(&inode->i_mutex); |
| 2825 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2826 | if (ret) |
| 2827 | goto out_deact; |
| 2828 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2829 | /* add/rm files for all cgroups created before */ |
| 2830 | rcu_read_lock(); |
| 2831 | cgroup_for_each_descendant_pre(cgrp, root) { |
| 2832 | if (cgroup_is_dead(cgrp)) |
| 2833 | continue; |
| 2834 | |
| 2835 | inode = cgrp->dentry->d_inode; |
| 2836 | dget(cgrp->dentry); |
| 2837 | rcu_read_unlock(); |
| 2838 | |
| 2839 | dput(prev); |
| 2840 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2841 | |
| 2842 | mutex_lock(&inode->i_mutex); |
| 2843 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2844 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2845 | ret = cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2846 | mutex_unlock(&cgroup_mutex); |
| 2847 | mutex_unlock(&inode->i_mutex); |
| 2848 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2849 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2850 | if (ret) |
| 2851 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2852 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | rcu_read_unlock(); |
| 2854 | dput(prev); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2855 | out_deact: |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2856 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2857 | return ret; |
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; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2877 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2878 | |
| 2879 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2880 | if (!set) |
| 2881 | return -ENOMEM; |
| 2882 | |
| 2883 | cgroup_cfts_prepare(); |
| 2884 | set->cfts = cfts; |
| 2885 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2886 | ret = cgroup_cfts_commit(ss, cfts, true); |
| 2887 | if (ret) |
| 2888 | cgroup_rm_cftypes(ss, cfts); |
| 2889 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2890 | } |
| 2891 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2892 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2893 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2894 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2895 | * @ss: target cgroup subsystem |
| 2896 | * @cfts: zero-length name terminated array of cftypes |
| 2897 | * |
| 2898 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2899 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2900 | * won't have them either. This function can be called anytime whether @ss |
| 2901 | * is attached or not. |
| 2902 | * |
| 2903 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2904 | * registered with @ss. |
| 2905 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2906 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2907 | { |
| 2908 | struct cftype_set *set; |
| 2909 | |
| 2910 | cgroup_cfts_prepare(); |
| 2911 | |
| 2912 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2913 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2914 | list_del(&set->node); |
| 2915 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2916 | cgroup_cfts_commit(ss, cfts, false); |
| 2917 | return 0; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | cgroup_cfts_commit(ss, NULL, false); |
| 2922 | return -ENOENT; |
| 2923 | } |
| 2924 | |
| 2925 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2926 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2927 | * @cgrp: the cgroup in question |
| 2928 | * |
| 2929 | * Return the number of tasks in the cgroup. |
| 2930 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2931 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2932 | { |
| 2933 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2934 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2935 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2936 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2937 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2938 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2939 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2940 | return count; |
| 2941 | } |
| 2942 | |
| 2943 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2944 | * Advance a list_head iterator. The iterator should be positioned at |
| 2945 | * the start of a css_set |
| 2946 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2947 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2948 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2949 | struct list_head *l = it->cset_link; |
| 2950 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2951 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2952 | |
| 2953 | /* Advance to the next non-empty css_set */ |
| 2954 | do { |
| 2955 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2956 | if (l == &cgrp->cset_links) { |
| 2957 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2958 | return; |
| 2959 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2960 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2961 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2962 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2963 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2964 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2967 | /* |
| 2968 | * To reduce the fork() overhead for systems that are not actually |
| 2969 | * using their cgroups capability, we don't maintain the lists running |
| 2970 | * through each css_set to its tasks until we see the list actually |
| 2971 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2972 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2973 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2974 | { |
| 2975 | struct task_struct *p, *g; |
| 2976 | write_lock(&css_set_lock); |
| 2977 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2978 | /* |
| 2979 | * We need tasklist_lock because RCU is not safe against |
| 2980 | * while_each_thread(). Besides, a forking task that has passed |
| 2981 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2982 | * is not guaranteed to have its child immediately visible in the |
| 2983 | * tasklist if we walk through it with RCU. |
| 2984 | */ |
| 2985 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2986 | do_each_thread(g, p) { |
| 2987 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2988 | /* |
| 2989 | * We should check if the process is exiting, otherwise |
| 2990 | * it will race with cgroup_exit() in that the list |
| 2991 | * entry won't be deleted though the process has exited. |
| 2992 | */ |
| 2993 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2994 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2995 | task_unlock(p); |
| 2996 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2997 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2998 | write_unlock(&css_set_lock); |
| 2999 | } |
| 3000 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3001 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3002 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 3003 | * @pos: the current cgroup |
| 3004 | * |
| 3005 | * This function returns the next sibling of @pos and should be called |
| 3006 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 3007 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 3008 | * state. |
| 3009 | */ |
| 3010 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 3011 | { |
| 3012 | struct cgroup *next; |
| 3013 | |
| 3014 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3015 | |
| 3016 | /* |
| 3017 | * @pos could already have been removed. Once a cgroup is removed, |
| 3018 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3019 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3020 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3021 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3022 | * finished its grace period even if it's already removed, and thus |
| 3023 | * safe to dereference from this RCU critical section. If |
| 3024 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3025 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3026 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3027 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3028 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3029 | if (&next->sibling != &pos->parent->children) |
| 3030 | return next; |
| 3031 | return NULL; |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Can't dereference the next pointer. Each cgroup is given a |
| 3036 | * monotonically increasing unique serial number and always |
| 3037 | * appended to the sibling list, so the next one can be found by |
| 3038 | * walking the parent's children until we see a cgroup with higher |
| 3039 | * serial number than @pos's. |
| 3040 | * |
| 3041 | * While this path can be slow, it's taken only when either the |
| 3042 | * current cgroup is removed or iteration and removal race. |
| 3043 | */ |
| 3044 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3045 | if (next->serial_nr > pos->serial_nr) |
| 3046 | return next; |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3050 | |
| 3051 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3052 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3053 | * @pos: the current position (%NULL to initiate traversal) |
| 3054 | * @cgroup: cgroup whose descendants to walk |
| 3055 | * |
| 3056 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3057 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3058 | * |
| 3059 | * While this function requires RCU read locking, it doesn't require the |
| 3060 | * whole traversal to be contained in a single RCU critical section. This |
| 3061 | * function will return the correct next descendant as long as both @pos |
| 3062 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3063 | */ |
| 3064 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3065 | struct cgroup *cgroup) |
| 3066 | { |
| 3067 | struct cgroup *next; |
| 3068 | |
| 3069 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3070 | |
| 3071 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3072 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3073 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3074 | |
| 3075 | /* visit the first child if exists */ |
| 3076 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3077 | if (next) |
| 3078 | return next; |
| 3079 | |
| 3080 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3081 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3082 | next = cgroup_next_sibling(pos); |
| 3083 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3084 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3085 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3086 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3087 | |
| 3088 | return NULL; |
| 3089 | } |
| 3090 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3091 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3092 | /** |
| 3093 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3094 | * @pos: cgroup of interest |
| 3095 | * |
| 3096 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3097 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3098 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3099 | * |
| 3100 | * While this function requires RCU read locking, it doesn't require the |
| 3101 | * whole traversal to be contained in a single RCU critical section. This |
| 3102 | * function will return the correct rightmost descendant as long as @pos is |
| 3103 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3104 | */ |
| 3105 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3106 | { |
| 3107 | struct cgroup *last, *tmp; |
| 3108 | |
| 3109 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3110 | |
| 3111 | do { |
| 3112 | last = pos; |
| 3113 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3114 | pos = NULL; |
| 3115 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3116 | pos = tmp; |
| 3117 | } while (pos); |
| 3118 | |
| 3119 | return last; |
| 3120 | } |
| 3121 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3122 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3123 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3124 | { |
| 3125 | struct cgroup *last; |
| 3126 | |
| 3127 | do { |
| 3128 | last = pos; |
| 3129 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3130 | sibling); |
| 3131 | } while (pos); |
| 3132 | |
| 3133 | return last; |
| 3134 | } |
| 3135 | |
| 3136 | /** |
| 3137 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3138 | * @pos: the current position (%NULL to initiate traversal) |
| 3139 | * @cgroup: cgroup whose descendants to walk |
| 3140 | * |
| 3141 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3142 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3143 | * |
| 3144 | * While this function requires RCU read locking, it doesn't require the |
| 3145 | * whole traversal to be contained in a single RCU critical section. This |
| 3146 | * function will return the correct next descendant as long as both @pos |
| 3147 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3148 | */ |
| 3149 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3150 | struct cgroup *cgroup) |
| 3151 | { |
| 3152 | struct cgroup *next; |
| 3153 | |
| 3154 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3155 | |
| 3156 | /* if first iteration, visit the leftmost descendant */ |
| 3157 | if (!pos) { |
| 3158 | next = cgroup_leftmost_descendant(cgroup); |
| 3159 | return next != cgroup ? next : NULL; |
| 3160 | } |
| 3161 | |
| 3162 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3163 | next = cgroup_next_sibling(pos); |
| 3164 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3165 | return cgroup_leftmost_descendant(next); |
| 3166 | |
| 3167 | /* no sibling left, visit parent */ |
| 3168 | next = pos->parent; |
| 3169 | return next != cgroup ? next : NULL; |
| 3170 | } |
| 3171 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3172 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3173 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3174 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3175 | { |
| 3176 | /* |
| 3177 | * The first time anyone tries to iterate across a cgroup, |
| 3178 | * we need to enable the list linking each css_set to its |
| 3179 | * tasks, and fix up all existing tasks. |
| 3180 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3181 | if (!use_task_css_set_links) |
| 3182 | cgroup_enable_task_cg_lists(); |
| 3183 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3184 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3185 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3186 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3187 | } |
| 3188 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3189 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3190 | struct cgroup_iter *it) |
| 3191 | { |
| 3192 | struct task_struct *res; |
| 3193 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3194 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3195 | |
| 3196 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3197 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3198 | return NULL; |
| 3199 | res = list_entry(l, struct task_struct, cg_list); |
| 3200 | /* Advance iterator to find next entry */ |
| 3201 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3202 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3203 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3204 | /* We reached the end of this task list - move on to |
| 3205 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3206 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3207 | } else { |
| 3208 | it->task = l; |
| 3209 | } |
| 3210 | return res; |
| 3211 | } |
| 3212 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3213 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3214 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3215 | { |
| 3216 | read_unlock(&css_set_lock); |
| 3217 | } |
| 3218 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3219 | static inline int started_after_time(struct task_struct *t1, |
| 3220 | struct timespec *time, |
| 3221 | struct task_struct *t2) |
| 3222 | { |
| 3223 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3224 | if (start_diff > 0) { |
| 3225 | return 1; |
| 3226 | } else if (start_diff < 0) { |
| 3227 | return 0; |
| 3228 | } else { |
| 3229 | /* |
| 3230 | * Arbitrarily, if two processes started at the same |
| 3231 | * time, we'll say that the lower pointer value |
| 3232 | * started first. Note that t2 may have exited by now |
| 3233 | * so this may not be a valid pointer any longer, but |
| 3234 | * that's fine - it still serves to distinguish |
| 3235 | * between two tasks started (effectively) simultaneously. |
| 3236 | */ |
| 3237 | return t1 > t2; |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * This function is a callback from heap_insert() and is used to order |
| 3243 | * the heap. |
| 3244 | * In this case we order the heap in descending task start time. |
| 3245 | */ |
| 3246 | static inline int started_after(void *p1, void *p2) |
| 3247 | { |
| 3248 | struct task_struct *t1 = p1; |
| 3249 | struct task_struct *t2 = p2; |
| 3250 | return started_after_time(t1, &t2->start_time, t2); |
| 3251 | } |
| 3252 | |
| 3253 | /** |
| 3254 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3255 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3256 | * |
| 3257 | * Arguments include pointers to callback functions test_task() and |
| 3258 | * process_task(). |
| 3259 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3260 | * and if it returns true, call process_task() for it also. |
| 3261 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3262 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3263 | * but does not lock css_set_lock for the call to process_task(). |
| 3264 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3265 | * creation. |
| 3266 | * It is guaranteed that process_task() will act on every task that |
| 3267 | * is a member of the cgroup for the duration of this call. This |
| 3268 | * function may or may not call process_task() for tasks that exit |
| 3269 | * or move to a different cgroup during the call, or are forked or |
| 3270 | * move into the cgroup during the call. |
| 3271 | * |
| 3272 | * Note that test_task() may be called with locks held, and may in some |
| 3273 | * situations be called multiple times for the same task, so it should |
| 3274 | * be cheap. |
| 3275 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3276 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3277 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3278 | * may cause this function to fail). |
| 3279 | */ |
| 3280 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3281 | { |
| 3282 | int retval, i; |
| 3283 | struct cgroup_iter it; |
| 3284 | struct task_struct *p, *dropped; |
| 3285 | /* Never dereference latest_task, since it's not refcounted */ |
| 3286 | struct task_struct *latest_task = NULL; |
| 3287 | struct ptr_heap tmp_heap; |
| 3288 | struct ptr_heap *heap; |
| 3289 | struct timespec latest_time = { 0, 0 }; |
| 3290 | |
| 3291 | if (scan->heap) { |
| 3292 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3293 | heap = scan->heap; |
| 3294 | heap->gt = &started_after; |
| 3295 | } else { |
| 3296 | /* We need to allocate our own heap memory */ |
| 3297 | heap = &tmp_heap; |
| 3298 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3299 | if (retval) |
| 3300 | /* cannot allocate the heap */ |
| 3301 | return retval; |
| 3302 | } |
| 3303 | |
| 3304 | again: |
| 3305 | /* |
| 3306 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3307 | * to determine which are of interest, and using the scanner's |
| 3308 | * "process_task" callback to process any of them that need an update. |
| 3309 | * Since we don't want to hold any locks during the task updates, |
| 3310 | * gather tasks to be processed in a heap structure. |
| 3311 | * The heap is sorted by descending task start time. |
| 3312 | * If the statically-sized heap fills up, we overflow tasks that |
| 3313 | * started later, and in future iterations only consider tasks that |
| 3314 | * started after the latest task in the previous pass. This |
| 3315 | * guarantees forward progress and that we don't miss any tasks. |
| 3316 | */ |
| 3317 | heap->size = 0; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 3318 | cgroup_iter_start(scan->cgrp, &it); |
| 3319 | while ((p = cgroup_iter_next(scan->cgrp, &it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3320 | /* |
| 3321 | * Only affect tasks that qualify per the caller's callback, |
| 3322 | * if he provided one |
| 3323 | */ |
| 3324 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3325 | continue; |
| 3326 | /* |
| 3327 | * Only process tasks that started after the last task |
| 3328 | * we processed |
| 3329 | */ |
| 3330 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3331 | continue; |
| 3332 | dropped = heap_insert(heap, p); |
| 3333 | if (dropped == NULL) { |
| 3334 | /* |
| 3335 | * The new task was inserted; the heap wasn't |
| 3336 | * previously full |
| 3337 | */ |
| 3338 | get_task_struct(p); |
| 3339 | } else if (dropped != p) { |
| 3340 | /* |
| 3341 | * The new task was inserted, and pushed out a |
| 3342 | * different task |
| 3343 | */ |
| 3344 | get_task_struct(p); |
| 3345 | put_task_struct(dropped); |
| 3346 | } |
| 3347 | /* |
| 3348 | * Else the new task was newer than anything already in |
| 3349 | * the heap and wasn't inserted |
| 3350 | */ |
| 3351 | } |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 3352 | cgroup_iter_end(scan->cgrp, &it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3353 | |
| 3354 | if (heap->size) { |
| 3355 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3356 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3357 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3358 | latest_time = q->start_time; |
| 3359 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3360 | } |
| 3361 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3362 | scan->process_task(q, scan); |
| 3363 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3364 | } |
| 3365 | /* |
| 3366 | * If we had to process any tasks at all, scan again |
| 3367 | * in case some of them were in the middle of forking |
| 3368 | * children that didn't get processed. |
| 3369 | * Not the most efficient way to do it, but it avoids |
| 3370 | * having to take callback_mutex in the fork path |
| 3371 | */ |
| 3372 | goto again; |
| 3373 | } |
| 3374 | if (heap == &tmp_heap) |
| 3375 | heap_free(&tmp_heap); |
| 3376 | return 0; |
| 3377 | } |
| 3378 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3379 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3380 | struct cgroup_scanner *scan) |
| 3381 | { |
| 3382 | struct cgroup *new_cgroup = scan->data; |
| 3383 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3384 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3385 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3386 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | /** |
| 3390 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3391 | * @to: cgroup to which the tasks will be moved |
| 3392 | * @from: cgroup in which the tasks currently reside |
| 3393 | */ |
| 3394 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3395 | { |
| 3396 | struct cgroup_scanner scan; |
| 3397 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 3398 | scan.cgrp = from; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3399 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3400 | scan.process_task = cgroup_transfer_one_task; |
| 3401 | scan.heap = NULL; |
| 3402 | scan.data = to; |
| 3403 | |
| 3404 | return cgroup_scan_tasks(&scan); |
| 3405 | } |
| 3406 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3407 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3408 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3409 | * |
| 3410 | * Reading this file can return large amounts of data if a cgroup has |
| 3411 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3412 | * but we cannot guarantee that the information we produce is correct |
| 3413 | * unless we produce it entirely atomically. |
| 3414 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3415 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3416 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3417 | /* which pidlist file are we talking about? */ |
| 3418 | enum cgroup_filetype { |
| 3419 | CGROUP_FILE_PROCS, |
| 3420 | CGROUP_FILE_TASKS, |
| 3421 | }; |
| 3422 | |
| 3423 | /* |
| 3424 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3425 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3426 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3427 | * to the cgroup. |
| 3428 | */ |
| 3429 | struct cgroup_pidlist { |
| 3430 | /* |
| 3431 | * used to find which pidlist is wanted. doesn't change as long as |
| 3432 | * this particular list stays in the list. |
| 3433 | */ |
| 3434 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3435 | /* array of xids */ |
| 3436 | pid_t *list; |
| 3437 | /* how many elements the above list has */ |
| 3438 | int length; |
| 3439 | /* how many files are using the current array */ |
| 3440 | int use_count; |
| 3441 | /* each of these stored in a list by its cgroup */ |
| 3442 | struct list_head links; |
| 3443 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3444 | struct cgroup *owner; |
| 3445 | /* protects the other fields */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3446 | struct rw_semaphore rwsem; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3447 | }; |
| 3448 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3449 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3450 | * The following two functions "fix" the issue where there are more pids |
| 3451 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3452 | * TODO: replace with a kernel-wide solution to this problem |
| 3453 | */ |
| 3454 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3455 | static void *pidlist_allocate(int count) |
| 3456 | { |
| 3457 | if (PIDLIST_TOO_LARGE(count)) |
| 3458 | return vmalloc(count * sizeof(pid_t)); |
| 3459 | else |
| 3460 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3461 | } |
| 3462 | static void pidlist_free(void *p) |
| 3463 | { |
| 3464 | if (is_vmalloc_addr(p)) |
| 3465 | vfree(p); |
| 3466 | else |
| 3467 | kfree(p); |
| 3468 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3469 | |
| 3470 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3471 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3472 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3473 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3474 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3475 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3476 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3477 | |
| 3478 | /* |
| 3479 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3480 | * edge cases first; no work needs to be done for either |
| 3481 | */ |
| 3482 | if (length == 0 || length == 1) |
| 3483 | return length; |
| 3484 | /* src and dest walk down the list; dest counts unique elements */ |
| 3485 | for (src = 1; src < length; src++) { |
| 3486 | /* find next unique element */ |
| 3487 | while (list[src] == list[src-1]) { |
| 3488 | src++; |
| 3489 | if (src == length) |
| 3490 | goto after; |
| 3491 | } |
| 3492 | /* dest always points to where the next unique element goes */ |
| 3493 | list[dest] = list[src]; |
| 3494 | dest++; |
| 3495 | } |
| 3496 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3497 | return dest; |
| 3498 | } |
| 3499 | |
| 3500 | static int cmppid(const void *a, const void *b) |
| 3501 | { |
| 3502 | return *(pid_t *)a - *(pid_t *)b; |
| 3503 | } |
| 3504 | |
| 3505 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3506 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3507 | * returns with the lock on that pidlist already held, and takes care |
| 3508 | * of the use count, or returns NULL with no locks held if we're out of |
| 3509 | * memory. |
| 3510 | */ |
| 3511 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3512 | enum cgroup_filetype type) |
| 3513 | { |
| 3514 | struct cgroup_pidlist *l; |
| 3515 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3516 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3517 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3518 | /* |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3519 | * We can't drop the pidlist_mutex before taking the l->rwsem in case |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3520 | * the last ref-holder is trying to remove l from the list at the same |
| 3521 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3522 | * list we find out from under us - compare release_pid_array(). |
| 3523 | */ |
| 3524 | mutex_lock(&cgrp->pidlist_mutex); |
| 3525 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3526 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3527 | /* make sure l doesn't vanish out from under us */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3528 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3529 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3530 | return l; |
| 3531 | } |
| 3532 | } |
| 3533 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3534 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3535 | if (!l) { |
| 3536 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3537 | return l; |
| 3538 | } |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3539 | init_rwsem(&l->rwsem); |
| 3540 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3541 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3542 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3543 | l->owner = cgrp; |
| 3544 | list_add(&l->links, &cgrp->pidlists); |
| 3545 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3546 | return l; |
| 3547 | } |
| 3548 | |
| 3549 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3550 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3551 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3552 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3553 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3554 | { |
| 3555 | pid_t *array; |
| 3556 | int length; |
| 3557 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3558 | struct cgroup_iter it; |
| 3559 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3560 | struct cgroup_pidlist *l; |
| 3561 | |
| 3562 | /* |
| 3563 | * If cgroup gets more users after we read count, we won't have |
| 3564 | * enough space - tough. This race is indistinguishable to the |
| 3565 | * caller from the case that the additional cgroup users didn't |
| 3566 | * show up until sometime later on. |
| 3567 | */ |
| 3568 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3569 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | if (!array) |
| 3571 | return -ENOMEM; |
| 3572 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3573 | cgroup_iter_start(cgrp, &it); |
| 3574 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3575 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3576 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3577 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3578 | if (type == CGROUP_FILE_PROCS) |
| 3579 | pid = task_tgid_vnr(tsk); |
| 3580 | else |
| 3581 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3582 | if (pid > 0) /* make sure to only use valid results */ |
| 3583 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3584 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3585 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3586 | length = n; |
| 3587 | /* now sort & (if procs) strip out duplicates */ |
| 3588 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3589 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3590 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3591 | l = cgroup_pidlist_find(cgrp, type); |
| 3592 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3593 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3594 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3595 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3596 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3597 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3598 | l->list = array; |
| 3599 | l->length = length; |
| 3600 | l->use_count++; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3601 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3602 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3603 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3604 | } |
| 3605 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3606 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3607 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3608 | * @stats: cgroupstats to fill information into |
| 3609 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3610 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3611 | * |
| 3612 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3613 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3614 | */ |
| 3615 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3616 | { |
| 3617 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3618 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3619 | struct cgroup_iter it; |
| 3620 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3621 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3622 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3623 | * Validate dentry by checking the superblock operations, |
| 3624 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3625 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3626 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3627 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3628 | goto err; |
| 3629 | |
| 3630 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3631 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3632 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3633 | cgroup_iter_start(cgrp, &it); |
| 3634 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3635 | switch (tsk->state) { |
| 3636 | case TASK_RUNNING: |
| 3637 | stats->nr_running++; |
| 3638 | break; |
| 3639 | case TASK_INTERRUPTIBLE: |
| 3640 | stats->nr_sleeping++; |
| 3641 | break; |
| 3642 | case TASK_UNINTERRUPTIBLE: |
| 3643 | stats->nr_uninterruptible++; |
| 3644 | break; |
| 3645 | case TASK_STOPPED: |
| 3646 | stats->nr_stopped++; |
| 3647 | break; |
| 3648 | default: |
| 3649 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3650 | stats->nr_io_wait++; |
| 3651 | break; |
| 3652 | } |
| 3653 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3654 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3655 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3656 | err: |
| 3657 | return ret; |
| 3658 | } |
| 3659 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3660 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3661 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | * 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] | 3663 | * 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] | 3664 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3665 | */ |
| 3666 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3667 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3668 | { |
| 3669 | /* |
| 3670 | * Initially we receive a position value that corresponds to |
| 3671 | * one more than the last pid shown (or 0 on the first call or |
| 3672 | * after a seek to the start). Use a binary-search to find the |
| 3673 | * next pid to display, if any |
| 3674 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | int index = 0, pid = *pos; |
| 3677 | int *iter; |
| 3678 | |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3679 | down_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3680 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3681 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3682 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3683 | while (index < end) { |
| 3684 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3685 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3686 | index = mid; |
| 3687 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3688 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3689 | index = mid + 1; |
| 3690 | else |
| 3691 | end = mid; |
| 3692 | } |
| 3693 | } |
| 3694 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3695 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3696 | return NULL; |
| 3697 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3698 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3699 | *pos = *iter; |
| 3700 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3703 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3704 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3705 | struct cgroup_pidlist *l = s->private; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3706 | up_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3707 | } |
| 3708 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3709 | 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] | 3710 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3711 | struct cgroup_pidlist *l = s->private; |
| 3712 | pid_t *p = v; |
| 3713 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3714 | /* |
| 3715 | * Advance to the next pid in the array. If this goes off the |
| 3716 | * end, we're done |
| 3717 | */ |
| 3718 | p++; |
| 3719 | if (p >= end) { |
| 3720 | return NULL; |
| 3721 | } else { |
| 3722 | *pos = *p; |
| 3723 | return p; |
| 3724 | } |
| 3725 | } |
| 3726 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3727 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3728 | { |
| 3729 | return seq_printf(s, "%d\n", *(int *)v); |
| 3730 | } |
| 3731 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3732 | /* |
| 3733 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3734 | * independent of whether it's tasks or procs |
| 3735 | */ |
| 3736 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3737 | .start = cgroup_pidlist_start, |
| 3738 | .stop = cgroup_pidlist_stop, |
| 3739 | .next = cgroup_pidlist_next, |
| 3740 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3741 | }; |
| 3742 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3743 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3744 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3745 | /* |
| 3746 | * the case where we're the last user of this particular pidlist will |
| 3747 | * have us remove it from the cgroup's list, which entails taking the |
| 3748 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3749 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3750 | */ |
| 3751 | mutex_lock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3752 | down_write(&l->rwsem); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3753 | BUG_ON(!l->use_count); |
| 3754 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3755 | /* we're the last user if refcount is 0; remove and free */ |
| 3756 | list_del(&l->links); |
| 3757 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3758 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3759 | put_pid_ns(l->key.ns); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3760 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3761 | kfree(l); |
| 3762 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3763 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3764 | mutex_unlock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3765 | up_write(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3766 | } |
| 3767 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3768 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3769 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3770 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3771 | if (!(file->f_mode & FMODE_READ)) |
| 3772 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3773 | /* |
| 3774 | * the seq_file will only be initialized if the file was opened for |
| 3775 | * reading; hence we check if it's not null only in that case. |
| 3776 | */ |
| 3777 | l = ((struct seq_file *)file->private_data)->private; |
| 3778 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | return seq_release(inode, file); |
| 3780 | } |
| 3781 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3782 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3783 | .read = seq_read, |
| 3784 | .llseek = seq_lseek, |
| 3785 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3786 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3787 | }; |
| 3788 | |
| 3789 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3790 | * The following functions handle opens on a file that displays a pidlist |
| 3791 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3792 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3793 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3795 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3796 | { |
| 3797 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3798 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3799 | int retval; |
| 3800 | |
| 3801 | /* Nothing to do for write-only files */ |
| 3802 | if (!(file->f_mode & FMODE_READ)) |
| 3803 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3804 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3805 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3806 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3807 | if (retval) |
| 3808 | return retval; |
| 3809 | /* configure file information */ |
| 3810 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3811 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3812 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3813 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3814 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3815 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3816 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3817 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3818 | return 0; |
| 3819 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3820 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3821 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3822 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3823 | } |
| 3824 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3825 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3826 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3827 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3828 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3829 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3830 | struct cftype *cft) |
| 3831 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3832 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3833 | } |
| 3834 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3835 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3836 | struct cftype *cft, |
| 3837 | u64 val) |
| 3838 | { |
| 3839 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3840 | if (val) |
| 3841 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3842 | else |
| 3843 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3844 | return 0; |
| 3845 | } |
| 3846 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3847 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3848 | * When dput() is called asynchronously, if umount has been done and |
| 3849 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3850 | * there's a small window that vfs will see the root dentry with non-zero |
| 3851 | * refcnt and trigger BUG(). |
| 3852 | * |
| 3853 | * That's why we hold a reference before dput() and drop it right after. |
| 3854 | */ |
| 3855 | static void cgroup_dput(struct cgroup *cgrp) |
| 3856 | { |
| 3857 | struct super_block *sb = cgrp->root->sb; |
| 3858 | |
| 3859 | atomic_inc(&sb->s_active); |
| 3860 | dput(cgrp->dentry); |
| 3861 | deactivate_super(sb); |
| 3862 | } |
| 3863 | |
| 3864 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3865 | * Unregister event and free resources. |
| 3866 | * |
| 3867 | * Gets called from workqueue. |
| 3868 | */ |
| 3869 | static void cgroup_event_remove(struct work_struct *work) |
| 3870 | { |
| 3871 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3872 | remove); |
| 3873 | struct cgroup *cgrp = event->cgrp; |
| 3874 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3875 | remove_wait_queue(event->wqh, &event->wait); |
| 3876 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3877 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3878 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3879 | /* Notify userspace the event is going away. */ |
| 3880 | eventfd_signal(event->eventfd, 1); |
| 3881 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3882 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3883 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3884 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | /* |
| 3888 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3889 | * |
| 3890 | * Called with wqh->lock held and interrupts disabled. |
| 3891 | */ |
| 3892 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3893 | int sync, void *key) |
| 3894 | { |
| 3895 | struct cgroup_event *event = container_of(wait, |
| 3896 | struct cgroup_event, wait); |
| 3897 | struct cgroup *cgrp = event->cgrp; |
| 3898 | unsigned long flags = (unsigned long)key; |
| 3899 | |
| 3900 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3901 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3902 | * If the event has been detached at cgroup removal, we |
| 3903 | * can simply return knowing the other side will cleanup |
| 3904 | * for us. |
| 3905 | * |
| 3906 | * We can't race against event freeing since the other |
| 3907 | * side will require wqh->lock via remove_wait_queue(), |
| 3908 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3909 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3910 | spin_lock(&cgrp->event_list_lock); |
| 3911 | if (!list_empty(&event->list)) { |
| 3912 | list_del_init(&event->list); |
| 3913 | /* |
| 3914 | * We are in atomic context, but cgroup_event_remove() |
| 3915 | * may sleep, so we have to call it in workqueue. |
| 3916 | */ |
| 3917 | schedule_work(&event->remove); |
| 3918 | } |
| 3919 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | return 0; |
| 3923 | } |
| 3924 | |
| 3925 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3926 | wait_queue_head_t *wqh, poll_table *pt) |
| 3927 | { |
| 3928 | struct cgroup_event *event = container_of(pt, |
| 3929 | struct cgroup_event, pt); |
| 3930 | |
| 3931 | event->wqh = wqh; |
| 3932 | add_wait_queue(wqh, &event->wait); |
| 3933 | } |
| 3934 | |
| 3935 | /* |
| 3936 | * Parse input and register new cgroup event handler. |
| 3937 | * |
| 3938 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3939 | * Interpretation of args is defined by control file implementation. |
| 3940 | */ |
| 3941 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3942 | const char *buffer) |
| 3943 | { |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3944 | struct cgroup_event *event; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3945 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3946 | unsigned int efd, cfd; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3947 | struct file *efile; |
| 3948 | struct file *cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3949 | char *endp; |
| 3950 | int ret; |
| 3951 | |
| 3952 | efd = simple_strtoul(buffer, &endp, 10); |
| 3953 | if (*endp != ' ') |
| 3954 | return -EINVAL; |
| 3955 | buffer = endp + 1; |
| 3956 | |
| 3957 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3958 | if ((*endp != ' ') && (*endp != '\0')) |
| 3959 | return -EINVAL; |
| 3960 | buffer = endp + 1; |
| 3961 | |
| 3962 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3963 | if (!event) |
| 3964 | return -ENOMEM; |
| 3965 | event->cgrp = cgrp; |
| 3966 | INIT_LIST_HEAD(&event->list); |
| 3967 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3968 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3969 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3970 | |
| 3971 | efile = eventfd_fget(efd); |
| 3972 | if (IS_ERR(efile)) { |
| 3973 | ret = PTR_ERR(efile); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3974 | goto out_kfree; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3978 | if (IS_ERR(event->eventfd)) { |
| 3979 | ret = PTR_ERR(event->eventfd); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3980 | goto out_put_efile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | cfile = fget(cfd); |
| 3984 | if (!cfile) { |
| 3985 | ret = -EBADF; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3986 | goto out_put_eventfd; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3990 | /* 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] | 3991 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3992 | if (ret < 0) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3993 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3994 | |
| 3995 | event->cft = __file_cft(cfile); |
| 3996 | if (IS_ERR(event->cft)) { |
| 3997 | ret = PTR_ERR(event->cft); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 3998 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3999 | } |
| 4000 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4001 | /* |
| 4002 | * The file to be monitored must be in the same cgroup as |
| 4003 | * cgroup.event_control is. |
| 4004 | */ |
| 4005 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4006 | if (cgrp_cfile != cgrp) { |
| 4007 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4008 | goto out_put_cfile; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4009 | } |
| 4010 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4011 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4012 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4013 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | ret = event->cft->register_event(cgrp, event->cft, |
| 4017 | event->eventfd, buffer); |
| 4018 | if (ret) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4019 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4020 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4021 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4022 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4023 | /* |
| 4024 | * Events should be removed after rmdir of cgroup directory, but before |
| 4025 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4026 | * directory dentry to do that. |
| 4027 | */ |
| 4028 | dget(cgrp->dentry); |
| 4029 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4030 | spin_lock(&cgrp->event_list_lock); |
| 4031 | list_add(&event->list, &cgrp->event_list); |
| 4032 | spin_unlock(&cgrp->event_list_lock); |
| 4033 | |
| 4034 | fput(cfile); |
| 4035 | fput(efile); |
| 4036 | |
| 4037 | return 0; |
| 4038 | |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4039 | out_put_cfile: |
| 4040 | fput(cfile); |
| 4041 | out_put_eventfd: |
| 4042 | eventfd_ctx_put(event->eventfd); |
| 4043 | out_put_efile: |
| 4044 | fput(efile); |
| 4045 | out_kfree: |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4046 | kfree(event); |
| 4047 | |
| 4048 | return ret; |
| 4049 | } |
| 4050 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4051 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4052 | struct cftype *cft) |
| 4053 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4054 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4058 | struct cftype *cft, |
| 4059 | u64 val) |
| 4060 | { |
| 4061 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4062 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4063 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4064 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4068 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4069 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4070 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4071 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4072 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4073 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4074 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4075 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4076 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4077 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4078 | .write_string = cgroup_write_event_control, |
| 4079 | .mode = S_IWUGO, |
| 4080 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4081 | { |
| 4082 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4083 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4084 | .read_u64 = cgroup_clone_children_read, |
| 4085 | .write_u64 = cgroup_clone_children_write, |
| 4086 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4087 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4088 | .name = "cgroup.sane_behavior", |
| 4089 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4090 | .read_seq_string = cgroup_sane_behavior_show, |
| 4091 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4092 | |
| 4093 | /* |
| 4094 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4095 | * don't exist if sane_behavior. If you're depending on these, be |
| 4096 | * prepared to be burned. |
| 4097 | */ |
| 4098 | { |
| 4099 | .name = "tasks", |
| 4100 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4101 | .open = cgroup_tasks_open, |
| 4102 | .write_u64 = cgroup_tasks_write, |
| 4103 | .release = cgroup_pidlist_release, |
| 4104 | .mode = S_IRUGO | S_IWUSR, |
| 4105 | }, |
| 4106 | { |
| 4107 | .name = "notify_on_release", |
| 4108 | .flags = CFTYPE_INSANE, |
| 4109 | .read_u64 = cgroup_read_notify_on_release, |
| 4110 | .write_u64 = cgroup_write_notify_on_release, |
| 4111 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4112 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4113 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4114 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4115 | .read_seq_string = cgroup_release_agent_show, |
| 4116 | .write_string = cgroup_release_agent_write, |
| 4117 | .max_write_len = PATH_MAX, |
| 4118 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4119 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4120 | }; |
| 4121 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4122 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4123 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4124 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4125 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4126 | * |
| 4127 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4128 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4129 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4130 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4131 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4132 | int i, ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4133 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4134 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4135 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4136 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4137 | |
| 4138 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4139 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4140 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4141 | list_for_each_entry(set, &ss->cftsets, node) { |
| 4142 | ret = cgroup_addrm_files(cgrp, ss, set->cfts, true); |
| 4143 | if (ret < 0) |
| 4144 | goto err; |
| 4145 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4146 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4147 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4148 | /* This cgroup is ready now */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4149 | for_each_root_subsys(cgrp->root, ss) { |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4150 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4151 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 4152 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4153 | /* |
| 4154 | * Update id->css pointer and make this css visible from |
| 4155 | * CSS ID functions. This pointer will be dereferened |
| 4156 | * from RCU-read-side without locks. |
| 4157 | */ |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4158 | if (id) |
| 4159 | rcu_assign_pointer(id->css, css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4160 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4161 | |
| 4162 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4163 | err: |
| 4164 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4165 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4168 | static void css_dput_fn(struct work_struct *work) |
| 4169 | { |
| 4170 | struct cgroup_subsys_state *css = |
| 4171 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4172 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4173 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4174 | } |
| 4175 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4176 | static void css_release(struct percpu_ref *ref) |
| 4177 | { |
| 4178 | struct cgroup_subsys_state *css = |
| 4179 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4180 | |
| 4181 | schedule_work(&css->dput_work); |
| 4182 | } |
| 4183 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4184 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4185 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4186 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4187 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4188 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4189 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4190 | css->id = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4191 | if (cgrp == cgroup_dummy_top) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4192 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4193 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4194 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4195 | |
| 4196 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4197 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4198 | * css_put(). dput() requires process context, which css_put() may |
| 4199 | * be called without. @css->dput_work will be used to invoke |
| 4200 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4201 | */ |
| 4202 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4205 | /* invoke ->css_online() on a new CSS and mark it online if successful */ |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4206 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4207 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4208 | int ret = 0; |
| 4209 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4210 | lockdep_assert_held(&cgroup_mutex); |
| 4211 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4212 | if (ss->css_online) |
| 4213 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4214 | if (!ret) |
| 4215 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4216 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4217 | } |
| 4218 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4219 | /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4220 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4221 | { |
| 4222 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4223 | |
| 4224 | lockdep_assert_held(&cgroup_mutex); |
| 4225 | |
| 4226 | if (!(css->flags & CSS_ONLINE)) |
| 4227 | return; |
| 4228 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4229 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4230 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4231 | |
| 4232 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4233 | } |
| 4234 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4235 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4236 | * cgroup_create - create a cgroup |
| 4237 | * @parent: cgroup that will be parent of the new cgroup |
| 4238 | * @dentry: dentry of the new cgroup |
| 4239 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4240 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4241 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4242 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4243 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4244 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4245 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4246 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4247 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4248 | struct cgroupfs_root *root = parent->root; |
| 4249 | int err = 0; |
| 4250 | struct cgroup_subsys *ss; |
| 4251 | struct super_block *sb = root->sb; |
| 4252 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4253 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4254 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4255 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4256 | return -ENOMEM; |
| 4257 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4258 | name = cgroup_alloc_name(dentry); |
| 4259 | if (!name) |
| 4260 | goto err_free_cgrp; |
| 4261 | rcu_assign_pointer(cgrp->name, name); |
| 4262 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4263 | /* |
| 4264 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4265 | * a half-baked cgroup. |
| 4266 | */ |
| 4267 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4268 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4269 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4270 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4271 | /* |
| 4272 | * Only live parents can have children. Note that the liveliness |
| 4273 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4274 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4275 | * anyway so that locking is contained inside cgroup proper and we |
| 4276 | * don't get nasty surprises if we ever grow another caller. |
| 4277 | */ |
| 4278 | if (!cgroup_lock_live_group(parent)) { |
| 4279 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4280 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4283 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4284 | * get deleted on unmount if there are child cgroups. This |
| 4285 | * can be done outside cgroup_mutex, since the sb can't |
| 4286 | * disappear while someone has an open control file on the |
| 4287 | * fs */ |
| 4288 | atomic_inc(&sb->s_active); |
| 4289 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4290 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4291 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4292 | dentry->d_fsdata = cgrp; |
| 4293 | cgrp->dentry = dentry; |
| 4294 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4295 | cgrp->parent = parent; |
| 4296 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4297 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4298 | if (notify_on_release(parent)) |
| 4299 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4300 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4301 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4302 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4303 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4304 | for_each_root_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4305 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4306 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4307 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4308 | if (IS_ERR(css)) { |
| 4309 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4310 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4311 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4312 | |
| 4313 | err = percpu_ref_init(&css->refcnt, css_release); |
Li Zefan | da0a12c | 2013-07-31 16:16:28 +0800 | [diff] [blame] | 4314 | if (err) { |
| 4315 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4316 | goto err_free_all; |
Li Zefan | da0a12c | 2013-07-31 16:16:28 +0800 | [diff] [blame] | 4317 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4318 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4319 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4320 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4321 | if (ss->use_id) { |
| 4322 | err = alloc_css_id(ss, parent, cgrp); |
| 4323 | if (err) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4324 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4325 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4326 | } |
| 4327 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4328 | /* |
| 4329 | * Create directory. cgroup_create_file() returns with the new |
| 4330 | * directory locked on success so that it can be populated without |
| 4331 | * dropping cgroup_mutex. |
| 4332 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4333 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | if (err < 0) |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4335 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4336 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4337 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4338 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4339 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4340 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4341 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4342 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4343 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4344 | /* each css holds a ref to the cgroup's dentry */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4345 | for_each_root_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4346 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4347 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4348 | /* hold a ref to the parent's dentry */ |
| 4349 | dget(parent->dentry); |
| 4350 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4351 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4352 | for_each_root_subsys(root, ss) { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4353 | err = online_css(ss, cgrp); |
| 4354 | if (err) |
| 4355 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4356 | |
| 4357 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4358 | parent->parent) { |
| 4359 | 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", |
| 4360 | current->comm, current->pid, ss->name); |
| 4361 | if (!strcmp(ss->name, "memory")) |
| 4362 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4363 | ss->warned_broken_hierarchy = true; |
| 4364 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4367 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4368 | |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4369 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
| 4370 | if (err) |
| 4371 | goto err_destroy; |
| 4372 | |
| 4373 | err = cgroup_populate_dir(cgrp, root->subsys_mask); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4374 | if (err) |
| 4375 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4376 | |
| 4377 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4378 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4379 | |
| 4380 | return 0; |
| 4381 | |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4382 | err_free_all: |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4383 | for_each_root_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4384 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4385 | |
| 4386 | if (css) { |
| 4387 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4388 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4389 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4390 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4391 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4392 | /* Release the reference count that we took on the superblock */ |
| 4393 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4394 | err_free_id: |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4395 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4396 | err_free_name: |
| 4397 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4398 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4399 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4400 | return err; |
Tejun Heo | 4b8b47e | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4401 | |
| 4402 | err_destroy: |
| 4403 | cgroup_destroy_locked(cgrp); |
| 4404 | mutex_unlock(&cgroup_mutex); |
| 4405 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4406 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4407 | } |
| 4408 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4409 | 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] | 4410 | { |
| 4411 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4412 | |
| 4413 | /* the vfs holds inode->i_mutex already */ |
| 4414 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4415 | } |
| 4416 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4417 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4418 | { |
| 4419 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4420 | return; |
| 4421 | |
| 4422 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4423 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4424 | schedule_work(&cgrp->destroy_work); |
| 4425 | } |
| 4426 | |
| 4427 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4428 | { |
| 4429 | struct cgroup_subsys_state *css = |
| 4430 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4431 | |
| 4432 | cgroup_css_killed(css->cgroup); |
| 4433 | } |
| 4434 | |
| 4435 | /** |
| 4436 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4437 | * @cgrp: cgroup to be destroyed |
| 4438 | * |
| 4439 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4440 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4441 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4442 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4443 | * the following two steps. |
| 4444 | * |
| 4445 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4446 | * userland visible parts and start killing the percpu refcnts of |
| 4447 | * css's. Set up so that the next stage will be kicked off once all |
| 4448 | * the percpu refcnts are confirmed to be killed. |
| 4449 | * |
| 4450 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4451 | * rest of destruction. Once all cgroup references are gone, the |
| 4452 | * cgroup is RCU-freed. |
| 4453 | * |
| 4454 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4455 | * the userland is concerned and a new cgroup with the same name may be |
| 4456 | * created. As cgroup doesn't care about the names internally, this |
| 4457 | * doesn't cause any problem. |
| 4458 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4459 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4460 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4461 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4462 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4463 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4464 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4465 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4466 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4467 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4468 | lockdep_assert_held(&cgroup_mutex); |
| 4469 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4470 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4471 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4472 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4473 | */ |
| 4474 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4475 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4476 | read_unlock(&css_set_lock); |
| 4477 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4478 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4479 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4480 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4481 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4482 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4483 | * css reference will be given out via css_tryget(). We can't |
| 4484 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4485 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4486 | * as killed on all CPUs on return. |
| 4487 | * |
| 4488 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4489 | * css is confirmed to be seen as killed on all CPUs. The |
| 4490 | * notification callback keeps track of the number of css's to be |
| 4491 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4492 | * destruction once the percpu refs of all css's are confirmed to |
| 4493 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4494 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4495 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4496 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4497 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4498 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4499 | /* |
| 4500 | * Killing would put the base ref, but we need to keep it |
| 4501 | * alive until after ->css_offline. |
| 4502 | */ |
| 4503 | percpu_ref_get(&css->refcnt); |
| 4504 | |
| 4505 | atomic_inc(&cgrp->css_kill_cnt); |
| 4506 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4507 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4508 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4509 | |
| 4510 | /* |
| 4511 | * Mark @cgrp dead. This prevents further task migration and child |
| 4512 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4513 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4514 | * resume iteration after dropping RCU read lock. See |
| 4515 | * cgroup_next_sibling() for details. |
| 4516 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4517 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4518 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4519 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4520 | raw_spin_lock(&release_list_lock); |
| 4521 | if (!list_empty(&cgrp->release_list)) |
| 4522 | list_del_init(&cgrp->release_list); |
| 4523 | raw_spin_unlock(&release_list_lock); |
| 4524 | |
| 4525 | /* |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4526 | * Clear and remove @cgrp directory. The removal puts the base ref |
| 4527 | * but we aren't quite done with @cgrp yet, so hold onto it. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4528 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4529 | cgroup_clear_dir(cgrp, cgrp->root->subsys_mask); |
| 4530 | cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4531 | dget(d); |
| 4532 | cgroup_d_remove_dir(d); |
| 4533 | |
| 4534 | /* |
| 4535 | * Unregister events and notify userspace. |
| 4536 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4537 | * directory to avoid race between userspace and kernelspace. |
| 4538 | */ |
| 4539 | spin_lock(&cgrp->event_list_lock); |
| 4540 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4541 | list_del_init(&event->list); |
| 4542 | schedule_work(&event->remove); |
| 4543 | } |
| 4544 | spin_unlock(&cgrp->event_list_lock); |
| 4545 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | }; |
| 4548 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4549 | /** |
| 4550 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4551 | * @work: cgroup->destroy_free_work |
| 4552 | * |
| 4553 | * This function is invoked from a work item for a cgroup which is being |
| 4554 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4555 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4556 | * is the second step of destruction described in the comment above |
| 4557 | * cgroup_destroy_locked(). |
| 4558 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4559 | static void cgroup_offline_fn(struct work_struct *work) |
| 4560 | { |
| 4561 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4562 | struct cgroup *parent = cgrp->parent; |
| 4563 | struct dentry *d = cgrp->dentry; |
| 4564 | struct cgroup_subsys *ss; |
| 4565 | |
| 4566 | mutex_lock(&cgroup_mutex); |
| 4567 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4568 | /* |
| 4569 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4570 | * initate destruction. |
| 4571 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4572 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4573 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4574 | |
| 4575 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4576 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4577 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4578 | * proceeds regardless of css refs. On the last put of each css, |
| 4579 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4580 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4581 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4582 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4583 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4584 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4585 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4586 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4587 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4588 | /* |
| 4589 | * We should remove the cgroup object from idr before its grace |
| 4590 | * period starts, so we won't be looking up a cgroup while the |
| 4591 | * cgroup is being freed. |
| 4592 | */ |
| 4593 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4594 | cgrp->id = -1; |
| 4595 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4596 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4597 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4598 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4599 | check_for_release(parent); |
| 4600 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4601 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4604 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4605 | { |
| 4606 | int ret; |
| 4607 | |
| 4608 | mutex_lock(&cgroup_mutex); |
| 4609 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4610 | mutex_unlock(&cgroup_mutex); |
| 4611 | |
| 4612 | return ret; |
| 4613 | } |
| 4614 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4615 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4616 | { |
| 4617 | INIT_LIST_HEAD(&ss->cftsets); |
| 4618 | |
| 4619 | /* |
| 4620 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4621 | * deregistration. |
| 4622 | */ |
| 4623 | if (ss->base_cftypes) { |
| 4624 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4625 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4626 | } |
| 4627 | } |
| 4628 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4629 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4630 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4631 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4632 | |
| 4633 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4634 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4635 | mutex_lock(&cgroup_mutex); |
| 4636 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4637 | /* init base cftset */ |
| 4638 | cgroup_init_cftsets(ss); |
| 4639 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4640 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4641 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4642 | ss->root = &cgroup_dummy_root; |
| 4643 | css = ss->css_alloc(cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4644 | /* We don't handle early failures gracefully */ |
| 4645 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4646 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4647 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4648 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4649 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4650 | * newly registered, all tasks and hence the |
| 4651 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4652 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4653 | |
| 4654 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4655 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4656 | /* At system boot, before all subsystems have been |
| 4657 | * registered, no tasks have been forked, so we don't |
| 4658 | * need to invoke fork callbacks here. */ |
| 4659 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4660 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4661 | BUG_ON(online_css(ss, cgroup_dummy_top)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4662 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4663 | mutex_unlock(&cgroup_mutex); |
| 4664 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4665 | /* this function shouldn't be used with modular subsystems, since they |
| 4666 | * need to register a subsys_id, among other things */ |
| 4667 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4671 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4672 | * @ss: the subsystem to load |
| 4673 | * |
| 4674 | * 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] | 4675 | * 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] | 4676 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4677 | * simpler cgroup_init_subsys. |
| 4678 | */ |
| 4679 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4680 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4681 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4682 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4683 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4684 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4685 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4686 | |
| 4687 | /* check name and function validity */ |
| 4688 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4689 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4690 | return -EINVAL; |
| 4691 | |
| 4692 | /* |
| 4693 | * we don't support callbacks in modular subsystems. this check is |
| 4694 | * before the ss->module check for consistency; a subsystem that could |
| 4695 | * be a module should still have no callbacks even if the user isn't |
| 4696 | * compiling it as one. |
| 4697 | */ |
| 4698 | if (ss->fork || ss->exit) |
| 4699 | return -EINVAL; |
| 4700 | |
| 4701 | /* |
| 4702 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4703 | * since cgroup_init_subsys will have already taken care of it. |
| 4704 | */ |
| 4705 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4706 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4707 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4708 | return 0; |
| 4709 | } |
| 4710 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4711 | /* init base cftset */ |
| 4712 | cgroup_init_cftsets(ss); |
| 4713 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4714 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4715 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4716 | |
| 4717 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4718 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4719 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4720 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4721 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4722 | css = ss->css_alloc(cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4723 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4724 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4725 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4726 | mutex_unlock(&cgroup_mutex); |
| 4727 | return PTR_ERR(css); |
| 4728 | } |
| 4729 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4730 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4731 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4732 | |
| 4733 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4734 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4735 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4736 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4737 | ret = cgroup_init_idr(ss, css); |
| 4738 | if (ret) |
| 4739 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | /* |
| 4743 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4744 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4745 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4746 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4747 | * table state, so each changed css_set will need its hash recomputed. |
| 4748 | * this is all done under the css_set_lock. |
| 4749 | */ |
| 4750 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4751 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4752 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4753 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4754 | continue; |
| 4755 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4756 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4757 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4758 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4759 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4760 | key = css_set_hash(cset->subsys); |
| 4761 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4762 | } |
| 4763 | write_unlock(&css_set_lock); |
| 4764 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4765 | ret = online_css(ss, cgroup_dummy_top); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4766 | if (ret) |
| 4767 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4768 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4769 | /* success! */ |
| 4770 | mutex_unlock(&cgroup_mutex); |
| 4771 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4772 | |
| 4773 | err_unload: |
| 4774 | mutex_unlock(&cgroup_mutex); |
| 4775 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4776 | cgroup_unload_subsys(ss); |
| 4777 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4778 | } |
| 4779 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4780 | |
| 4781 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4782 | * cgroup_unload_subsys: unload a modular subsystem |
| 4783 | * @ss: the subsystem to unload |
| 4784 | * |
| 4785 | * This function should be called in a modular subsystem's exitcall. When this |
| 4786 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4787 | * the subsystem will not be attached to any hierarchy. |
| 4788 | */ |
| 4789 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4790 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4791 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4792 | |
| 4793 | BUG_ON(ss->module == NULL); |
| 4794 | |
| 4795 | /* |
| 4796 | * we shouldn't be called if the subsystem is in use, and the use of |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 4797 | * try_module_get() in rebind_subsystems() should ensure that it |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4798 | * doesn't start being used while we're killing it off. |
| 4799 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4800 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4801 | |
| 4802 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4803 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4804 | offline_css(ss, cgroup_dummy_top); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4805 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4806 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4807 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4808 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4809 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4810 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4811 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4812 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4813 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4814 | |
| 4815 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4816 | * disentangle the css from all css_sets attached to the dummy |
| 4817 | * top. as in loading, we need to pay our respects to the hashtable |
| 4818 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4819 | */ |
| 4820 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4821 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4822 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4823 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4824 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4825 | hash_del(&cset->hlist); |
| 4826 | cset->subsys[ss->subsys_id] = NULL; |
| 4827 | key = css_set_hash(cset->subsys); |
| 4828 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4829 | } |
| 4830 | write_unlock(&css_set_lock); |
| 4831 | |
| 4832 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4833 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4834 | * need to free before marking as null because ss->css_free needs |
| 4835 | * the cgrp->subsys pointer to find their state. note that this |
| 4836 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4837 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4838 | ss->css_free(cgroup_dummy_top); |
| 4839 | cgroup_dummy_top->subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4840 | |
| 4841 | mutex_unlock(&cgroup_mutex); |
| 4842 | } |
| 4843 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4844 | |
| 4845 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4846 | * cgroup_init_early - cgroup initialization at system boot |
| 4847 | * |
| 4848 | * Initialize cgroups at system boot, and initialize any |
| 4849 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4850 | */ |
| 4851 | int __init cgroup_init_early(void) |
| 4852 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4853 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4854 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4855 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4856 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4857 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4858 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4859 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4860 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4861 | init_cgroup_root(&cgroup_dummy_root); |
| 4862 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4863 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4864 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4865 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4866 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4867 | list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4868 | 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] | 4869 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4870 | /* at bootup time, we don't worry about modular subsystems */ |
| 4871 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4872 | BUG_ON(!ss->name); |
| 4873 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4874 | BUG_ON(!ss->css_alloc); |
| 4875 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4876 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4877 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4878 | ss->name, ss->subsys_id); |
| 4879 | BUG(); |
| 4880 | } |
| 4881 | |
| 4882 | if (ss->early_init) |
| 4883 | cgroup_init_subsys(ss); |
| 4884 | } |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4889 | * cgroup_init - cgroup initialization |
| 4890 | * |
| 4891 | * Register cgroup filesystem and /proc file, and initialize |
| 4892 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4893 | */ |
| 4894 | int __init cgroup_init(void) |
| 4895 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4896 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4897 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4898 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4899 | |
| 4900 | err = bdi_init(&cgroup_backing_dev_info); |
| 4901 | if (err) |
| 4902 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4903 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4904 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4905 | if (!ss->early_init) |
| 4906 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4907 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4908 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4909 | } |
| 4910 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4911 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4912 | mutex_lock(&cgroup_mutex); |
| 4913 | mutex_lock(&cgroup_root_mutex); |
| 4914 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4915 | /* Add init_css_set to the hash table */ |
| 4916 | key = css_set_hash(init_css_set.subsys); |
| 4917 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 4918 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4919 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4920 | |
Li Zefan | 4e96ee8 | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4921 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 4922 | 0, 1, GFP_KERNEL); |
| 4923 | BUG_ON(err < 0); |
| 4924 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4925 | mutex_unlock(&cgroup_root_mutex); |
| 4926 | mutex_unlock(&cgroup_mutex); |
| 4927 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4928 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4929 | if (!cgroup_kobj) { |
| 4930 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4931 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | err = register_filesystem(&cgroup_fs_type); |
| 4935 | if (err < 0) { |
| 4936 | kobject_put(cgroup_kobj); |
| 4937 | goto out; |
| 4938 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4939 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4940 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4941 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4942 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4943 | if (err) |
| 4944 | bdi_destroy(&cgroup_backing_dev_info); |
| 4945 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4946 | return err; |
| 4947 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4948 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4949 | /* |
| 4950 | * proc_cgroup_show() |
| 4951 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4952 | * - Used for /proc/<pid>/cgroup. |
| 4953 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4954 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4955 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4956 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4957 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4958 | * cgroup to top_cgroup. |
| 4959 | */ |
| 4960 | |
| 4961 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4962 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4963 | { |
| 4964 | struct pid *pid; |
| 4965 | struct task_struct *tsk; |
| 4966 | char *buf; |
| 4967 | int retval; |
| 4968 | struct cgroupfs_root *root; |
| 4969 | |
| 4970 | retval = -ENOMEM; |
| 4971 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4972 | if (!buf) |
| 4973 | goto out; |
| 4974 | |
| 4975 | retval = -ESRCH; |
| 4976 | pid = m->private; |
| 4977 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4978 | if (!tsk) |
| 4979 | goto out_free; |
| 4980 | |
| 4981 | retval = 0; |
| 4982 | |
| 4983 | mutex_lock(&cgroup_mutex); |
| 4984 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4985 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4986 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4987 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4988 | int count = 0; |
| 4989 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4990 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4991 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4992 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4993 | if (strlen(root->name)) |
| 4994 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4995 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4996 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4997 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4998 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4999 | if (retval < 0) |
| 5000 | goto out_unlock; |
| 5001 | seq_puts(m, buf); |
| 5002 | seq_putc(m, '\n'); |
| 5003 | } |
| 5004 | |
| 5005 | out_unlock: |
| 5006 | mutex_unlock(&cgroup_mutex); |
| 5007 | put_task_struct(tsk); |
| 5008 | out_free: |
| 5009 | kfree(buf); |
| 5010 | out: |
| 5011 | return retval; |
| 5012 | } |
| 5013 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5014 | /* Display information about each subsystem and each hierarchy */ |
| 5015 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5016 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5017 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5018 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5019 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5020 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5021 | /* |
| 5022 | * ideally we don't want subsystems moving around while we do this. |
| 5023 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5024 | * subsys/hierarchy state. |
| 5025 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5026 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5027 | |
| 5028 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5029 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5030 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5031 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5032 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5033 | mutex_unlock(&cgroup_mutex); |
| 5034 | return 0; |
| 5035 | } |
| 5036 | |
| 5037 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5038 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5039 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5040 | } |
| 5041 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5042 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5043 | .open = cgroupstats_open, |
| 5044 | .read = seq_read, |
| 5045 | .llseek = seq_lseek, |
| 5046 | .release = single_release, |
| 5047 | }; |
| 5048 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5049 | /** |
| 5050 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5051 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5052 | * |
| 5053 | * Description: A task inherits its parent's cgroup at fork(). |
| 5054 | * |
| 5055 | * A pointer to the shared css_set was automatically copied in |
| 5056 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5057 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5058 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5059 | * have already changed current->cgroups, allowing the previously |
| 5060 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5061 | * |
| 5062 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5063 | * task, and the passed argument 'child' points to the child task. |
| 5064 | */ |
| 5065 | void cgroup_fork(struct task_struct *child) |
| 5066 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5067 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5068 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5069 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5070 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5071 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5075 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5076 | * @child: the task in question |
| 5077 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5078 | * Adds the task to the list running through its css_set if necessary and |
| 5079 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5080 | * visible on the task list in case we race with the first call to |
| 5081 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5082 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5083 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5084 | void cgroup_post_fork(struct task_struct *child) |
| 5085 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5086 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5087 | int i; |
| 5088 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5089 | /* |
| 5090 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5091 | * under the tasklist_lock and we read it here after we added the child |
| 5092 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5093 | * yet in the tasklist when we walked through it from |
| 5094 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5095 | * should be visible now due to the paired locking and barriers implied |
| 5096 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5097 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5098 | * lock on fork. |
| 5099 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5100 | if (use_task_css_set_links) { |
| 5101 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5102 | task_lock(child); |
| 5103 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5104 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5105 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5106 | write_unlock(&css_set_lock); |
| 5107 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5108 | |
| 5109 | /* |
| 5110 | * Call ss->fork(). This must happen after @child is linked on |
| 5111 | * css_set; otherwise, @child might change state between ->fork() |
| 5112 | * and addition to css_set. |
| 5113 | */ |
| 5114 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5115 | /* |
| 5116 | * fork/exit callbacks are supported only for builtin |
| 5117 | * subsystems, and the builtin section of the subsys |
| 5118 | * array is immutable, so we don't need to lock the |
| 5119 | * subsys array here. On the other hand, modular section |
| 5120 | * of the array can be freed at module unload, so we |
| 5121 | * can't touch that. |
| 5122 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5123 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5124 | if (ss->fork) |
| 5125 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5126 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5127 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5128 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5129 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5130 | * cgroup_exit - detach cgroup from exiting task |
| 5131 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5132 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5133 | * |
| 5134 | * Description: Detach cgroup from @tsk and release it. |
| 5135 | * |
| 5136 | * Note that cgroups marked notify_on_release force every task in |
| 5137 | * them to take the global cgroup_mutex mutex when exiting. |
| 5138 | * This could impact scaling on very large systems. Be reluctant to |
| 5139 | * use notify_on_release cgroups where very high task exit scaling |
| 5140 | * is required on large systems. |
| 5141 | * |
| 5142 | * the_top_cgroup_hack: |
| 5143 | * |
| 5144 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5145 | * |
| 5146 | * We call cgroup_exit() while the task is still competent to |
| 5147 | * handle notify_on_release(), then leave the task attached to the |
| 5148 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5149 | * |
| 5150 | * To do this properly, we would increment the reference count on |
| 5151 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5152 | * code we would add a second cgroup function call, to drop that |
| 5153 | * reference. This would just create an unnecessary hot spot on |
| 5154 | * the top_cgroup reference count, to no avail. |
| 5155 | * |
| 5156 | * Normally, holding a reference to a cgroup without bumping its |
| 5157 | * count is unsafe. The cgroup could go away, or someone could |
| 5158 | * attach us to a different cgroup, decrementing the count on |
| 5159 | * the first cgroup that we never incremented. But in this case, |
| 5160 | * 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] | 5161 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5162 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5163 | */ |
| 5164 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5165 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5166 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5167 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5168 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5169 | |
| 5170 | /* |
| 5171 | * Unlink from the css_set task list if necessary. |
| 5172 | * Optimistically check cg_list before taking |
| 5173 | * css_set_lock |
| 5174 | */ |
| 5175 | if (!list_empty(&tsk->cg_list)) { |
| 5176 | write_lock(&css_set_lock); |
| 5177 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5178 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5179 | write_unlock(&css_set_lock); |
| 5180 | } |
| 5181 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5182 | /* Reassign the task to the init_css_set. */ |
| 5183 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5184 | cset = task_css_set(tsk); |
| 5185 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5186 | |
| 5187 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5188 | /* |
| 5189 | * fork/exit callbacks are supported only for builtin |
| 5190 | * subsystems, see cgroup_post_fork() for details. |
| 5191 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5192 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5193 | if (ss->exit) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5194 | struct cgroup *old_cgrp = cset->subsys[i]->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5195 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5196 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5197 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5198 | } |
| 5199 | } |
| 5200 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5201 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5202 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5203 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5204 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5205 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5206 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5207 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5208 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5209 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5210 | /* |
| 5211 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5212 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5213 | * it now |
| 5214 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5215 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5216 | |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5217 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5218 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5219 | list_empty(&cgrp->release_list)) { |
| 5220 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5221 | need_schedule_work = 1; |
| 5222 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5223 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5224 | if (need_schedule_work) |
| 5225 | schedule_work(&release_agent_work); |
| 5226 | } |
| 5227 | } |
| 5228 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5229 | /* |
| 5230 | * Notify userspace when a cgroup is released, by running the |
| 5231 | * configured release agent with the name of the cgroup (path |
| 5232 | * relative to the root of cgroup file system) as the argument. |
| 5233 | * |
| 5234 | * Most likely, this user command will try to rmdir this cgroup. |
| 5235 | * |
| 5236 | * This races with the possibility that some other task will be |
| 5237 | * attached to this cgroup before it is removed, or that some other |
| 5238 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5239 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5240 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5241 | * to continue to serve a useful existence. Next time it's released, |
| 5242 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5243 | * |
| 5244 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5245 | * means only wait until the task is successfully execve()'d. The |
| 5246 | * separate release agent task is forked by call_usermodehelper(), |
| 5247 | * then control in this thread returns here, without waiting for the |
| 5248 | * release agent task. We don't bother to wait because the caller of |
| 5249 | * this routine has no use for the exit status of the release agent |
| 5250 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5251 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5252 | static void cgroup_release_agent(struct work_struct *work) |
| 5253 | { |
| 5254 | BUG_ON(work != &release_agent_work); |
| 5255 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5256 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5257 | while (!list_empty(&release_list)) { |
| 5258 | char *argv[3], *envp[3]; |
| 5259 | int i; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5260 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5261 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5262 | struct cgroup, |
| 5263 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5264 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5265 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5266 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5267 | if (!pathbuf) |
| 5268 | goto continue_free; |
| 5269 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5270 | goto continue_free; |
| 5271 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5272 | if (!agentbuf) |
| 5273 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5274 | |
| 5275 | i = 0; |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5276 | argv[i++] = agentbuf; |
| 5277 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5278 | argv[i] = NULL; |
| 5279 | |
| 5280 | i = 0; |
| 5281 | /* minimal command environment */ |
| 5282 | envp[i++] = "HOME=/"; |
| 5283 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5284 | envp[i] = NULL; |
| 5285 | |
| 5286 | /* Drop the lock while we invoke the usermode helper, |
| 5287 | * since the exec could involve hitting disk and hence |
| 5288 | * be a slow process */ |
| 5289 | mutex_unlock(&cgroup_mutex); |
| 5290 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5291 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e06 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5292 | continue_free: |
| 5293 | kfree(pathbuf); |
| 5294 | kfree(agentbuf); |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5295 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5296 | } |
Thomas Gleixner | cdcc136 | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5297 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5298 | mutex_unlock(&cgroup_mutex); |
| 5299 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5300 | |
| 5301 | static int __init cgroup_disable(char *str) |
| 5302 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5303 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5304 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5305 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5306 | |
| 5307 | while ((token = strsep(&str, ",")) != NULL) { |
| 5308 | if (!*token) |
| 5309 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5310 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5311 | /* |
| 5312 | * cgroup_disable, being at boot time, can't know about |
| 5313 | * module subsystems, so we don't worry about them. |
| 5314 | */ |
| 5315 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5316 | if (!strcmp(token, ss->name)) { |
| 5317 | ss->disabled = 1; |
| 5318 | printk(KERN_INFO "Disabling %s control group" |
| 5319 | " subsystem\n", ss->name); |
| 5320 | break; |
| 5321 | } |
| 5322 | } |
| 5323 | } |
| 5324 | return 1; |
| 5325 | } |
| 5326 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5327 | |
| 5328 | /* |
| 5329 | * Functons for CSS ID. |
| 5330 | */ |
| 5331 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5332 | /* 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] | 5333 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5334 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5335 | struct css_id *cssid; |
| 5336 | |
| 5337 | /* |
| 5338 | * This css_id() can return correct value when somone has refcnt |
| 5339 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5340 | * it's unchanged until freed. |
| 5341 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5342 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5343 | |
| 5344 | if (cssid) |
| 5345 | return cssid->id; |
| 5346 | return 0; |
| 5347 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5348 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5349 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5350 | /** |
| 5351 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5352 | * @child: the css to be tested. |
| 5353 | * @root: the css supporsed to be an ancestor of the child. |
| 5354 | * |
| 5355 | * 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] | 5356 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5357 | * But, considering usual usage, the csses should be valid objects after test. |
| 5358 | * Assuming that the caller will do some action to the child if this returns |
| 5359 | * returns true, the caller must take "child";s reference count. |
| 5360 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5361 | */ |
| 5362 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5363 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5364 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5365 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5366 | struct css_id *child_id; |
| 5367 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5368 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5369 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5370 | if (!child_id) |
| 5371 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5372 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5373 | if (!root_id) |
| 5374 | return false; |
| 5375 | if (child_id->depth < root_id->depth) |
| 5376 | return false; |
| 5377 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5378 | return false; |
| 5379 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5380 | } |
| 5381 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5382 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5383 | { |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5384 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 5385 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5386 | /* When this is called before css_id initialization, id can be NULL */ |
| 5387 | if (!id) |
| 5388 | return; |
| 5389 | |
| 5390 | BUG_ON(!ss->use_id); |
| 5391 | |
| 5392 | rcu_assign_pointer(id->css, NULL); |
| 5393 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5394 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5395 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5396 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5397 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5398 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5399 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5400 | |
| 5401 | /* |
| 5402 | * This is called by init or create(). Then, calls to this function are |
| 5403 | * always serialized (By cgroup_mutex() at create()). |
| 5404 | */ |
| 5405 | |
| 5406 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5407 | { |
| 5408 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5409 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5410 | |
| 5411 | BUG_ON(!ss->use_id); |
| 5412 | |
| 5413 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5414 | newid = kzalloc(size, GFP_KERNEL); |
| 5415 | if (!newid) |
| 5416 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5417 | |
| 5418 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5419 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5420 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5421 | 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] | 5422 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5423 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5424 | |
| 5425 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5426 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5427 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5428 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5429 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5430 | newid->depth = depth; |
| 5431 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5432 | err_out: |
| 5433 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5434 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5435 | |
| 5436 | } |
| 5437 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5438 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5439 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5440 | { |
| 5441 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5442 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5443 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5444 | idr_init(&ss->idr); |
| 5445 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5446 | newid = get_new_cssid(ss, 0); |
| 5447 | if (IS_ERR(newid)) |
| 5448 | return PTR_ERR(newid); |
| 5449 | |
| 5450 | newid->stack[0] = newid->id; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5451 | RCU_INIT_POINTER(newid->css, rootcss); |
| 5452 | RCU_INIT_POINTER(rootcss->id, newid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5457 | struct cgroup *child) |
| 5458 | { |
| 5459 | int subsys_id, i, depth = 0; |
| 5460 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5461 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5462 | |
| 5463 | subsys_id = ss->subsys_id; |
| 5464 | parent_css = parent->subsys[subsys_id]; |
| 5465 | child_css = child->subsys[subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5466 | parent_id = rcu_dereference_protected(parent_css->id, true); |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5467 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5468 | |
| 5469 | child_id = get_new_cssid(ss, depth); |
| 5470 | if (IS_ERR(child_id)) |
| 5471 | return PTR_ERR(child_id); |
| 5472 | |
| 5473 | for (i = 0; i < depth; i++) |
| 5474 | child_id->stack[i] = parent_id->stack[i]; |
| 5475 | child_id->stack[depth] = child_id->id; |
| 5476 | /* |
| 5477 | * child_id->css pointer will be set after this cgroup is available |
| 5478 | * see cgroup_populate_dir() |
| 5479 | */ |
| 5480 | rcu_assign_pointer(child_css->id, child_id); |
| 5481 | |
| 5482 | return 0; |
| 5483 | } |
| 5484 | |
| 5485 | /** |
| 5486 | * css_lookup - lookup css by id |
| 5487 | * @ss: cgroup subsys to be looked into. |
| 5488 | * @id: the id |
| 5489 | * |
| 5490 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5491 | * NULL if not. Should be called under rcu_read_lock() |
| 5492 | */ |
| 5493 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5494 | { |
| 5495 | struct css_id *cssid = NULL; |
| 5496 | |
| 5497 | BUG_ON(!ss->use_id); |
| 5498 | cssid = idr_find(&ss->idr, id); |
| 5499 | |
| 5500 | if (unlikely(!cssid)) |
| 5501 | return NULL; |
| 5502 | |
| 5503 | return rcu_dereference(cssid->css); |
| 5504 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5505 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5506 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5507 | /* |
| 5508 | * get corresponding css from file open on cgroupfs directory |
| 5509 | */ |
| 5510 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5511 | { |
| 5512 | struct cgroup *cgrp; |
| 5513 | struct inode *inode; |
| 5514 | struct cgroup_subsys_state *css; |
| 5515 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5516 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5517 | /* check in cgroup filesystem dir */ |
| 5518 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5519 | return ERR_PTR(-EBADF); |
| 5520 | |
| 5521 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5522 | return ERR_PTR(-EINVAL); |
| 5523 | |
| 5524 | /* get cgroup */ |
| 5525 | cgrp = __d_cgrp(f->f_dentry); |
| 5526 | css = cgrp->subsys[id]; |
| 5527 | return css ? css : ERR_PTR(-ENOENT); |
| 5528 | } |
| 5529 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5530 | #ifdef CONFIG_CGROUP_DEBUG |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5531 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5532 | { |
| 5533 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5534 | |
| 5535 | if (!css) |
| 5536 | return ERR_PTR(-ENOMEM); |
| 5537 | |
| 5538 | return css; |
| 5539 | } |
| 5540 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5541 | static void debug_css_free(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5542 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5543 | kfree(cgrp->subsys[debug_subsys_id]); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5544 | } |
| 5545 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5546 | static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5547 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5548 | return cgroup_task_count(cgrp); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5549 | } |
| 5550 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5551 | static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5552 | { |
| 5553 | return (u64)(unsigned long)current->cgroups; |
| 5554 | } |
| 5555 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5556 | static u64 current_css_set_refcount_read(struct cgroup *cgrp, |
| 5557 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5558 | { |
| 5559 | u64 count; |
| 5560 | |
| 5561 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5562 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5563 | rcu_read_unlock(); |
| 5564 | return count; |
| 5565 | } |
| 5566 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5567 | static int current_css_set_cg_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5568 | struct cftype *cft, |
| 5569 | struct seq_file *seq) |
| 5570 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5571 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5572 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5573 | |
| 5574 | read_lock(&css_set_lock); |
| 5575 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5576 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5577 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5578 | struct cgroup *c = link->cgrp; |
| 5579 | const char *name; |
| 5580 | |
| 5581 | if (c->dentry) |
| 5582 | name = c->dentry->d_name.name; |
| 5583 | else |
| 5584 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5585 | seq_printf(seq, "Root %d group %s\n", |
| 5586 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5587 | } |
| 5588 | rcu_read_unlock(); |
| 5589 | read_unlock(&css_set_lock); |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
| 5593 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5594 | static int cgroup_css_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5595 | struct cftype *cft, |
| 5596 | struct seq_file *seq) |
| 5597 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5598 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5599 | |
| 5600 | read_lock(&css_set_lock); |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5601 | list_for_each_entry(link, &cgrp->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5602 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5603 | struct task_struct *task; |
| 5604 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5605 | seq_printf(seq, "css_set %p\n", cset); |
| 5606 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5607 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5608 | seq_puts(seq, " ...\n"); |
| 5609 | break; |
| 5610 | } else { |
| 5611 | seq_printf(seq, " task %d\n", |
| 5612 | task_pid_vnr(task)); |
| 5613 | } |
| 5614 | } |
| 5615 | } |
| 5616 | read_unlock(&css_set_lock); |
| 5617 | return 0; |
| 5618 | } |
| 5619 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5620 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5621 | { |
| 5622 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5623 | } |
| 5624 | |
| 5625 | static struct cftype debug_files[] = { |
| 5626 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5627 | .name = "taskcount", |
| 5628 | .read_u64 = debug_taskcount_read, |
| 5629 | }, |
| 5630 | |
| 5631 | { |
| 5632 | .name = "current_css_set", |
| 5633 | .read_u64 = current_css_set_read, |
| 5634 | }, |
| 5635 | |
| 5636 | { |
| 5637 | .name = "current_css_set_refcount", |
| 5638 | .read_u64 = current_css_set_refcount_read, |
| 5639 | }, |
| 5640 | |
| 5641 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5642 | .name = "current_css_set_cg_links", |
| 5643 | .read_seq_string = current_css_set_cg_links_read, |
| 5644 | }, |
| 5645 | |
| 5646 | { |
| 5647 | .name = "cgroup_css_links", |
| 5648 | .read_seq_string = cgroup_css_links_read, |
| 5649 | }, |
| 5650 | |
| 5651 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5652 | .name = "releasable", |
| 5653 | .read_u64 = releasable_read, |
| 5654 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5655 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5656 | { } /* terminate */ |
| 5657 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5658 | |
| 5659 | struct cgroup_subsys debug_subsys = { |
| 5660 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5661 | .css_alloc = debug_css_alloc, |
| 5662 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5663 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5664 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5665 | }; |
| 5666 | #endif /* CONFIG_CGROUP_DEBUG */ |