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); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 222 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 223 | 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 | cdcc136f | 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 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 370 | /* |
| 371 | * css_set_lock protects the list of css_set objects, and the chain of |
| 372 | * tasks off each css_set. Nests outside task->alloc_lock due to |
| 373 | * cgroup_task_iter_start(). |
| 374 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 375 | static DEFINE_RWLOCK(css_set_lock); |
| 376 | static int css_set_count; |
| 377 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 378 | /* |
| 379 | * hash table for cgroup groups. This improves the performance to find |
| 380 | * an existing css_set. This hash doesn't (currently) take into |
| 381 | * account cgroups in empty hierarchies. |
| 382 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 383 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 384 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
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 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 387 | { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 388 | unsigned long key = 0UL; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 389 | struct cgroup_subsys *ss; |
| 390 | int i; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 391 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 392 | for_each_subsys(ss, i) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 393 | key += (unsigned long)css[i]; |
| 394 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 395 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 396 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 399 | /* |
| 400 | * We don't maintain the lists running through each css_set to its task |
| 401 | * until after the first call to cgroup_task_iter_start(). This reduces |
| 402 | * the fork()/exit() overhead for people who have cgroups compiled into |
| 403 | * their kernel but not actually in use. |
| 404 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 405 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 406 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 407 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 408 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 409 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 410 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Ensure that the refcount doesn't hit zero while any readers |
| 413 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 414 | * rwlock |
| 415 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 416 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 417 | return; |
| 418 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 419 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 420 | write_unlock(&css_set_lock); |
| 421 | return; |
| 422 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 423 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 424 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 425 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 426 | css_set_count--; |
| 427 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 428 | 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] | 429 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 430 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 431 | list_del(&link->cset_link); |
| 432 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 433 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 434 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 435 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 436 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 437 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 438 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 439 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 440 | |
| 441 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 442 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 443 | |
| 444 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 445 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | * refcounted get/put for css_set objects |
| 450 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 451 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 452 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 453 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 456 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 457 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 458 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 461 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 462 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 463 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 466 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 467 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 468 | * @cset: candidate css_set being tested |
| 469 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 470 | * @new_cgrp: cgroup that's being entered by the task |
| 471 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 472 | * |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 473 | * Returns true if "cset" matches "old_cset" except for the hierarchy |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 474 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 475 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 476 | static bool compare_css_sets(struct css_set *cset, |
| 477 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | struct cgroup *new_cgrp, |
| 479 | struct cgroup_subsys_state *template[]) |
| 480 | { |
| 481 | struct list_head *l1, *l2; |
| 482 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 483 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 484 | /* Not all subsystems matched */ |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Compare cgroup pointers in order to distinguish between |
| 490 | * different cgroups in heirarchies with no subsystems. We |
| 491 | * could get by with just this check alone (and skip the |
| 492 | * memcmp above) but on most setups the memcmp check will |
| 493 | * avoid the need for this more expensive check on almost all |
| 494 | * candidates. |
| 495 | */ |
| 496 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 497 | l1 = &cset->cgrp_links; |
| 498 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 499 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 500 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 501 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 502 | |
| 503 | l1 = l1->next; |
| 504 | l2 = l2->next; |
| 505 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 506 | if (l1 == &cset->cgrp_links) { |
| 507 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 508 | break; |
| 509 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 510 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 511 | } |
| 512 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 513 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 514 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 515 | cgrp1 = link1->cgrp; |
| 516 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 517 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 518 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * If this hierarchy is the hierarchy of the cgroup |
| 522 | * that's changing, then we need to check that this |
| 523 | * css_set points to the new cgroup; if it's any other |
| 524 | * hierarchy, then this css_set should point to the |
| 525 | * same cgroup as the old css_set. |
| 526 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | if (cgrp1->root == new_cgrp->root) { |
| 528 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 529 | return false; |
| 530 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 531 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 538 | /** |
| 539 | * find_existing_css_set - init css array and find the matching css_set |
| 540 | * @old_cset: the css_set that we're using before the cgroup transition |
| 541 | * @cgrp: the cgroup that we're moving into |
| 542 | * @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] | 543 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 544 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 545 | struct cgroup *cgrp, |
| 546 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 547 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 548 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 549 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 550 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 551 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 552 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 553 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 554 | /* |
| 555 | * Build the set of subsystem state objects that we want to see in the |
| 556 | * new css_set. while subsystems can change globally, the entries here |
| 557 | * won't change, so no need for locking. |
| 558 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 559 | for_each_subsys(ss, i) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 560 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | /* Subsystem is in this hierarchy. So we want |
| 562 | * the subsystem state from the new |
| 563 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 564 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 565 | } else { |
| 566 | /* Subsystem is not in this hierarchy, so we |
| 567 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 568 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 572 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 573 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 574 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 575 | continue; |
| 576 | |
| 577 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 578 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 579 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 580 | |
| 581 | /* No existing cgroup group matched */ |
| 582 | return NULL; |
| 583 | } |
| 584 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 585 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 586 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 587 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 588 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 589 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 590 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 591 | kfree(link); |
| 592 | } |
| 593 | } |
| 594 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 595 | /** |
| 596 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 597 | * @count: the number of links to allocate |
| 598 | * @tmp_links: list_head the allocated links are put on |
| 599 | * |
| 600 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 601 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 602 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | 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] | 604 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 605 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 606 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 607 | |
| 608 | INIT_LIST_HEAD(tmp_links); |
| 609 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 610 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 611 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 612 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 613 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 614 | return -ENOMEM; |
| 615 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 616 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 617 | } |
| 618 | return 0; |
| 619 | } |
| 620 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 621 | /** |
| 622 | * 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] | 623 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 624 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 625 | * @cgrp: the destination cgroup |
| 626 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 627 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 628 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 629 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 630 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 631 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 632 | BUG_ON(list_empty(tmp_links)); |
| 633 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 634 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 635 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 636 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 637 | /* |
| 638 | * Always add links to the tail of the list so that the list |
| 639 | * is sorted by order of hierarchy creation |
| 640 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 641 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 644 | /** |
| 645 | * find_css_set - return a new css_set with one cgroup updated |
| 646 | * @old_cset: the baseline css_set |
| 647 | * @cgrp: the cgroup to be updated |
| 648 | * |
| 649 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 650 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 651 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 652 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 653 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 655 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 656 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 657 | struct list_head tmp_links; |
| 658 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 659 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 660 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 661 | lockdep_assert_held(&cgroup_mutex); |
| 662 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 663 | /* First see if we already have a cgroup group that matches |
| 664 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 665 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 666 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 667 | if (cset) |
| 668 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 669 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 670 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 671 | if (cset) |
| 672 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 673 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 674 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 675 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 676 | return NULL; |
| 677 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 678 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 679 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 680 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 681 | return NULL; |
| 682 | } |
| 683 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 684 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 685 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 686 | INIT_LIST_HEAD(&cset->tasks); |
| 687 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 688 | |
| 689 | /* Copy the set of subsystem state objects generated in |
| 690 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 691 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 692 | |
| 693 | write_lock(&css_set_lock); |
| 694 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 695 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 696 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 697 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 698 | if (c->root == cgrp->root) |
| 699 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 700 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 701 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 702 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 703 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 704 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 705 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 706 | |
| 707 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 708 | key = css_set_hash(cset->subsys); |
| 709 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 710 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 711 | write_unlock(&css_set_lock); |
| 712 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 713 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 716 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 717 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 718 | * called with cgroup_mutex held. |
| 719 | */ |
| 720 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 721 | struct cgroupfs_root *root) |
| 722 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 723 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 724 | struct cgroup *res = NULL; |
| 725 | |
| 726 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 727 | read_lock(&css_set_lock); |
| 728 | /* |
| 729 | * No need to lock the task - since we hold cgroup_mutex the |
| 730 | * task can't change groups, so the only thing that can happen |
| 731 | * is that it exits and its css is set back to init_css_set. |
| 732 | */ |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 733 | cset = task_css_set(task); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 734 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 735 | res = &root->top_cgroup; |
| 736 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 737 | struct cgrp_cset_link *link; |
| 738 | |
| 739 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 740 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 741 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 742 | if (c->root == root) { |
| 743 | res = c; |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | read_unlock(&css_set_lock); |
| 749 | BUG_ON(!res); |
| 750 | return res; |
| 751 | } |
| 752 | |
| 753 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 754 | * There is one global cgroup mutex. We also require taking |
| 755 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 756 | * See "The task_lock() exception", at the end of this comment. |
| 757 | * |
| 758 | * A task must hold cgroup_mutex to modify cgroups. |
| 759 | * |
| 760 | * Any task can increment and decrement the count field without lock. |
| 761 | * So in general, code holding cgroup_mutex can't rely on the count |
| 762 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 763 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 764 | * means that no tasks are currently attached, therefore there is no |
| 765 | * way a task attached to that cgroup can fork (the other way to |
| 766 | * increment the count). So code holding cgroup_mutex can safely |
| 767 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 768 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 769 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 770 | * needs that mutex. |
| 771 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 772 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 773 | * (usually) take cgroup_mutex. These are the two most performance |
| 774 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 775 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 776 | * 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] | 777 | * to the release agent with the name of the cgroup (path relative to |
| 778 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 779 | * |
| 780 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 781 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 782 | * tasks in the system use _some_ cgroup, and since there is always at |
| 783 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 784 | * always has either children cgroups and/or using tasks. So we don't |
| 785 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 786 | * |
| 787 | * The task_lock() exception |
| 788 | * |
| 789 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 790 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 791 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 792 | * several performance critical places that need to reference |
| 793 | * task->cgroup without the expense of grabbing a system global |
| 794 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 795 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 796 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 797 | * the task_struct routinely used for such matters. |
| 798 | * |
| 799 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 800 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 801 | */ |
| 802 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 803 | /* |
| 804 | * A couple of forward declarations required, due to cyclic reference loop: |
| 805 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 806 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 807 | * -> cgroup_mkdir. |
| 808 | */ |
| 809 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 810 | 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] | 811 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 812 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 813 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 814 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 815 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 816 | |
| 817 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 818 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 819 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 820 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 821 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 822 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 823 | struct cgroup *parent, struct cgroup *child); |
| 824 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 825 | 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] | 826 | { |
| 827 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 828 | |
| 829 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 830 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 831 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 832 | inode->i_uid = current_fsuid(); |
| 833 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 834 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 835 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 836 | } |
| 837 | return inode; |
| 838 | } |
| 839 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 840 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 841 | { |
| 842 | struct cgroup_name *name; |
| 843 | |
| 844 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 845 | if (!name) |
| 846 | return NULL; |
| 847 | strcpy(name->name, dentry->d_name.name); |
| 848 | return name; |
| 849 | } |
| 850 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 851 | static void cgroup_free_fn(struct work_struct *work) |
| 852 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 853 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 854 | struct cgroup_subsys *ss; |
| 855 | |
| 856 | mutex_lock(&cgroup_mutex); |
| 857 | /* |
| 858 | * Release the subsystem state objects. |
| 859 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 860 | for_each_root_subsys(cgrp->root, ss) { |
| 861 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 862 | |
| 863 | ss->css_free(css); |
| 864 | } |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 865 | |
| 866 | cgrp->root->number_of_cgroups--; |
| 867 | mutex_unlock(&cgroup_mutex); |
| 868 | |
| 869 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 870 | * We get a ref to the parent's dentry, and put the ref when |
| 871 | * this cgroup is being freed, so it's guaranteed that the |
| 872 | * parent won't be destroyed before its children. |
| 873 | */ |
| 874 | dput(cgrp->parent->dentry); |
| 875 | |
| 876 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 877 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 878 | * created the cgroup. This will free cgrp->root, if we are |
| 879 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 880 | */ |
| 881 | deactivate_super(cgrp->root->sb); |
| 882 | |
| 883 | /* |
| 884 | * if we're getting rid of the cgroup, refcount should ensure |
| 885 | * that there are no pidlists left. |
| 886 | */ |
| 887 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 888 | |
| 889 | simple_xattrs_free(&cgrp->xattrs); |
| 890 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 891 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 892 | kfree(cgrp); |
| 893 | } |
| 894 | |
| 895 | static void cgroup_free_rcu(struct rcu_head *head) |
| 896 | { |
| 897 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 898 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 899 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 900 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 901 | } |
| 902 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 903 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 904 | { |
| 905 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 906 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 907 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 908 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 909 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 910 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 911 | } else { |
| 912 | struct cfent *cfe = __d_cfe(dentry); |
| 913 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 914 | |
| 915 | WARN_ONCE(!list_empty(&cfe->node) && |
| 916 | cgrp != &cgrp->root->top_cgroup, |
| 917 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 918 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 919 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 920 | } |
| 921 | iput(inode); |
| 922 | } |
| 923 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 924 | static int cgroup_delete(const struct dentry *d) |
| 925 | { |
| 926 | return 1; |
| 927 | } |
| 928 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 929 | static void remove_dir(struct dentry *d) |
| 930 | { |
| 931 | struct dentry *parent = dget(d->d_parent); |
| 932 | |
| 933 | d_delete(d); |
| 934 | simple_rmdir(parent->d_inode, d); |
| 935 | dput(parent); |
| 936 | } |
| 937 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 938 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 939 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 940 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 941 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 942 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 943 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 944 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 945 | /* |
| 946 | * If we're doing cleanup due to failure of cgroup_create(), |
| 947 | * the corresponding @cfe may not exist. |
| 948 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 949 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 950 | struct dentry *d = cfe->dentry; |
| 951 | |
| 952 | if (cft && cfe->type != cft) |
| 953 | continue; |
| 954 | |
| 955 | dget(d); |
| 956 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 957 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 958 | list_del_init(&cfe->node); |
| 959 | dput(d); |
| 960 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 961 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 962 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 965 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 966 | * cgroup_clear_dir - remove subsys files in a cgroup directory |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 967 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 968 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 969 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 970 | static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 971 | { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 972 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 973 | int i; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 974 | |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 975 | for_each_subsys(ss, i) { |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 976 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 977 | |
| 978 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 979 | continue; |
| 980 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 981 | cgroup_addrm_files(cgrp, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 982 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* |
| 986 | * NOTE : the dentry must have been dget()'ed |
| 987 | */ |
| 988 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 989 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 990 | struct dentry *parent; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 991 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 992 | parent = dentry->d_parent; |
| 993 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 994 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 995 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 996 | spin_unlock(&dentry->d_lock); |
| 997 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 998 | remove_dir(dentry); |
| 999 | } |
| 1000 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 1001 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1002 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 1003 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 1004 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1005 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1007 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1008 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1009 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1010 | struct cgroup_subsys *ss; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1011 | unsigned long pinned = 0; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1012 | int i, ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1013 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1014 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1015 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1016 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1017 | /* Check that any added subsystems are currently free */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1018 | for_each_subsys(ss, i) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1019 | if (!(added_mask & (1 << i))) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1020 | continue; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1021 | |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1022 | /* is the subsystem mounted elsewhere? */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1023 | if (ss->root != &cgroup_dummy_root) { |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1024 | ret = -EBUSY; |
| 1025 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1026 | } |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* pin the module */ |
| 1029 | if (!try_module_get(ss->module)) { |
| 1030 | ret = -ENOENT; |
| 1031 | goto out_put; |
| 1032 | } |
| 1033 | pinned |= 1 << i; |
| 1034 | } |
| 1035 | |
| 1036 | /* subsys could be missing if unloaded between parsing and here */ |
| 1037 | if (added_mask != pinned) { |
| 1038 | ret = -ENOENT; |
| 1039 | goto out_put; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1042 | ret = cgroup_populate_dir(cgrp, added_mask); |
| 1043 | if (ret) |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1044 | goto out_put; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* |
| 1047 | * Nothing can fail from this point on. Remove files for the |
| 1048 | * removed subsystems and rebind each subsystem. |
| 1049 | */ |
| 1050 | cgroup_clear_dir(cgrp, removed_mask); |
| 1051 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1052 | for_each_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1053 | unsigned long bit = 1UL << i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1054 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1055 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1056 | /* We're binding this subsystem to this hierarchy */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1057 | BUG_ON(cgrp->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1058 | BUG_ON(!cgroup_dummy_top->subsys[i]); |
| 1059 | BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1060 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1061 | cgrp->subsys[i] = cgroup_dummy_top->subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1062 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1063 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1064 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1065 | if (ss->bind) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1066 | ss->bind(cgrp->subsys[i]); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1067 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1068 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1069 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1070 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1071 | /* We're removing this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1072 | BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1073 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1074 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1075 | if (ss->bind) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1076 | ss->bind(cgroup_dummy_top->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1077 | cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1078 | cgrp->subsys[i] = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1079 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1080 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1081 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1082 | /* subsystem is now free - drop reference on module */ |
| 1083 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1084 | root->subsys_mask &= ~bit; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1088 | /* |
| 1089 | * Mark @root has finished binding subsystems. @root->subsys_mask |
| 1090 | * now matches the bound subsystems. |
| 1091 | */ |
| 1092 | root->flags |= CGRP_ROOT_SUBSYS_BOUND; |
| 1093 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1094 | return 0; |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1095 | |
| 1096 | out_put: |
| 1097 | for_each_subsys(ss, i) |
| 1098 | if (pinned & (1 << i)) |
| 1099 | module_put(ss->module); |
| 1100 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1103 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1104 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1105 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1106 | struct cgroup_subsys *ss; |
| 1107 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1108 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 1109 | for_each_root_subsys(root, ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1111 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1112 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1113 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1114 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1115 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1116 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1117 | if (strlen(root->release_agent_path)) |
| 1118 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1119 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1120 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1121 | if (strlen(root->name)) |
| 1122 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1123 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1128 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1129 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1130 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1131 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1132 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1133 | /* User explicitly requested empty subsystem */ |
| 1134 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1135 | |
| 1136 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1137 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1138 | }; |
| 1139 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1140 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1141 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1142 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1143 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1144 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1145 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1146 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1147 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1148 | char *token, *o = data; |
| 1149 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1150 | unsigned long mask = (unsigned long)-1; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1151 | struct cgroup_subsys *ss; |
| 1152 | int i; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1153 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1154 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1155 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1156 | #ifdef CONFIG_CPUSETS |
| 1157 | mask = ~(1UL << cpuset_subsys_id); |
| 1158 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1159 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1160 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1161 | |
| 1162 | while ((token = strsep(&o, ",")) != NULL) { |
| 1163 | if (!*token) |
| 1164 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1165 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1166 | /* Explicitly have no subsystems */ |
| 1167 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1168 | continue; |
| 1169 | } |
| 1170 | if (!strcmp(token, "all")) { |
| 1171 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1172 | if (one_ss) |
| 1173 | return -EINVAL; |
| 1174 | all_ss = true; |
| 1175 | continue; |
| 1176 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1177 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1178 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1179 | continue; |
| 1180 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1181 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1182 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
| 1185 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1186 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1187 | continue; |
| 1188 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1189 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1190 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1191 | continue; |
| 1192 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1193 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1194 | /* Specifying two release agents is forbidden */ |
| 1195 | if (opts->release_agent) |
| 1196 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1197 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1198 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1199 | if (!opts->release_agent) |
| 1200 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1201 | continue; |
| 1202 | } |
| 1203 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1204 | const char *name = token + 5; |
| 1205 | /* Can't specify an empty name */ |
| 1206 | if (!strlen(name)) |
| 1207 | return -EINVAL; |
| 1208 | /* Must match [\w.-]+ */ |
| 1209 | for (i = 0; i < strlen(name); i++) { |
| 1210 | char c = name[i]; |
| 1211 | if (isalnum(c)) |
| 1212 | continue; |
| 1213 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1214 | continue; |
| 1215 | return -EINVAL; |
| 1216 | } |
| 1217 | /* Specifying two names is forbidden */ |
| 1218 | if (opts->name) |
| 1219 | return -EINVAL; |
| 1220 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1221 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1222 | GFP_KERNEL); |
| 1223 | if (!opts->name) |
| 1224 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1225 | |
| 1226 | continue; |
| 1227 | } |
| 1228 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1229 | for_each_subsys(ss, i) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1230 | if (strcmp(token, ss->name)) |
| 1231 | continue; |
| 1232 | if (ss->disabled) |
| 1233 | continue; |
| 1234 | |
| 1235 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1236 | if (all_ss) |
| 1237 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1238 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1239 | one_ss = true; |
| 1240 | |
| 1241 | break; |
| 1242 | } |
| 1243 | if (i == CGROUP_SUBSYS_COUNT) |
| 1244 | return -ENOENT; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1249 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1250 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1251 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1252 | if (all_ss || (!one_ss && !opts->none && !opts->name)) |
| 1253 | for_each_subsys(ss, i) |
| 1254 | if (!ss->disabled) |
| 1255 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1256 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1257 | /* Consistency checks */ |
| 1258 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1259 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1260 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1261 | |
| 1262 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1263 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | |
| 1267 | if (opts->cpuset_clone_children) { |
| 1268 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1273 | /* |
| 1274 | * Option noprefix was introduced just for backward compatibility |
| 1275 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1276 | * the cpuset subsystem. |
| 1277 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1278 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1279 | return -EINVAL; |
| 1280 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1281 | |
| 1282 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1283 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1284 | return -EINVAL; |
| 1285 | |
| 1286 | /* |
| 1287 | * We either have to specify by name or by subsystems. (So all |
| 1288 | * empty hierarchies must have a name). |
| 1289 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1290 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1291 | return -EINVAL; |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1297 | { |
| 1298 | int ret = 0; |
| 1299 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1300 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1301 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1302 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1303 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1304 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1305 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1309 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1310 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1311 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* See what subsystems are wanted */ |
| 1314 | ret = parse_cgroupfs_options(data, &opts); |
| 1315 | if (ret) |
| 1316 | goto out_unlock; |
| 1317 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1318 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1319 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1320 | task_tgid_nr(current), current->comm); |
| 1321 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1322 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1323 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1324 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1325 | /* Don't allow flags or name to change at remount */ |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1326 | if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) || |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1327 | (opts.name && strcmp(opts.name, root->name))) { |
Tejun Heo | 0ce6cba | 2013-06-27 19:37:26 -0700 | [diff] [blame] | 1328 | pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n", |
| 1329 | opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "", |
| 1330 | root->flags & CGRP_ROOT_OPTION_MASK, root->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1331 | ret = -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1332 | goto out_unlock; |
| 1333 | } |
| 1334 | |
Tejun Heo | f172e67 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1335 | /* remounting is not allowed for populated hierarchies */ |
| 1336 | if (root->number_of_cgroups > 1) { |
| 1337 | ret = -EBUSY; |
| 1338 | goto out_unlock; |
| 1339 | } |
| 1340 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1341 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1342 | if (ret) |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1343 | goto out_unlock; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1344 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1345 | if (opts.release_agent) |
| 1346 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1347 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1348 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1349 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1350 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1351 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1352 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1353 | return ret; |
| 1354 | } |
| 1355 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1356 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1357 | .statfs = simple_statfs, |
| 1358 | .drop_inode = generic_delete_inode, |
| 1359 | .show_options = cgroup_show_options, |
| 1360 | .remount_fs = cgroup_remount, |
| 1361 | }; |
| 1362 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1363 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1364 | { |
| 1365 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1366 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1367 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1368 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1369 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1370 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1371 | mutex_init(&cgrp->pidlist_mutex); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 1372 | cgrp->dummy_css.cgroup = cgrp; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1373 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1374 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1375 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1376 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1377 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1378 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1379 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1380 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1381 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1382 | INIT_LIST_HEAD(&root->subsys_list); |
| 1383 | INIT_LIST_HEAD(&root->root_list); |
| 1384 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1385 | cgrp->root = root; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 1386 | RCU_INIT_POINTER(cgrp->name, &root_cgroup_name); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1387 | init_cgroup_housekeeping(cgrp); |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1388 | idr_init(&root->cgroup_idr); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1391 | 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] | 1392 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1393 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1394 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1395 | lockdep_assert_held(&cgroup_mutex); |
| 1396 | lockdep_assert_held(&cgroup_root_mutex); |
| 1397 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1398 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end, |
| 1399 | GFP_KERNEL); |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1400 | if (id < 0) |
| 1401 | return id; |
| 1402 | |
| 1403 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1408 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1409 | lockdep_assert_held(&cgroup_mutex); |
| 1410 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1411 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1412 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1413 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1414 | root->hierarchy_id = 0; |
| 1415 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1418 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1419 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1420 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1421 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1422 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1423 | /* If we asked for a name then it must match */ |
| 1424 | if (opts->name && strcmp(opts->name, root->name)) |
| 1425 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1426 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1427 | /* |
| 1428 | * If we asked for subsystems (or explicitly for no |
| 1429 | * subsystems) then they must match |
| 1430 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1431 | if ((opts->subsys_mask || opts->none) |
| 1432 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1438 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1439 | { |
| 1440 | struct cgroupfs_root *root; |
| 1441 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1442 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1443 | return NULL; |
| 1444 | |
| 1445 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1446 | if (!root) |
| 1447 | return ERR_PTR(-ENOMEM); |
| 1448 | |
| 1449 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1450 | |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * We need to set @root->subsys_mask now so that @root can be |
| 1453 | * matched by cgroup_test_super() before it finishes |
| 1454 | * initialization; otherwise, competing mounts with the same |
| 1455 | * options may try to bind the same subsystems instead of waiting |
| 1456 | * for the first one leading to unexpected mount errors. |
| 1457 | * SUBSYS_BOUND will be set once actual binding is complete. |
| 1458 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1459 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1460 | root->flags = opts->flags; |
| 1461 | if (opts->release_agent) |
| 1462 | strcpy(root->release_agent_path, opts->release_agent); |
| 1463 | if (opts->name) |
| 1464 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1465 | if (opts->cpuset_clone_children) |
| 1466 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1467 | return root; |
| 1468 | } |
| 1469 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1470 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1471 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1472 | if (root) { |
| 1473 | /* hierarhcy ID shoulid already have been released */ |
| 1474 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1475 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1476 | idr_destroy(&root->cgroup_idr); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1477 | kfree(root); |
| 1478 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1481 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1482 | { |
| 1483 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1484 | struct cgroup_sb_opts *opts = data; |
| 1485 | |
| 1486 | /* If we don't have a new root, we can't set up a new sb */ |
| 1487 | if (!opts->new_root) |
| 1488 | return -EINVAL; |
| 1489 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1490 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1491 | |
| 1492 | ret = set_anon_super(sb, NULL); |
| 1493 | if (ret) |
| 1494 | return ret; |
| 1495 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1496 | sb->s_fs_info = opts->new_root; |
| 1497 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1498 | |
| 1499 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1500 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1501 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1502 | sb->s_op = &cgroup_ops; |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1508 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1509 | static const struct dentry_operations cgroup_dops = { |
| 1510 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1511 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1512 | }; |
| 1513 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1514 | struct inode *inode = |
| 1515 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1516 | |
| 1517 | if (!inode) |
| 1518 | return -ENOMEM; |
| 1519 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1520 | inode->i_fop = &simple_dir_operations; |
| 1521 | inode->i_op = &cgroup_dir_inode_operations; |
| 1522 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1523 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1524 | sb->s_root = d_make_root(inode); |
| 1525 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1526 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1527 | /* for everything else we want ->d_op set */ |
| 1528 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1532 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1533 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1534 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1535 | { |
| 1536 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1537 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1538 | int ret = 0; |
| 1539 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1540 | struct cgroupfs_root *new_root; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1541 | struct list_head tmp_links; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1542 | struct inode *inode; |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1543 | const struct cred *cred; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1544 | |
| 1545 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1546 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1547 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1548 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1549 | if (ret) |
| 1550 | goto out_err; |
| 1551 | |
| 1552 | /* |
| 1553 | * Allocate a new cgroup root. We may not need it if we're |
| 1554 | * reusing an existing hierarchy. |
| 1555 | */ |
| 1556 | new_root = cgroup_root_from_opts(&opts); |
| 1557 | if (IS_ERR(new_root)) { |
| 1558 | ret = PTR_ERR(new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1559 | goto out_err; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1560 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1561 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1562 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1563 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1564 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1565 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1566 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1567 | cgroup_free_root(opts.new_root); |
Tejun Heo | 1d5be6b | 2013-07-12 13:38:17 -0700 | [diff] [blame] | 1568 | goto out_err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1571 | root = sb->s_fs_info; |
| 1572 | BUG_ON(!root); |
| 1573 | if (root == opts.new_root) { |
| 1574 | /* We used the new root structure, so this is a new hierarchy */ |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1575 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1576 | struct cgroupfs_root *existing_root; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1577 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1578 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1579 | |
| 1580 | BUG_ON(sb->s_root != NULL); |
| 1581 | |
| 1582 | ret = cgroup_get_rootdir(sb); |
| 1583 | if (ret) |
| 1584 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1585 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1586 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1587 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1588 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1589 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1590 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 1591 | root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp, |
| 1592 | 0, 1, GFP_KERNEL); |
| 1593 | if (root_cgrp->id < 0) |
| 1594 | goto unlock_drop; |
| 1595 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1596 | /* Check for name clashes with existing mounts */ |
| 1597 | ret = -EBUSY; |
| 1598 | if (strlen(root->name)) |
| 1599 | for_each_active_root(existing_root) |
| 1600 | if (!strcmp(existing_root->name, root->name)) |
| 1601 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1602 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1603 | /* |
| 1604 | * We're accessing css_set_count without locking |
| 1605 | * css_set_lock here, but that's OK - it can only be |
| 1606 | * increased by someone holding cgroup_lock, and |
| 1607 | * that's us. The worst that can happen is that we |
| 1608 | * have some link structures left over |
| 1609 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1610 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1611 | if (ret) |
| 1612 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1613 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 1614 | /* ID 0 is reserved for dummy root, 1 for unified hierarchy */ |
| 1615 | ret = cgroup_init_root_id(root, 2, 0); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1616 | if (ret) |
| 1617 | goto unlock_drop; |
| 1618 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1619 | sb->s_root->d_fsdata = root_cgrp; |
| 1620 | root_cgrp->dentry = sb->s_root; |
| 1621 | |
| 1622 | /* |
| 1623 | * We're inside get_sb() and will call lookup_one_len() to |
| 1624 | * create the root files, which doesn't work if SELinux is |
| 1625 | * in use. The following cred dancing somehow works around |
| 1626 | * it. See 2ce9738ba ("cgroupfs: use init_cred when |
| 1627 | * populating new cgroupfs mount") for more details. |
| 1628 | */ |
| 1629 | cred = override_creds(&init_cred); |
| 1630 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1631 | ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1632 | if (ret) |
| 1633 | goto rm_base_files; |
| 1634 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1635 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1636 | if (ret) |
| 1637 | goto rm_base_files; |
| 1638 | |
| 1639 | revert_creds(cred); |
| 1640 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1641 | /* |
| 1642 | * There must be no failure case after here, since rebinding |
| 1643 | * takes care of subsystems' refcounts, which are explicitly |
| 1644 | * dropped in the failure exit path. |
| 1645 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1646 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1647 | list_add(&root->root_list, &cgroup_roots); |
| 1648 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1649 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1650 | /* Link the top cgroup in this hierarchy into all |
| 1651 | * the css_set objects */ |
| 1652 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1653 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1654 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1655 | write_unlock(&css_set_lock); |
| 1656 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1657 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1658 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1659 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1660 | BUG_ON(root->number_of_cgroups != 1); |
| 1661 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1662 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1663 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1664 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1665 | } else { |
| 1666 | /* |
| 1667 | * We re-used an existing hierarchy - the new root (if |
| 1668 | * any) is not needed |
| 1669 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1670 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1671 | |
Tejun Heo | c7ba828 | 2013-06-29 14:06:10 -0700 | [diff] [blame] | 1672 | if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) { |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1673 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1674 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1675 | ret = -EINVAL; |
| 1676 | goto drop_new_super; |
| 1677 | } else { |
| 1678 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1679 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1680 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1683 | kfree(opts.release_agent); |
| 1684 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1685 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1686 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1687 | rm_base_files: |
| 1688 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1689 | cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1690 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1691 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1692 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1693 | mutex_unlock(&cgroup_root_mutex); |
| 1694 | mutex_unlock(&cgroup_mutex); |
| 1695 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1696 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1697 | deactivate_locked_super(sb); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1698 | out_err: |
| 1699 | kfree(opts.release_agent); |
| 1700 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1701 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1705 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1706 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1707 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1708 | int ret; |
| 1709 | |
| 1710 | BUG_ON(!root); |
| 1711 | |
| 1712 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1713 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1714 | |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1715 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1716 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1717 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1718 | |
| 1719 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | 1672d04 | 2013-06-25 18:04:54 -0700 | [diff] [blame] | 1720 | if (root->flags & CGRP_ROOT_SUBSYS_BOUND) { |
| 1721 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
| 1722 | /* Shouldn't be able to fail ... */ |
| 1723 | BUG_ON(ret); |
| 1724 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1725 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1726 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1727 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1728 | * root cgroup |
| 1729 | */ |
| 1730 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1731 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1732 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1733 | list_del(&link->cset_link); |
| 1734 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1735 | kfree(link); |
| 1736 | } |
| 1737 | write_unlock(&css_set_lock); |
| 1738 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1739 | if (!list_empty(&root->root_list)) { |
| 1740 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1741 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1742 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1743 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1744 | cgroup_exit_root_id(root); |
| 1745 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1746 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1747 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 3126121 | 2013-06-28 17:07:30 -0700 | [diff] [blame] | 1748 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1749 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1750 | simple_xattrs_free(&cgrp->xattrs); |
| 1751 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1752 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1753 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static struct file_system_type cgroup_fs_type = { |
| 1757 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1758 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | .kill_sb = cgroup_kill_sb, |
| 1760 | }; |
| 1761 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1762 | static struct kobject *cgroup_kobj; |
| 1763 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1764 | /** |
| 1765 | * cgroup_path - generate the path of a cgroup |
| 1766 | * @cgrp: the cgroup in question |
| 1767 | * @buf: the buffer to write the path into |
| 1768 | * @buflen: the length of the buffer |
| 1769 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1770 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1771 | * |
| 1772 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1773 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1774 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1775 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1776 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1777 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1778 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1779 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1780 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1781 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1782 | if (!cgrp->parent) { |
| 1783 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1784 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | return 0; |
| 1786 | } |
| 1787 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1788 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1789 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1790 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1791 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1792 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1793 | const char *name = cgroup_name(cgrp); |
| 1794 | int len; |
| 1795 | |
| 1796 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1797 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1798 | goto out; |
| 1799 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1800 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1801 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1802 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1803 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1804 | |
| 1805 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1806 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1807 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | out: |
| 1810 | rcu_read_unlock(); |
| 1811 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1813 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1814 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1815 | /** |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1816 | * 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] | 1817 | * @task: target task |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1818 | * @buf: the buffer to write the path into |
| 1819 | * @buflen: the length of the buffer |
| 1820 | * |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1821 | * Determine @task's cgroup on the first (the one with the lowest non-zero |
| 1822 | * hierarchy_id) cgroup hierarchy and copy its path into @buf. This |
| 1823 | * function grabs cgroup_mutex and shouldn't be used inside locks used by |
| 1824 | * cgroup controller callbacks. |
| 1825 | * |
| 1826 | * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short. |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1827 | */ |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1828 | 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] | 1829 | { |
| 1830 | struct cgroupfs_root *root; |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1831 | struct cgroup *cgrp; |
| 1832 | int hierarchy_id = 1, ret = 0; |
| 1833 | |
| 1834 | if (buflen < 2) |
| 1835 | return -ENAMETOOLONG; |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1836 | |
| 1837 | mutex_lock(&cgroup_mutex); |
| 1838 | |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1839 | root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); |
| 1840 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1841 | if (root) { |
| 1842 | cgrp = task_cgroup_from_root(task, root); |
| 1843 | ret = cgroup_path(cgrp, buf, buflen); |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1844 | } else { |
| 1845 | /* if no hierarchy exists, everyone is in "/" */ |
| 1846 | memcpy(buf, "/", 2); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1850 | return ret; |
| 1851 | } |
Tejun Heo | 913ffdb | 2013-07-11 16:34:48 -0700 | [diff] [blame] | 1852 | EXPORT_SYMBOL_GPL(task_cgroup_path); |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1853 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1854 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | * Control Group taskset |
| 1856 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1857 | struct task_and_cgroup { |
| 1858 | struct task_struct *task; |
| 1859 | struct cgroup *cgrp; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 1860 | struct css_set *cset; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1861 | }; |
| 1862 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1863 | struct cgroup_taskset { |
| 1864 | struct task_and_cgroup single; |
| 1865 | struct flex_array *tc_array; |
| 1866 | int tc_array_len; |
| 1867 | int idx; |
| 1868 | struct cgroup *cur_cgrp; |
| 1869 | }; |
| 1870 | |
| 1871 | /** |
| 1872 | * cgroup_taskset_first - reset taskset and return the first task |
| 1873 | * @tset: taskset of interest |
| 1874 | * |
| 1875 | * @tset iteration is initialized and the first task is returned. |
| 1876 | */ |
| 1877 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1878 | { |
| 1879 | if (tset->tc_array) { |
| 1880 | tset->idx = 0; |
| 1881 | return cgroup_taskset_next(tset); |
| 1882 | } else { |
| 1883 | tset->cur_cgrp = tset->single.cgrp; |
| 1884 | return tset->single.task; |
| 1885 | } |
| 1886 | } |
| 1887 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1888 | |
| 1889 | /** |
| 1890 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1891 | * @tset: taskset of interest |
| 1892 | * |
| 1893 | * Return the next task in @tset. Iteration must have been initialized |
| 1894 | * with cgroup_taskset_first(). |
| 1895 | */ |
| 1896 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1897 | { |
| 1898 | struct task_and_cgroup *tc; |
| 1899 | |
| 1900 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1901 | return NULL; |
| 1902 | |
| 1903 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1904 | tset->cur_cgrp = tc->cgrp; |
| 1905 | return tc->task; |
| 1906 | } |
| 1907 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1908 | |
| 1909 | /** |
| 1910 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1911 | * @tset: taskset of interest |
| 1912 | * |
| 1913 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1914 | * function must be preceded by either cgroup_taskset_first() or |
| 1915 | * cgroup_taskset_next(). |
| 1916 | */ |
| 1917 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1918 | { |
| 1919 | return tset->cur_cgrp; |
| 1920 | } |
| 1921 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1922 | |
| 1923 | /** |
| 1924 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1925 | * @tset: taskset of interest |
| 1926 | */ |
| 1927 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1928 | { |
| 1929 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1930 | } |
| 1931 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1932 | |
| 1933 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1934 | /* |
| 1935 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1936 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1937 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1938 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1939 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1940 | struct task_struct *tsk, |
| 1941 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1943 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | |
| 1945 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1946 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1947 | * setting such that we can't race against cgroup_exit() changing the |
| 1948 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1949 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1950 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 1951 | old_cset = task_css_set(tsk); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1952 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1954 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | task_unlock(tsk); |
| 1956 | |
| 1957 | /* Update the css_set linked lists if we're using them */ |
| 1958 | write_lock(&css_set_lock); |
| 1959 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1960 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1961 | write_unlock(&css_set_lock); |
| 1962 | |
| 1963 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1964 | * We just gained a reference on old_cset by taking it from the |
| 1965 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1966 | * we're safe to drop it here; it will be freed under RCU. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1967 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1968 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1969 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1972 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1973 | * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1974 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1975 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1976 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1977 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1978 | * Call holding cgroup_mutex and the group_rwsem of the leader. Will take |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1979 | * task_lock of @tsk or each thread in the threadgroup individually in turn. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1980 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1981 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1982 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1983 | { |
| 1984 | int retval, i, group_size; |
| 1985 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1986 | struct cgroupfs_root *root = cgrp->root; |
| 1987 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1988 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1989 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1990 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1991 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* |
| 1994 | * step 0: in order to do expensive, possibly blocking operations for |
| 1995 | * every thread, we cannot iterate the thread group list, since it needs |
| 1996 | * rcu or tasklist locked. instead, build an array of all threads in the |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1997 | * group - group_rwsem prevents new threads from appearing, and if |
| 1998 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1999 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2000 | if (threadgroup) |
| 2001 | group_size = get_nr_threads(tsk); |
| 2002 | else |
| 2003 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2004 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2005 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2006 | if (!group) |
| 2007 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2008 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2009 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2010 | if (retval) |
| 2011 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2012 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2013 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2014 | /* |
| 2015 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2016 | * already PF_EXITING could be freed from underneath us unless we |
| 2017 | * take an rcu_read_lock. |
| 2018 | */ |
| 2019 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2020 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2021 | struct task_and_cgroup ent; |
| 2022 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2023 | /* @tsk either already exited or can't exit until the end */ |
| 2024 | if (tsk->flags & PF_EXITING) |
| 2025 | continue; |
| 2026 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2027 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2028 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2029 | ent.task = tsk; |
| 2030 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2031 | /* nothing to do if this task is already in the cgroup */ |
| 2032 | if (ent.cgrp == cgrp) |
| 2033 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2034 | /* |
| 2035 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2036 | * earlier, but it's good form to communicate our expectations. |
| 2037 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2038 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2039 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2040 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2041 | |
| 2042 | if (!threadgroup) |
| 2043 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2045 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2046 | /* remember the number of threads in the array for later. */ |
| 2047 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2048 | tset.tc_array = group; |
| 2049 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2050 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2051 | /* methods shouldn't be called if no task is actually migrating */ |
| 2052 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2053 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2054 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2055 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2056 | /* |
| 2057 | * step 1: check that we can legitimately attach to the cgroup. |
| 2058 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2059 | for_each_root_subsys(root, ss) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2060 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 2061 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2062 | if (ss->can_attach) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2063 | retval = ss->can_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2064 | if (retval) { |
| 2065 | failed_ss = ss; |
| 2066 | goto out_cancel_attach; |
| 2067 | } |
| 2068 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2073 | * we use find_css_set, which allocates a new one if necessary. |
| 2074 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2075 | for (i = 0; i < group_size; i++) { |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2076 | struct css_set *old_cset; |
| 2077 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2078 | tc = flex_array_get(group, i); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 2079 | old_cset = task_css_set(tc->task); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2080 | tc->cset = find_css_set(old_cset, cgrp); |
| 2081 | if (!tc->cset) { |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2082 | retval = -ENOMEM; |
| 2083 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2088 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2089 | * proceed to move all tasks to the new cgroup. There are no |
| 2090 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2091 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2093 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2094 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2095 | } |
| 2096 | /* nothing is sensitive to fork() after this point. */ |
| 2097 | |
| 2098 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2099 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2100 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2101 | for_each_root_subsys(root, ss) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2102 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 2103 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2104 | if (ss->attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2105 | ss->attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | * step 5: success! and cleanup |
| 2110 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2112 | out_put_css_set_refs: |
| 2113 | if (retval) { |
| 2114 | for (i = 0; i < group_size; i++) { |
| 2115 | tc = flex_array_get(group, i); |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2116 | if (!tc->cset) |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2117 | break; |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2118 | put_css_set(tc->cset); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2119 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2120 | } |
| 2121 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2122 | if (retval) { |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 2123 | for_each_root_subsys(root, ss) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2124 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 2125 | |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2126 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2127 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2128 | if (ss->cancel_attach) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2129 | ss->cancel_attach(css, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2130 | } |
| 2131 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2132 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2133 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | return retval; |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * Find the task_struct of the task to attach by vpid and pass it along to the |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2139 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2140 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | */ |
| 2142 | static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2143 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2144 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2145 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2146 | int ret; |
| 2147 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2148 | if (!cgroup_lock_live_group(cgrp)) |
| 2149 | return -ENODEV; |
| 2150 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2151 | retry_find_task: |
| 2152 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2153 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2154 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2155 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2156 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2157 | ret= -ESRCH; |
| 2158 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2159 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2160 | /* |
| 2161 | * even if we're attaching all tasks in the thread group, we |
| 2162 | * only need to check permissions on one of them. |
| 2163 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2164 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2165 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2166 | !uid_eq(cred->euid, tcred->uid) && |
| 2167 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2168 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2169 | ret = -EACCES; |
| 2170 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2171 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2172 | } else |
| 2173 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2174 | |
| 2175 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2176 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2177 | |
| 2178 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2179 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2180 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2181 | * with no rt_runtime allocated. Just say no. |
| 2182 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2183 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2184 | ret = -EINVAL; |
| 2185 | rcu_read_unlock(); |
| 2186 | goto out_unlock_cgroup; |
| 2187 | } |
| 2188 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2189 | get_task_struct(tsk); |
| 2190 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2191 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2192 | threadgroup_lock(tsk); |
| 2193 | if (threadgroup) { |
| 2194 | if (!thread_group_leader(tsk)) { |
| 2195 | /* |
| 2196 | * a race with de_thread from another thread's exec() |
| 2197 | * may strip us of our leadership, if this happens, |
| 2198 | * there is no choice but to throw this task away and |
| 2199 | * try again; this is |
| 2200 | * "double-double-toil-and-trouble-check locking". |
| 2201 | */ |
| 2202 | threadgroup_unlock(tsk); |
| 2203 | put_task_struct(tsk); |
| 2204 | goto retry_find_task; |
| 2205 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2209 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2210 | threadgroup_unlock(tsk); |
| 2211 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2212 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2213 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2214 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2215 | return ret; |
| 2216 | } |
| 2217 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2218 | /** |
| 2219 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2220 | * @from: attach to all cgroups of a given task |
| 2221 | * @tsk: the task to be attached |
| 2222 | */ |
| 2223 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2224 | { |
| 2225 | struct cgroupfs_root *root; |
| 2226 | int retval = 0; |
| 2227 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2228 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2229 | for_each_active_root(root) { |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2230 | struct cgroup *from_cgrp = task_cgroup_from_root(from, root); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2231 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 2232 | retval = cgroup_attach_task(from_cgrp, tsk, false); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2233 | if (retval) |
| 2234 | break; |
| 2235 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2236 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2237 | |
| 2238 | return retval; |
| 2239 | } |
| 2240 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2241 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2242 | static int cgroup_tasks_write(struct cgroup_subsys_state *css, |
| 2243 | struct cftype *cft, u64 pid) |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2244 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2245 | return attach_task_by_pid(css->cgroup, pid, false); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2248 | static int cgroup_procs_write(struct cgroup_subsys_state *css, |
| 2249 | struct cftype *cft, u64 tgid) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2250 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2251 | return attach_task_by_pid(css->cgroup, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2254 | static int cgroup_release_agent_write(struct cgroup_subsys_state *css, |
| 2255 | struct cftype *cft, const char *buffer) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2256 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2257 | BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2258 | if (strlen(buffer) >= PATH_MAX) |
| 2259 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2260 | if (!cgroup_lock_live_group(css->cgroup)) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2261 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2262 | mutex_lock(&cgroup_root_mutex); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2263 | strcpy(css->cgroup->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2264 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2265 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2269 | static int cgroup_release_agent_show(struct cgroup_subsys_state *css, |
| 2270 | struct cftype *cft, struct seq_file *seq) |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2271 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2272 | struct cgroup *cgrp = css->cgroup; |
| 2273 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2274 | if (!cgroup_lock_live_group(cgrp)) |
| 2275 | return -ENODEV; |
| 2276 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2277 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2278 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2279 | return 0; |
| 2280 | } |
| 2281 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2282 | static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css, |
| 2283 | struct cftype *cft, struct seq_file *seq) |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2284 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2285 | seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2286 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2289 | /* return the css for the given cgroup file */ |
| 2290 | static struct cgroup_subsys_state *cgroup_file_css(struct cfent *cfe) |
| 2291 | { |
| 2292 | struct cftype *cft = cfe->type; |
| 2293 | struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent); |
| 2294 | |
| 2295 | if (cft->ss) |
| 2296 | return cgrp->subsys[cft->ss->subsys_id]; |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2297 | return &cgrp->dummy_css; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2298 | } |
| 2299 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2300 | /* A buffer size big enough for numbers or short strings */ |
| 2301 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2302 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2303 | static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css, |
| 2304 | struct cftype *cft, struct file *file, |
| 2305 | const char __user *userbuf, size_t nbytes, |
| 2306 | loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2307 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2308 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2309 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2310 | char *end; |
| 2311 | |
| 2312 | if (!nbytes) |
| 2313 | return -EINVAL; |
| 2314 | if (nbytes >= sizeof(buffer)) |
| 2315 | return -E2BIG; |
| 2316 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2317 | return -EFAULT; |
| 2318 | |
| 2319 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2320 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2321 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2322 | if (*end) |
| 2323 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2324 | retval = cft->write_u64(css, cft, val); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2325 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2326 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2327 | if (*end) |
| 2328 | return -EINVAL; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2329 | retval = cft->write_s64(css, cft, val); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2330 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2331 | if (!retval) |
| 2332 | retval = nbytes; |
| 2333 | return retval; |
| 2334 | } |
| 2335 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2336 | static ssize_t cgroup_write_string(struct cgroup_subsys_state *css, |
| 2337 | struct cftype *cft, struct file *file, |
| 2338 | const char __user *userbuf, size_t nbytes, |
| 2339 | loff_t *unused_ppos) |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2340 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2341 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2342 | int retval = 0; |
| 2343 | size_t max_bytes = cft->max_write_len; |
| 2344 | char *buffer = local_buffer; |
| 2345 | |
| 2346 | if (!max_bytes) |
| 2347 | max_bytes = sizeof(local_buffer) - 1; |
| 2348 | if (nbytes >= max_bytes) |
| 2349 | return -E2BIG; |
| 2350 | /* Allocate a dynamic buffer if we need one */ |
| 2351 | if (nbytes >= sizeof(local_buffer)) { |
| 2352 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2353 | if (buffer == NULL) |
| 2354 | return -ENOMEM; |
| 2355 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2356 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2357 | retval = -EFAULT; |
| 2358 | goto out; |
| 2359 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2360 | |
| 2361 | buffer[nbytes] = 0; /* nul-terminate */ |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2362 | retval = cft->write_string(css, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2363 | if (!retval) |
| 2364 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2365 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2366 | if (buffer != local_buffer) |
| 2367 | kfree(buffer); |
| 2368 | return retval; |
| 2369 | } |
| 2370 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2371 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2372 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2373 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2374 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2375 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2376 | struct cgroup_subsys_state *css = cgroup_file_css(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2377 | |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2378 | if (cft->write) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2379 | return cft->write(css, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2380 | if (cft->write_u64 || cft->write_s64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2381 | return cgroup_write_X64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2382 | if (cft->write_string) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2383 | return cgroup_write_string(css, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2384 | if (cft->trigger) { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2385 | int ret = cft->trigger(css, (unsigned int)cft->private); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2386 | return ret ? ret : nbytes; |
| 2387 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2388 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2391 | static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css, |
| 2392 | struct cftype *cft, struct file *file, |
| 2393 | char __user *buf, size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2394 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2395 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2396 | u64 val = cft->read_u64(css, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2397 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2398 | |
| 2399 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2400 | } |
| 2401 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2402 | static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css, |
| 2403 | struct cftype *cft, struct file *file, |
| 2404 | char __user *buf, size_t nbytes, loff_t *ppos) |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2405 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2406 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2407 | s64 val = cft->read_s64(css, cft); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2408 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2409 | |
| 2410 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2411 | } |
| 2412 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2413 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2414 | size_t nbytes, loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2415 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2416 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2417 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2418 | struct cgroup_subsys_state *css = cgroup_file_css(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2419 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2420 | if (cft->read) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2421 | return cft->read(css, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2422 | if (cft->read_u64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2423 | return cgroup_read_u64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2424 | if (cft->read_s64) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2425 | return cgroup_read_s64(css, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2426 | return -EINVAL; |
| 2427 | } |
| 2428 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2429 | /* |
| 2430 | * seqfile ops/methods for returning structured data. Currently just |
| 2431 | * supports string->u64 maps, but can be extended in future. |
| 2432 | */ |
| 2433 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2434 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2435 | { |
| 2436 | struct seq_file *sf = cb->state; |
| 2437 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2438 | } |
| 2439 | |
| 2440 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2441 | { |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2442 | struct cfent *cfe = m->private; |
| 2443 | struct cftype *cft = cfe->type; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2444 | struct cgroup_subsys_state *css = cgroup_file_css(cfe); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2445 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2446 | if (cft->read_map) { |
| 2447 | struct cgroup_map_cb cb = { |
| 2448 | .fill = cgroup_map_add, |
| 2449 | .state = m, |
| 2450 | }; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2451 | return cft->read_map(css, cft, &cb); |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2452 | } |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2453 | return cft->read_seq_string(css, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2454 | } |
| 2455 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2456 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2457 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2458 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2459 | .llseek = seq_lseek, |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2460 | .release = single_release, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2461 | }; |
| 2462 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2463 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2464 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2465 | struct cfent *cfe = __d_cfe(file->f_dentry); |
| 2466 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2467 | struct cgroup_subsys_state *css = cgroup_file_css(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2468 | int err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2469 | |
| 2470 | err = generic_file_open(inode, file); |
| 2471 | if (err) |
| 2472 | return err; |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2473 | |
| 2474 | /* |
| 2475 | * If the file belongs to a subsystem, pin the css. Will be |
| 2476 | * unpinned either on open failure or release. This ensures that |
| 2477 | * @css stays alive for all file operations. |
| 2478 | */ |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2479 | if (css->ss && !css_tryget(css)) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2480 | return -ENODEV; |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2481 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2482 | if (cft->read_map || cft->read_seq_string) { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2483 | file->f_op = &cgroup_seqfile_operations; |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2484 | err = single_open(file, cgroup_seqfile_show, cfe); |
| 2485 | } else if (cft->open) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2486 | err = cft->open(inode, file); |
Li Zefan | e0798ce | 2013-07-31 17:36:25 +0800 | [diff] [blame] | 2487 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2488 | |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2489 | if (css->ss && err) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2490 | css_put(css); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2491 | return err; |
| 2492 | } |
| 2493 | |
| 2494 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2495 | { |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2496 | struct cfent *cfe = __d_cfe(file->f_dentry); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2497 | struct cftype *cft = __d_cft(file->f_dentry); |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2498 | struct cgroup_subsys_state *css = cgroup_file_css(cfe); |
| 2499 | int ret = 0; |
| 2500 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2501 | if (cft->release) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2502 | ret = cft->release(inode, file); |
Tejun Heo | 67f4c36 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 2503 | if (css->ss) |
Tejun Heo | f7d5881 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2504 | css_put(css); |
| 2505 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2510 | */ |
| 2511 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2512 | struct inode *new_dir, struct dentry *new_dentry) |
| 2513 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2514 | int ret; |
| 2515 | struct cgroup_name *name, *old_name; |
| 2516 | struct cgroup *cgrp; |
| 2517 | |
| 2518 | /* |
| 2519 | * It's convinient to use parent dir's i_mutex to protected |
| 2520 | * cgrp->name. |
| 2521 | */ |
| 2522 | lockdep_assert_held(&old_dir->i_mutex); |
| 2523 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2524 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2525 | return -ENOTDIR; |
| 2526 | if (new_dentry->d_inode) |
| 2527 | return -EEXIST; |
| 2528 | if (old_dir != new_dir) |
| 2529 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2530 | |
| 2531 | cgrp = __d_cgrp(old_dentry); |
| 2532 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2533 | /* |
| 2534 | * This isn't a proper migration and its usefulness is very |
| 2535 | * limited. Disallow if sane_behavior. |
| 2536 | */ |
| 2537 | if (cgroup_sane_behavior(cgrp)) |
| 2538 | return -EPERM; |
| 2539 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2540 | name = cgroup_alloc_name(new_dentry); |
| 2541 | if (!name) |
| 2542 | return -ENOMEM; |
| 2543 | |
| 2544 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2545 | if (ret) { |
| 2546 | kfree(name); |
| 2547 | return ret; |
| 2548 | } |
| 2549 | |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 2550 | old_name = rcu_dereference_protected(cgrp->name, true); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2551 | rcu_assign_pointer(cgrp->name, name); |
| 2552 | |
| 2553 | kfree_rcu(old_name, rcu_head); |
| 2554 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2557 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2558 | { |
| 2559 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2560 | return &__d_cgrp(dentry)->xattrs; |
| 2561 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2562 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | static inline int xattr_enabled(struct dentry *dentry) |
| 2566 | { |
| 2567 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2568 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | static bool is_valid_xattr(const char *name) |
| 2572 | { |
| 2573 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2574 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2575 | return true; |
| 2576 | return false; |
| 2577 | } |
| 2578 | |
| 2579 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2580 | const void *val, size_t size, int flags) |
| 2581 | { |
| 2582 | if (!xattr_enabled(dentry)) |
| 2583 | return -EOPNOTSUPP; |
| 2584 | if (!is_valid_xattr(name)) |
| 2585 | return -EINVAL; |
| 2586 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2587 | } |
| 2588 | |
| 2589 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2590 | { |
| 2591 | if (!xattr_enabled(dentry)) |
| 2592 | return -EOPNOTSUPP; |
| 2593 | if (!is_valid_xattr(name)) |
| 2594 | return -EINVAL; |
| 2595 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2596 | } |
| 2597 | |
| 2598 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2599 | void *buf, size_t size) |
| 2600 | { |
| 2601 | if (!xattr_enabled(dentry)) |
| 2602 | return -EOPNOTSUPP; |
| 2603 | if (!is_valid_xattr(name)) |
| 2604 | return -EINVAL; |
| 2605 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2606 | } |
| 2607 | |
| 2608 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2609 | { |
| 2610 | if (!xattr_enabled(dentry)) |
| 2611 | return -EOPNOTSUPP; |
| 2612 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2613 | } |
| 2614 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2615 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2616 | .read = cgroup_file_read, |
| 2617 | .write = cgroup_file_write, |
| 2618 | .llseek = generic_file_llseek, |
| 2619 | .open = cgroup_file_open, |
| 2620 | .release = cgroup_file_release, |
| 2621 | }; |
| 2622 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2623 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2624 | .setxattr = cgroup_setxattr, |
| 2625 | .getxattr = cgroup_getxattr, |
| 2626 | .listxattr = cgroup_listxattr, |
| 2627 | .removexattr = cgroup_removexattr, |
| 2628 | }; |
| 2629 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2630 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2631 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2632 | .mkdir = cgroup_mkdir, |
| 2633 | .rmdir = cgroup_rmdir, |
| 2634 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2635 | .setxattr = cgroup_setxattr, |
| 2636 | .getxattr = cgroup_getxattr, |
| 2637 | .listxattr = cgroup_listxattr, |
| 2638 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2639 | }; |
| 2640 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2641 | 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] | 2642 | { |
| 2643 | if (dentry->d_name.len > NAME_MAX) |
| 2644 | return ERR_PTR(-ENAMETOOLONG); |
| 2645 | d_add(dentry, NULL); |
| 2646 | return NULL; |
| 2647 | } |
| 2648 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2649 | /* |
| 2650 | * Check if a file is a control file |
| 2651 | */ |
| 2652 | static inline struct cftype *__file_cft(struct file *file) |
| 2653 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2654 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2655 | return ERR_PTR(-EINVAL); |
| 2656 | return __d_cft(file->f_dentry); |
| 2657 | } |
| 2658 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2659 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2660 | struct super_block *sb) |
| 2661 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2662 | struct inode *inode; |
| 2663 | |
| 2664 | if (!dentry) |
| 2665 | return -ENOENT; |
| 2666 | if (dentry->d_inode) |
| 2667 | return -EEXIST; |
| 2668 | |
| 2669 | inode = cgroup_new_inode(mode, sb); |
| 2670 | if (!inode) |
| 2671 | return -ENOMEM; |
| 2672 | |
| 2673 | if (S_ISDIR(mode)) { |
| 2674 | inode->i_op = &cgroup_dir_inode_operations; |
| 2675 | inode->i_fop = &simple_dir_operations; |
| 2676 | |
| 2677 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2678 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2679 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2680 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2681 | /* |
| 2682 | * Control reaches here with cgroup_mutex held. |
| 2683 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2684 | * want to populate it immediately without releasing |
| 2685 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2686 | * yet, trylock will always succeed without affecting |
| 2687 | * lockdep checks. |
| 2688 | */ |
| 2689 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2690 | } else if (S_ISREG(mode)) { |
| 2691 | inode->i_size = 0; |
| 2692 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2693 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2694 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2695 | d_instantiate(dentry, inode); |
| 2696 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2700 | /** |
| 2701 | * cgroup_file_mode - deduce file mode of a control file |
| 2702 | * @cft: the control file in question |
| 2703 | * |
| 2704 | * returns cft->mode if ->mode is not 0 |
| 2705 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2706 | * returns S_IRUGO if it has only a read handler |
| 2707 | * returns S_IWUSR if it has only a write hander |
| 2708 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2709 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2710 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2711 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2712 | |
| 2713 | if (cft->mode) |
| 2714 | return cft->mode; |
| 2715 | |
| 2716 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2717 | cft->read_map || cft->read_seq_string) |
| 2718 | mode |= S_IRUGO; |
| 2719 | |
| 2720 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2721 | cft->write_string || cft->trigger) |
| 2722 | mode |= S_IWUSR; |
| 2723 | |
| 2724 | return mode; |
| 2725 | } |
| 2726 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2727 | static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2728 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2729 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2730 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2731 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2732 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2733 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2734 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2735 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2736 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2737 | if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
| 2738 | strcpy(name, cft->ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2739 | strcat(name, "."); |
| 2740 | } |
| 2741 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2742 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2743 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2744 | |
| 2745 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2746 | if (!cfe) |
| 2747 | return -ENOMEM; |
| 2748 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2749 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2750 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2751 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2752 | goto out; |
| 2753 | } |
| 2754 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2755 | cfe->type = (void *)cft; |
| 2756 | cfe->dentry = dentry; |
| 2757 | dentry->d_fsdata = cfe; |
| 2758 | simple_xattrs_init(&cfe->xattrs); |
| 2759 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2760 | mode = cgroup_file_mode(cft); |
| 2761 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2762 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2763 | list_add_tail(&cfe->node, &parent->files); |
| 2764 | cfe = NULL; |
| 2765 | } |
| 2766 | dput(dentry); |
| 2767 | out: |
| 2768 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2769 | return error; |
| 2770 | } |
| 2771 | |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2772 | /** |
| 2773 | * cgroup_addrm_files - add or remove files to a cgroup directory |
| 2774 | * @cgrp: the target cgroup |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2775 | * @cfts: array of cftypes to be added |
| 2776 | * @is_add: whether to add or remove |
| 2777 | * |
| 2778 | * Depending on @is_add, add or remove files defined by @cfts on @cgrp. |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2779 | * For removals, this function never fails. If addition fails, this |
| 2780 | * function doesn't remove files already added. The caller is responsible |
| 2781 | * for cleaning up. |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2782 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2783 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[], |
| 2784 | bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2785 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2786 | struct cftype *cft; |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2787 | int ret; |
| 2788 | |
| 2789 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 2790 | lockdep_assert_held(&cgroup_mutex); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2791 | |
| 2792 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2793 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2794 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2795 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2796 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2797 | continue; |
| 2798 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2799 | continue; |
| 2800 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2801 | if (is_add) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2802 | ret = cgroup_add_file(cgrp, cft); |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2803 | if (ret) { |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2804 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2805 | cft->name, ret); |
| 2806 | return ret; |
| 2807 | } |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2808 | } else { |
| 2809 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2810 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2811 | } |
Tejun Heo | b1f28d3 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 2812 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2815 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2816 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2817 | { |
| 2818 | /* |
| 2819 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2820 | * the existing cgroups under cgroup_mutex and create files. |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2821 | * Instead, we use css_for_each_descendant_pre() and drop RCU read |
| 2822 | * lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2823 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2824 | mutex_lock(&cgroup_mutex); |
| 2825 | } |
| 2826 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2827 | static int cgroup_cfts_commit(struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2828 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2829 | { |
| 2830 | LIST_HEAD(pending); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2831 | struct cgroup_subsys *ss = cfts[0].ss; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2832 | struct cgroup *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2833 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2834 | struct dentry *prev = NULL; |
| 2835 | struct inode *inode; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2836 | struct cgroup_subsys_state *css; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2837 | u64 update_before; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2838 | int ret = 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2839 | |
| 2840 | /* %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] | 2841 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2842 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2843 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2844 | return 0; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2847 | /* |
| 2848 | * All cgroups which are created after we drop cgroup_mutex will |
| 2849 | * 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] | 2850 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2851 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2852 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2854 | mutex_unlock(&cgroup_mutex); |
| 2855 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2856 | /* @root always needs to be updated */ |
| 2857 | inode = root->dentry->d_inode; |
| 2858 | mutex_lock(&inode->i_mutex); |
| 2859 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2860 | ret = cgroup_addrm_files(root, cfts, is_add); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2861 | mutex_unlock(&cgroup_mutex); |
| 2862 | mutex_unlock(&inode->i_mutex); |
| 2863 | |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2864 | if (ret) |
| 2865 | goto out_deact; |
| 2866 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2867 | /* add/rm files for all cgroups created before */ |
| 2868 | rcu_read_lock(); |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 2869 | css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) { |
| 2870 | struct cgroup *cgrp = css->cgroup; |
| 2871 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2872 | if (cgroup_is_dead(cgrp)) |
| 2873 | continue; |
| 2874 | |
| 2875 | inode = cgrp->dentry->d_inode; |
| 2876 | dget(cgrp->dentry); |
| 2877 | rcu_read_unlock(); |
| 2878 | |
| 2879 | dput(prev); |
| 2880 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2881 | |
| 2882 | mutex_lock(&inode->i_mutex); |
| 2883 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2884 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2885 | ret = cgroup_addrm_files(cgrp, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2886 | mutex_unlock(&cgroup_mutex); |
| 2887 | mutex_unlock(&inode->i_mutex); |
| 2888 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2889 | rcu_read_lock(); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2890 | if (ret) |
| 2891 | break; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2892 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2893 | rcu_read_unlock(); |
| 2894 | dput(prev); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2895 | out_deact: |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2896 | deactivate_super(sb); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2897 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2902 | * @ss: target cgroup subsystem |
| 2903 | * @cfts: zero-length name terminated array of cftypes |
| 2904 | * |
| 2905 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2906 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2907 | * have them too. This function can be called anytime whether @ss is |
| 2908 | * attached or not. |
| 2909 | * |
| 2910 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2911 | * function currently returns 0 as long as @cfts registration is successful |
| 2912 | * even if some file creation attempts on existing cgroups fail. |
| 2913 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2914 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2915 | { |
| 2916 | struct cftype_set *set; |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2917 | struct cftype *cft; |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2918 | int ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2919 | |
| 2920 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2921 | if (!set) |
| 2922 | return -ENOMEM; |
| 2923 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2924 | for (cft = cfts; cft->name[0] != '\0'; cft++) |
| 2925 | cft->ss = ss; |
| 2926 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2927 | cgroup_cfts_prepare(); |
| 2928 | set->cfts = cfts; |
| 2929 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2930 | ret = cgroup_cfts_commit(cfts, true); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2931 | if (ret) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2932 | cgroup_rm_cftypes(cfts); |
Tejun Heo | 9ccece8 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 2933 | return ret; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2934 | } |
| 2935 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2936 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2937 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2938 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2939 | * @cfts: zero-length name terminated array of cftypes |
| 2940 | * |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2941 | * Unregister @cfts. Files described by @cfts are removed from all |
| 2942 | * existing cgroups and all future cgroups won't have them either. This |
| 2943 | * function can be called anytime whether @cfts' subsys is attached or not. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2944 | * |
| 2945 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2946 | * registered. |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2947 | */ |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2948 | int cgroup_rm_cftypes(struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2949 | { |
| 2950 | struct cftype_set *set; |
| 2951 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2952 | if (!cfts || !cfts[0].ss) |
| 2953 | return -ENOENT; |
| 2954 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2955 | cgroup_cfts_prepare(); |
| 2956 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2957 | list_for_each_entry(set, &cfts[0].ss->cftsets, node) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2958 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2959 | list_del(&set->node); |
| 2960 | kfree(set); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2961 | cgroup_cfts_commit(cfts, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2962 | return 0; |
| 2963 | } |
| 2964 | } |
| 2965 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 2966 | cgroup_cfts_commit(NULL, false); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2967 | return -ENOENT; |
| 2968 | } |
| 2969 | |
| 2970 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2971 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2972 | * @cgrp: the cgroup in question |
| 2973 | * |
| 2974 | * Return the number of tasks in the cgroup. |
| 2975 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2976 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2977 | { |
| 2978 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2979 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2980 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2981 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2982 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2983 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2984 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2985 | return count; |
| 2986 | } |
| 2987 | |
| 2988 | /* |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 2989 | * To reduce the fork() overhead for systems that are not actually using |
| 2990 | * their cgroups capability, we don't maintain the lists running through |
| 2991 | * each css_set to its tasks until we see the list actually used - in other |
| 2992 | * words after the first call to cgroup_task_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2993 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2994 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2995 | { |
| 2996 | struct task_struct *p, *g; |
| 2997 | write_lock(&css_set_lock); |
| 2998 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2999 | /* |
| 3000 | * We need tasklist_lock because RCU is not safe against |
| 3001 | * while_each_thread(). Besides, a forking task that has passed |
| 3002 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 3003 | * is not guaranteed to have its child immediately visible in the |
| 3004 | * tasklist if we walk through it with RCU. |
| 3005 | */ |
| 3006 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3007 | do_each_thread(g, p) { |
| 3008 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 3009 | /* |
| 3010 | * We should check if the process is exiting, otherwise |
| 3011 | * it will race with cgroup_exit() in that the list |
| 3012 | * entry won't be deleted though the process has exited. |
| 3013 | */ |
| 3014 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 3015 | list_add(&p->cg_list, &task_css_set(p)->tasks); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3016 | task_unlock(p); |
| 3017 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 3018 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3019 | write_unlock(&css_set_lock); |
| 3020 | } |
| 3021 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3022 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3023 | * css_next_child - find the next child of a given css |
| 3024 | * @pos_css: the current position (%NULL to initiate traversal) |
| 3025 | * @parent_css: css whose children to walk |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3026 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3027 | * This function returns the next child of @parent_css and should be called |
| 3028 | * under RCU read lock. The only requirement is that @parent_css and |
| 3029 | * @pos_css are accessible. The next sibling is guaranteed to be returned |
| 3030 | * regardless of their states. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3031 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3032 | struct cgroup_subsys_state * |
| 3033 | css_next_child(struct cgroup_subsys_state *pos_css, |
| 3034 | struct cgroup_subsys_state *parent_css) |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3035 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3036 | struct cgroup *pos = pos_css ? pos_css->cgroup : NULL; |
| 3037 | struct cgroup *cgrp = parent_css->cgroup; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3038 | struct cgroup *next; |
| 3039 | |
| 3040 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3041 | |
| 3042 | /* |
| 3043 | * @pos could already have been removed. Once a cgroup is removed, |
| 3044 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3045 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3046 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3047 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3048 | * finished its grace period even if it's already removed, and thus |
| 3049 | * safe to dereference from this RCU critical section. If |
| 3050 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3051 | * to be visible as %true here. |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3052 | * |
| 3053 | * If @pos is dead, its next pointer can't be dereferenced; |
| 3054 | * however, as each cgroup is given a monotonically increasing |
| 3055 | * unique serial number and always appended to the sibling list, |
| 3056 | * the next one can be found by walking the parent's children until |
| 3057 | * we see a cgroup with higher serial number than @pos's. While |
| 3058 | * this path can be slower, it's taken only when either the current |
| 3059 | * cgroup is removed or iteration and removal race. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3060 | */ |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3061 | if (!pos) { |
| 3062 | next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling); |
| 3063 | } else if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3064 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
Tejun Heo | 3b287a5 | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3065 | } else { |
| 3066 | list_for_each_entry_rcu(next, &cgrp->children, sibling) |
| 3067 | if (next->serial_nr > pos->serial_nr) |
| 3068 | break; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3069 | } |
| 3070 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3071 | if (&next->sibling == &cgrp->children) |
| 3072 | return NULL; |
| 3073 | |
| 3074 | if (parent_css->ss) |
| 3075 | return cgroup_css(next, parent_css->ss->subsys_id); |
| 3076 | else |
| 3077 | return &next->dummy_css; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3078 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3079 | EXPORT_SYMBOL_GPL(css_next_child); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3080 | |
| 3081 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3082 | * css_next_descendant_pre - find the next descendant for pre-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3083 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3084 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3085 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3086 | * To be used by css_for_each_descendant_pre(). Find the next descendant |
| 3087 | * to visit for pre-order traversal of @root's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3088 | * |
| 3089 | * While this function requires RCU read locking, it doesn't require the |
| 3090 | * whole traversal to be contained in a single RCU critical section. This |
| 3091 | * function will return the correct next descendant as long as both @pos |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3092 | * and @root are accessible and @pos is a descendant of @root. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3093 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3094 | struct cgroup_subsys_state * |
| 3095 | css_next_descendant_pre(struct cgroup_subsys_state *pos, |
| 3096 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3097 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3098 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3099 | |
| 3100 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3101 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3102 | /* if first iteration, pretend we just visited @root */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3103 | if (!pos) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3104 | pos = root; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3105 | |
| 3106 | /* visit the first child if exists */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3107 | next = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3108 | if (next) |
| 3109 | return next; |
| 3110 | |
| 3111 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3112 | while (pos != root) { |
| 3113 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3114 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3115 | return next; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3116 | pos = css_parent(pos); |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3117 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3118 | |
| 3119 | return NULL; |
| 3120 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3121 | EXPORT_SYMBOL_GPL(css_next_descendant_pre); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3122 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3123 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3124 | * css_rightmost_descendant - return the rightmost descendant of a css |
| 3125 | * @pos: css of interest |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3126 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3127 | * Return the rightmost descendant of @pos. If there's no descendant, @pos |
| 3128 | * is returned. This can be used during pre-order traversal to skip |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3129 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3130 | * |
| 3131 | * While this function requires RCU read locking, it doesn't require the |
| 3132 | * whole traversal to be contained in a single RCU critical section. This |
| 3133 | * function will return the correct rightmost descendant as long as @pos is |
| 3134 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3135 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3136 | struct cgroup_subsys_state * |
| 3137 | css_rightmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3138 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3139 | struct cgroup_subsys_state *last, *tmp; |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3140 | |
| 3141 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3142 | |
| 3143 | do { |
| 3144 | last = pos; |
| 3145 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3146 | pos = NULL; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3147 | css_for_each_child(tmp, last) |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3148 | pos = tmp; |
| 3149 | } while (pos); |
| 3150 | |
| 3151 | return last; |
| 3152 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3153 | EXPORT_SYMBOL_GPL(css_rightmost_descendant); |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3154 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3155 | static struct cgroup_subsys_state * |
| 3156 | css_leftmost_descendant(struct cgroup_subsys_state *pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3157 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3158 | struct cgroup_subsys_state *last; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3159 | |
| 3160 | do { |
| 3161 | last = pos; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3162 | pos = css_next_child(NULL, pos); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3163 | } while (pos); |
| 3164 | |
| 3165 | return last; |
| 3166 | } |
| 3167 | |
| 3168 | /** |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3169 | * css_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3170 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3171 | * @root: css whose descendants to walk |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3172 | * |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3173 | * To be used by css_for_each_descendant_post(). Find the next descendant |
| 3174 | * to visit for post-order traversal of @root's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3175 | * |
| 3176 | * While this function requires RCU read locking, it doesn't require the |
| 3177 | * whole traversal to be contained in a single RCU critical section. This |
| 3178 | * function will return the correct next descendant as long as both @pos |
| 3179 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3180 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3181 | struct cgroup_subsys_state * |
| 3182 | css_next_descendant_post(struct cgroup_subsys_state *pos, |
| 3183 | struct cgroup_subsys_state *root) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3184 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3185 | struct cgroup_subsys_state *next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3186 | |
| 3187 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3188 | |
| 3189 | /* if first iteration, visit the leftmost descendant */ |
| 3190 | if (!pos) { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3191 | next = css_leftmost_descendant(root); |
| 3192 | return next != root ? next : NULL; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3196 | next = css_next_child(pos, css_parent(pos)); |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3197 | if (next) |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3198 | return css_leftmost_descendant(next); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3199 | |
| 3200 | /* no sibling left, visit parent */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3201 | next = css_parent(pos); |
| 3202 | return next != root ? next : NULL; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3203 | } |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 3204 | EXPORT_SYMBOL_GPL(css_next_descendant_post); |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3205 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3206 | /** |
| 3207 | * cgroup_advance_task_iter - advance a task itererator to the next css_set |
| 3208 | * @cgrp: the cgroup to walk tasks of |
| 3209 | * @it: the iterator to advance |
| 3210 | * |
| 3211 | * Advance @it to the next css_set to walk. |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3212 | */ |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3213 | static void cgroup_advance_task_iter(struct cgroup *cgrp, |
| 3214 | struct cgroup_task_iter *it) |
Tejun Heo | d515876 | 2013-08-08 20:11:26 -0400 | [diff] [blame] | 3215 | { |
| 3216 | struct list_head *l = it->cset_link; |
| 3217 | struct cgrp_cset_link *link; |
| 3218 | struct css_set *cset; |
| 3219 | |
| 3220 | /* Advance to the next non-empty css_set */ |
| 3221 | do { |
| 3222 | l = l->next; |
| 3223 | if (l == &cgrp->cset_links) { |
| 3224 | it->cset_link = NULL; |
| 3225 | return; |
| 3226 | } |
| 3227 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 3228 | cset = link->cset; |
| 3229 | } while (list_empty(&cset->tasks)); |
| 3230 | it->cset_link = l; |
| 3231 | it->task = cset->tasks.next; |
| 3232 | } |
| 3233 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3234 | /** |
| 3235 | * cgroup_task_iter_start - initiate task iteration |
| 3236 | * @cgrp: the cgroup to walk tasks of |
| 3237 | * @it: the task iterator to use |
| 3238 | * |
| 3239 | * Initiate iteration through the tasks of @cgrp. The caller can call |
| 3240 | * cgroup_task_iter_next() to walk through the tasks until the function |
| 3241 | * returns NULL. On completion of iteration, cgroup_task_iter_end() must |
| 3242 | * be called. |
| 3243 | * |
| 3244 | * Note that this function acquires a lock which is released when the |
| 3245 | * iteration finishes. The caller can't sleep while iteration is in |
| 3246 | * progress. |
| 3247 | */ |
| 3248 | void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3249 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3250 | { |
| 3251 | /* |
| 3252 | * The first time anyone tries to iterate across a cgroup, |
| 3253 | * we need to enable the list linking each css_set to its |
| 3254 | * tasks, and fix up all existing tasks. |
| 3255 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3256 | if (!use_task_css_set_links) |
| 3257 | cgroup_enable_task_cg_lists(); |
| 3258 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3259 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3260 | it->cset_link = &cgrp->cset_links; |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3261 | cgroup_advance_task_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3264 | /** |
| 3265 | * cgroup_task_iter_next - return the next task for the iterator |
| 3266 | * @cgrp: the cgroup to walk tasks of |
| 3267 | * @it: the task iterator being iterated |
| 3268 | * |
| 3269 | * The "next" function for task iteration. @it should have been |
| 3270 | * initialized via cgroup_task_iter_start(). Returns NULL when the |
| 3271 | * iteration reaches the end. |
| 3272 | */ |
| 3273 | struct task_struct *cgroup_task_iter_next(struct cgroup *cgrp, |
| 3274 | struct cgroup_task_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3275 | { |
| 3276 | struct task_struct *res; |
| 3277 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3278 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3279 | |
| 3280 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3281 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3282 | return NULL; |
| 3283 | res = list_entry(l, struct task_struct, cg_list); |
| 3284 | /* Advance iterator to find next entry */ |
| 3285 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3286 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3287 | if (l == &link->cset->tasks) { |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3288 | /* |
| 3289 | * We reached the end of this task list - move on to the |
| 3290 | * next cgrp_cset_link. |
| 3291 | */ |
| 3292 | cgroup_advance_task_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3293 | } else { |
| 3294 | it->task = l; |
| 3295 | } |
| 3296 | return res; |
| 3297 | } |
| 3298 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3299 | /** |
| 3300 | * cgroup_task_iter_end - finish task iteration |
| 3301 | * @cgrp: the cgroup to walk tasks of |
| 3302 | * @it: the task iterator to finish |
| 3303 | * |
| 3304 | * Finish task iteration started by cgroup_task_iter_start(). |
| 3305 | */ |
| 3306 | void cgroup_task_iter_end(struct cgroup *cgrp, struct cgroup_task_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3307 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3308 | { |
| 3309 | read_unlock(&css_set_lock); |
| 3310 | } |
| 3311 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3312 | static inline int started_after_time(struct task_struct *t1, |
| 3313 | struct timespec *time, |
| 3314 | struct task_struct *t2) |
| 3315 | { |
| 3316 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3317 | if (start_diff > 0) { |
| 3318 | return 1; |
| 3319 | } else if (start_diff < 0) { |
| 3320 | return 0; |
| 3321 | } else { |
| 3322 | /* |
| 3323 | * Arbitrarily, if two processes started at the same |
| 3324 | * time, we'll say that the lower pointer value |
| 3325 | * started first. Note that t2 may have exited by now |
| 3326 | * so this may not be a valid pointer any longer, but |
| 3327 | * that's fine - it still serves to distinguish |
| 3328 | * between two tasks started (effectively) simultaneously. |
| 3329 | */ |
| 3330 | return t1 > t2; |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | /* |
| 3335 | * This function is a callback from heap_insert() and is used to order |
| 3336 | * the heap. |
| 3337 | * In this case we order the heap in descending task start time. |
| 3338 | */ |
| 3339 | static inline int started_after(void *p1, void *p2) |
| 3340 | { |
| 3341 | struct task_struct *t1 = p1; |
| 3342 | struct task_struct *t2 = p2; |
| 3343 | return started_after_time(t1, &t2->start_time, t2); |
| 3344 | } |
| 3345 | |
| 3346 | /** |
| 3347 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3348 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3349 | * |
| 3350 | * Arguments include pointers to callback functions test_task() and |
| 3351 | * process_task(). |
| 3352 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3353 | * and if it returns true, call process_task() for it also. |
| 3354 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3355 | * Effectively duplicates cgroup_task_iter_{start,next,end}() |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3356 | * but does not lock css_set_lock for the call to process_task(). |
| 3357 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3358 | * creation. |
| 3359 | * It is guaranteed that process_task() will act on every task that |
| 3360 | * is a member of the cgroup for the duration of this call. This |
| 3361 | * function may or may not call process_task() for tasks that exit |
| 3362 | * or move to a different cgroup during the call, or are forked or |
| 3363 | * move into the cgroup during the call. |
| 3364 | * |
| 3365 | * Note that test_task() may be called with locks held, and may in some |
| 3366 | * situations be called multiple times for the same task, so it should |
| 3367 | * be cheap. |
| 3368 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3369 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3370 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3371 | * may cause this function to fail). |
| 3372 | */ |
| 3373 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3374 | { |
| 3375 | int retval, i; |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3376 | struct cgroup_task_iter it; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3377 | struct task_struct *p, *dropped; |
| 3378 | /* Never dereference latest_task, since it's not refcounted */ |
| 3379 | struct task_struct *latest_task = NULL; |
| 3380 | struct ptr_heap tmp_heap; |
| 3381 | struct ptr_heap *heap; |
| 3382 | struct timespec latest_time = { 0, 0 }; |
| 3383 | |
| 3384 | if (scan->heap) { |
| 3385 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3386 | heap = scan->heap; |
| 3387 | heap->gt = &started_after; |
| 3388 | } else { |
| 3389 | /* We need to allocate our own heap memory */ |
| 3390 | heap = &tmp_heap; |
| 3391 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3392 | if (retval) |
| 3393 | /* cannot allocate the heap */ |
| 3394 | return retval; |
| 3395 | } |
| 3396 | |
| 3397 | again: |
| 3398 | /* |
| 3399 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3400 | * to determine which are of interest, and using the scanner's |
| 3401 | * "process_task" callback to process any of them that need an update. |
| 3402 | * Since we don't want to hold any locks during the task updates, |
| 3403 | * gather tasks to be processed in a heap structure. |
| 3404 | * The heap is sorted by descending task start time. |
| 3405 | * If the statically-sized heap fills up, we overflow tasks that |
| 3406 | * started later, and in future iterations only consider tasks that |
| 3407 | * started after the latest task in the previous pass. This |
| 3408 | * guarantees forward progress and that we don't miss any tasks. |
| 3409 | */ |
| 3410 | heap->size = 0; |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3411 | cgroup_task_iter_start(scan->cgrp, &it); |
| 3412 | while ((p = cgroup_task_iter_next(scan->cgrp, &it))) { |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3413 | /* |
| 3414 | * Only affect tasks that qualify per the caller's callback, |
| 3415 | * if he provided one |
| 3416 | */ |
| 3417 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3418 | continue; |
| 3419 | /* |
| 3420 | * Only process tasks that started after the last task |
| 3421 | * we processed |
| 3422 | */ |
| 3423 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3424 | continue; |
| 3425 | dropped = heap_insert(heap, p); |
| 3426 | if (dropped == NULL) { |
| 3427 | /* |
| 3428 | * The new task was inserted; the heap wasn't |
| 3429 | * previously full |
| 3430 | */ |
| 3431 | get_task_struct(p); |
| 3432 | } else if (dropped != p) { |
| 3433 | /* |
| 3434 | * The new task was inserted, and pushed out a |
| 3435 | * different task |
| 3436 | */ |
| 3437 | get_task_struct(p); |
| 3438 | put_task_struct(dropped); |
| 3439 | } |
| 3440 | /* |
| 3441 | * Else the new task was newer than anything already in |
| 3442 | * the heap and wasn't inserted |
| 3443 | */ |
| 3444 | } |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3445 | cgroup_task_iter_end(scan->cgrp, &it); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3446 | |
| 3447 | if (heap->size) { |
| 3448 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3449 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3450 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3451 | latest_time = q->start_time; |
| 3452 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3453 | } |
| 3454 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3455 | scan->process_task(q, scan); |
| 3456 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3457 | } |
| 3458 | /* |
| 3459 | * If we had to process any tasks at all, scan again |
| 3460 | * in case some of them were in the middle of forking |
| 3461 | * children that didn't get processed. |
| 3462 | * Not the most efficient way to do it, but it avoids |
| 3463 | * having to take callback_mutex in the fork path |
| 3464 | */ |
| 3465 | goto again; |
| 3466 | } |
| 3467 | if (heap == &tmp_heap) |
| 3468 | heap_free(&tmp_heap); |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3472 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3473 | struct cgroup_scanner *scan) |
| 3474 | { |
| 3475 | struct cgroup *new_cgroup = scan->data; |
| 3476 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3477 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3478 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3479 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | /** |
| 3483 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3484 | * @to: cgroup to which the tasks will be moved |
| 3485 | * @from: cgroup in which the tasks currently reside |
| 3486 | */ |
| 3487 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3488 | { |
| 3489 | struct cgroup_scanner scan; |
| 3490 | |
Li Zefan | 6f4b7e6 | 2013-07-31 16:18:36 +0800 | [diff] [blame] | 3491 | scan.cgrp = from; |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3492 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3493 | scan.process_task = cgroup_transfer_one_task; |
| 3494 | scan.heap = NULL; |
| 3495 | scan.data = to; |
| 3496 | |
| 3497 | return cgroup_scan_tasks(&scan); |
| 3498 | } |
| 3499 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3500 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3501 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3502 | * |
| 3503 | * Reading this file can return large amounts of data if a cgroup has |
| 3504 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3505 | * but we cannot guarantee that the information we produce is correct |
| 3506 | * unless we produce it entirely atomically. |
| 3507 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3508 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3509 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3510 | /* which pidlist file are we talking about? */ |
| 3511 | enum cgroup_filetype { |
| 3512 | CGROUP_FILE_PROCS, |
| 3513 | CGROUP_FILE_TASKS, |
| 3514 | }; |
| 3515 | |
| 3516 | /* |
| 3517 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3518 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3519 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3520 | * to the cgroup. |
| 3521 | */ |
| 3522 | struct cgroup_pidlist { |
| 3523 | /* |
| 3524 | * used to find which pidlist is wanted. doesn't change as long as |
| 3525 | * this particular list stays in the list. |
| 3526 | */ |
| 3527 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3528 | /* array of xids */ |
| 3529 | pid_t *list; |
| 3530 | /* how many elements the above list has */ |
| 3531 | int length; |
| 3532 | /* how many files are using the current array */ |
| 3533 | int use_count; |
| 3534 | /* each of these stored in a list by its cgroup */ |
| 3535 | struct list_head links; |
| 3536 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3537 | struct cgroup *owner; |
| 3538 | /* protects the other fields */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3539 | struct rw_semaphore rwsem; |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3540 | }; |
| 3541 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3542 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3543 | * The following two functions "fix" the issue where there are more pids |
| 3544 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3545 | * TODO: replace with a kernel-wide solution to this problem |
| 3546 | */ |
| 3547 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3548 | static void *pidlist_allocate(int count) |
| 3549 | { |
| 3550 | if (PIDLIST_TOO_LARGE(count)) |
| 3551 | return vmalloc(count * sizeof(pid_t)); |
| 3552 | else |
| 3553 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3554 | } |
| 3555 | static void pidlist_free(void *p) |
| 3556 | { |
| 3557 | if (is_vmalloc_addr(p)) |
| 3558 | vfree(p); |
| 3559 | else |
| 3560 | kfree(p); |
| 3561 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3562 | |
| 3563 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3564 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3565 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3566 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3567 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3568 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3569 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3570 | |
| 3571 | /* |
| 3572 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3573 | * edge cases first; no work needs to be done for either |
| 3574 | */ |
| 3575 | if (length == 0 || length == 1) |
| 3576 | return length; |
| 3577 | /* src and dest walk down the list; dest counts unique elements */ |
| 3578 | for (src = 1; src < length; src++) { |
| 3579 | /* find next unique element */ |
| 3580 | while (list[src] == list[src-1]) { |
| 3581 | src++; |
| 3582 | if (src == length) |
| 3583 | goto after; |
| 3584 | } |
| 3585 | /* dest always points to where the next unique element goes */ |
| 3586 | list[dest] = list[src]; |
| 3587 | dest++; |
| 3588 | } |
| 3589 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3590 | return dest; |
| 3591 | } |
| 3592 | |
| 3593 | static int cmppid(const void *a, const void *b) |
| 3594 | { |
| 3595 | return *(pid_t *)a - *(pid_t *)b; |
| 3596 | } |
| 3597 | |
| 3598 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3599 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3600 | * returns with the lock on that pidlist already held, and takes care |
| 3601 | * of the use count, or returns NULL with no locks held if we're out of |
| 3602 | * memory. |
| 3603 | */ |
| 3604 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3605 | enum cgroup_filetype type) |
| 3606 | { |
| 3607 | struct cgroup_pidlist *l; |
| 3608 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3609 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3610 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3611 | /* |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3612 | * 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] | 3613 | * the last ref-holder is trying to remove l from the list at the same |
| 3614 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3615 | * list we find out from under us - compare release_pid_array(). |
| 3616 | */ |
| 3617 | mutex_lock(&cgrp->pidlist_mutex); |
| 3618 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3619 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3620 | /* make sure l doesn't vanish out from under us */ |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3621 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3622 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3623 | return l; |
| 3624 | } |
| 3625 | } |
| 3626 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3627 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3628 | if (!l) { |
| 3629 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3630 | return l; |
| 3631 | } |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3632 | init_rwsem(&l->rwsem); |
| 3633 | down_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3634 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3635 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3636 | l->owner = cgrp; |
| 3637 | list_add(&l->links, &cgrp->pidlists); |
| 3638 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3639 | return l; |
| 3640 | } |
| 3641 | |
| 3642 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3643 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3644 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3645 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3646 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3647 | { |
| 3648 | pid_t *array; |
| 3649 | int length; |
| 3650 | int pid, n = 0; /* used for populating the array */ |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3651 | struct cgroup_task_iter it; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3652 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3653 | struct cgroup_pidlist *l; |
| 3654 | |
| 3655 | /* |
| 3656 | * If cgroup gets more users after we read count, we won't have |
| 3657 | * enough space - tough. This race is indistinguishable to the |
| 3658 | * caller from the case that the additional cgroup users didn't |
| 3659 | * show up until sometime later on. |
| 3660 | */ |
| 3661 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3662 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3663 | if (!array) |
| 3664 | return -ENOMEM; |
| 3665 | /* now, populate the array */ |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3666 | cgroup_task_iter_start(cgrp, &it); |
| 3667 | while ((tsk = cgroup_task_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3668 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3669 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3670 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3671 | if (type == CGROUP_FILE_PROCS) |
| 3672 | pid = task_tgid_vnr(tsk); |
| 3673 | else |
| 3674 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | if (pid > 0) /* make sure to only use valid results */ |
| 3676 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3677 | } |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3678 | cgroup_task_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3679 | length = n; |
| 3680 | /* now sort & (if procs) strip out duplicates */ |
| 3681 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3682 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3683 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3684 | l = cgroup_pidlist_find(cgrp, type); |
| 3685 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3686 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3687 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3688 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3689 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3690 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3691 | l->list = array; |
| 3692 | l->length = length; |
| 3693 | l->use_count++; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3694 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3695 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3696 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3699 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3700 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3701 | * @stats: cgroupstats to fill information into |
| 3702 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3703 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3704 | * |
| 3705 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3706 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3707 | */ |
| 3708 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3709 | { |
| 3710 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3711 | struct cgroup *cgrp; |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3712 | struct cgroup_task_iter it; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3713 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3714 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3715 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3716 | * Validate dentry by checking the superblock operations, |
| 3717 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3718 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3719 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3720 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3721 | goto err; |
| 3722 | |
| 3723 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3724 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3725 | |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3726 | cgroup_task_iter_start(cgrp, &it); |
| 3727 | while ((tsk = cgroup_task_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3728 | switch (tsk->state) { |
| 3729 | case TASK_RUNNING: |
| 3730 | stats->nr_running++; |
| 3731 | break; |
| 3732 | case TASK_INTERRUPTIBLE: |
| 3733 | stats->nr_sleeping++; |
| 3734 | break; |
| 3735 | case TASK_UNINTERRUPTIBLE: |
| 3736 | stats->nr_uninterruptible++; |
| 3737 | break; |
| 3738 | case TASK_STOPPED: |
| 3739 | stats->nr_stopped++; |
| 3740 | break; |
| 3741 | default: |
| 3742 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3743 | stats->nr_io_wait++; |
| 3744 | break; |
| 3745 | } |
| 3746 | } |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 3747 | cgroup_task_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3748 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3749 | err: |
| 3750 | return ret; |
| 3751 | } |
| 3752 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3753 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3754 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3755 | * 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] | 3756 | * 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] | 3757 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3758 | */ |
| 3759 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3760 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3761 | { |
| 3762 | /* |
| 3763 | * Initially we receive a position value that corresponds to |
| 3764 | * one more than the last pid shown (or 0 on the first call or |
| 3765 | * after a seek to the start). Use a binary-search to find the |
| 3766 | * next pid to display, if any |
| 3767 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3768 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3769 | int index = 0, pid = *pos; |
| 3770 | int *iter; |
| 3771 | |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3772 | down_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3773 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3774 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3775 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3776 | while (index < end) { |
| 3777 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3778 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | index = mid; |
| 3780 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3781 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3782 | index = mid + 1; |
| 3783 | else |
| 3784 | end = mid; |
| 3785 | } |
| 3786 | } |
| 3787 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3788 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3789 | return NULL; |
| 3790 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3791 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3792 | *pos = *iter; |
| 3793 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3796 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3797 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3798 | struct cgroup_pidlist *l = s->private; |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3799 | up_read(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3802 | 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] | 3803 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3804 | struct cgroup_pidlist *l = s->private; |
| 3805 | pid_t *p = v; |
| 3806 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3807 | /* |
| 3808 | * Advance to the next pid in the array. If this goes off the |
| 3809 | * end, we're done |
| 3810 | */ |
| 3811 | p++; |
| 3812 | if (p >= end) { |
| 3813 | return NULL; |
| 3814 | } else { |
| 3815 | *pos = *p; |
| 3816 | return p; |
| 3817 | } |
| 3818 | } |
| 3819 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3820 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3821 | { |
| 3822 | return seq_printf(s, "%d\n", *(int *)v); |
| 3823 | } |
| 3824 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3825 | /* |
| 3826 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3827 | * independent of whether it's tasks or procs |
| 3828 | */ |
| 3829 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3830 | .start = cgroup_pidlist_start, |
| 3831 | .stop = cgroup_pidlist_stop, |
| 3832 | .next = cgroup_pidlist_next, |
| 3833 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3834 | }; |
| 3835 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3836 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3837 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3838 | /* |
| 3839 | * the case where we're the last user of this particular pidlist will |
| 3840 | * have us remove it from the cgroup's list, which entails taking the |
| 3841 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3842 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3843 | */ |
| 3844 | mutex_lock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3845 | down_write(&l->rwsem); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3846 | BUG_ON(!l->use_count); |
| 3847 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3848 | /* we're the last user if refcount is 0; remove and free */ |
| 3849 | list_del(&l->links); |
| 3850 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3851 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3852 | put_pid_ns(l->key.ns); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3853 | up_write(&l->rwsem); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3854 | kfree(l); |
| 3855 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3856 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3857 | mutex_unlock(&l->owner->pidlist_mutex); |
Li Zefan | b395890 | 2013-08-01 09:52:15 +0800 | [diff] [blame] | 3858 | up_write(&l->rwsem); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3859 | } |
| 3860 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3861 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3862 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3863 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3864 | if (!(file->f_mode & FMODE_READ)) |
| 3865 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3866 | /* |
| 3867 | * the seq_file will only be initialized if the file was opened for |
| 3868 | * reading; hence we check if it's not null only in that case. |
| 3869 | */ |
| 3870 | l = ((struct seq_file *)file->private_data)->private; |
| 3871 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3872 | return seq_release(inode, file); |
| 3873 | } |
| 3874 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3875 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3876 | .read = seq_read, |
| 3877 | .llseek = seq_lseek, |
| 3878 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3879 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3880 | }; |
| 3881 | |
| 3882 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3883 | * The following functions handle opens on a file that displays a pidlist |
| 3884 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3885 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3886 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3887 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3888 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3889 | { |
| 3890 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3891 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3892 | int retval; |
| 3893 | |
| 3894 | /* Nothing to do for write-only files */ |
| 3895 | if (!(file->f_mode & FMODE_READ)) |
| 3896 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3897 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3898 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3899 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3900 | if (retval) |
| 3901 | return retval; |
| 3902 | /* configure file information */ |
| 3903 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3904 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3905 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3906 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3907 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3908 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3909 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3910 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3911 | return 0; |
| 3912 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3913 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3914 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3915 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3916 | } |
| 3917 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3918 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3919 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3920 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3921 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3922 | static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css, |
| 3923 | struct cftype *cft) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3924 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3925 | return notify_on_release(css->cgroup); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3928 | static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css, |
| 3929 | struct cftype *cft, u64 val) |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3930 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3931 | clear_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3932 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3933 | set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3934 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3935 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags); |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3936 | return 0; |
| 3937 | } |
| 3938 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3939 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3940 | * When dput() is called asynchronously, if umount has been done and |
| 3941 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3942 | * there's a small window that vfs will see the root dentry with non-zero |
| 3943 | * refcnt and trigger BUG(). |
| 3944 | * |
| 3945 | * That's why we hold a reference before dput() and drop it right after. |
| 3946 | */ |
| 3947 | static void cgroup_dput(struct cgroup *cgrp) |
| 3948 | { |
| 3949 | struct super_block *sb = cgrp->root->sb; |
| 3950 | |
| 3951 | atomic_inc(&sb->s_active); |
| 3952 | dput(cgrp->dentry); |
| 3953 | deactivate_super(sb); |
| 3954 | } |
| 3955 | |
| 3956 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3957 | * Unregister event and free resources. |
| 3958 | * |
| 3959 | * Gets called from workqueue. |
| 3960 | */ |
| 3961 | static void cgroup_event_remove(struct work_struct *work) |
| 3962 | { |
| 3963 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3964 | remove); |
| 3965 | struct cgroup *cgrp = event->cgrp; |
| 3966 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3967 | remove_wait_queue(event->wqh, &event->wait); |
| 3968 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3969 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3970 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3971 | /* Notify userspace the event is going away. */ |
| 3972 | eventfd_signal(event->eventfd, 1); |
| 3973 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3974 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3975 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3976 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | /* |
| 3980 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3981 | * |
| 3982 | * Called with wqh->lock held and interrupts disabled. |
| 3983 | */ |
| 3984 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3985 | int sync, void *key) |
| 3986 | { |
| 3987 | struct cgroup_event *event = container_of(wait, |
| 3988 | struct cgroup_event, wait); |
| 3989 | struct cgroup *cgrp = event->cgrp; |
| 3990 | unsigned long flags = (unsigned long)key; |
| 3991 | |
| 3992 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3993 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3994 | * If the event has been detached at cgroup removal, we |
| 3995 | * can simply return knowing the other side will cleanup |
| 3996 | * for us. |
| 3997 | * |
| 3998 | * We can't race against event freeing since the other |
| 3999 | * side will require wqh->lock via remove_wait_queue(), |
| 4000 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4001 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 4002 | spin_lock(&cgrp->event_list_lock); |
| 4003 | if (!list_empty(&event->list)) { |
| 4004 | list_del_init(&event->list); |
| 4005 | /* |
| 4006 | * We are in atomic context, but cgroup_event_remove() |
| 4007 | * may sleep, so we have to call it in workqueue. |
| 4008 | */ |
| 4009 | schedule_work(&event->remove); |
| 4010 | } |
| 4011 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 4018 | wait_queue_head_t *wqh, poll_table *pt) |
| 4019 | { |
| 4020 | struct cgroup_event *event = container_of(pt, |
| 4021 | struct cgroup_event, pt); |
| 4022 | |
| 4023 | event->wqh = wqh; |
| 4024 | add_wait_queue(wqh, &event->wait); |
| 4025 | } |
| 4026 | |
| 4027 | /* |
| 4028 | * Parse input and register new cgroup event handler. |
| 4029 | * |
| 4030 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 4031 | * Interpretation of args is defined by control file implementation. |
| 4032 | */ |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4033 | static int cgroup_write_event_control(struct cgroup_subsys_state *css, |
| 4034 | struct cftype *cft, const char *buffer) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4035 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4036 | struct cgroup *cgrp = css->cgroup; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4037 | struct cgroup_event *event; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4038 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4039 | unsigned int efd, cfd; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4040 | struct file *efile; |
| 4041 | struct file *cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4042 | char *endp; |
| 4043 | int ret; |
| 4044 | |
| 4045 | efd = simple_strtoul(buffer, &endp, 10); |
| 4046 | if (*endp != ' ') |
| 4047 | return -EINVAL; |
| 4048 | buffer = endp + 1; |
| 4049 | |
| 4050 | cfd = simple_strtoul(buffer, &endp, 10); |
| 4051 | if ((*endp != ' ') && (*endp != '\0')) |
| 4052 | return -EINVAL; |
| 4053 | buffer = endp + 1; |
| 4054 | |
| 4055 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 4056 | if (!event) |
| 4057 | return -ENOMEM; |
| 4058 | event->cgrp = cgrp; |
| 4059 | INIT_LIST_HEAD(&event->list); |
| 4060 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 4061 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 4062 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 4063 | |
| 4064 | efile = eventfd_fget(efd); |
| 4065 | if (IS_ERR(efile)) { |
| 4066 | ret = PTR_ERR(efile); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4067 | goto out_kfree; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | event->eventfd = eventfd_ctx_fileget(efile); |
| 4071 | if (IS_ERR(event->eventfd)) { |
| 4072 | ret = PTR_ERR(event->eventfd); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4073 | goto out_put_efile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | cfile = fget(cfd); |
| 4077 | if (!cfile) { |
| 4078 | ret = -EBADF; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4079 | goto out_put_eventfd; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 4083 | /* 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] | 4084 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4085 | if (ret < 0) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4086 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4087 | |
| 4088 | event->cft = __file_cft(cfile); |
| 4089 | if (IS_ERR(event->cft)) { |
| 4090 | ret = PTR_ERR(event->cft); |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4091 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4092 | } |
| 4093 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4094 | /* |
| 4095 | * The file to be monitored must be in the same cgroup as |
| 4096 | * cgroup.event_control is. |
| 4097 | */ |
| 4098 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4099 | if (cgrp_cfile != cgrp) { |
| 4100 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4101 | goto out_put_cfile; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 4102 | } |
| 4103 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4104 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4105 | ret = -EINVAL; |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4106 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | ret = event->cft->register_event(cgrp, event->cft, |
| 4110 | event->eventfd, buffer); |
| 4111 | if (ret) |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4112 | goto out_put_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4113 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4114 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4115 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4116 | /* |
| 4117 | * Events should be removed after rmdir of cgroup directory, but before |
| 4118 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4119 | * directory dentry to do that. |
| 4120 | */ |
| 4121 | dget(cgrp->dentry); |
| 4122 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4123 | spin_lock(&cgrp->event_list_lock); |
| 4124 | list_add(&event->list, &cgrp->event_list); |
| 4125 | spin_unlock(&cgrp->event_list_lock); |
| 4126 | |
| 4127 | fput(cfile); |
| 4128 | fput(efile); |
| 4129 | |
| 4130 | return 0; |
| 4131 | |
Li Zefan | 876ede8 | 2013-08-01 09:51:47 +0800 | [diff] [blame] | 4132 | out_put_cfile: |
| 4133 | fput(cfile); |
| 4134 | out_put_eventfd: |
| 4135 | eventfd_ctx_put(event->eventfd); |
| 4136 | out_put_efile: |
| 4137 | fput(efile); |
| 4138 | out_kfree: |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4139 | kfree(event); |
| 4140 | |
| 4141 | return ret; |
| 4142 | } |
| 4143 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4144 | static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css, |
| 4145 | struct cftype *cft) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4146 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4147 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4150 | static int cgroup_clone_children_write(struct cgroup_subsys_state *css, |
| 4151 | struct cftype *cft, u64 val) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4152 | { |
| 4153 | if (val) |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4154 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4155 | else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4156 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4157 | return 0; |
| 4158 | } |
| 4159 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4160 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4161 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4162 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4163 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4164 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4165 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4166 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4167 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4168 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4169 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4170 | .write_string = cgroup_write_event_control, |
| 4171 | .mode = S_IWUGO, |
| 4172 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4173 | { |
| 4174 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4175 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4176 | .read_u64 = cgroup_clone_children_read, |
| 4177 | .write_u64 = cgroup_clone_children_write, |
| 4178 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4179 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4180 | .name = "cgroup.sane_behavior", |
| 4181 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4182 | .read_seq_string = cgroup_sane_behavior_show, |
| 4183 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4184 | |
| 4185 | /* |
| 4186 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4187 | * don't exist if sane_behavior. If you're depending on these, be |
| 4188 | * prepared to be burned. |
| 4189 | */ |
| 4190 | { |
| 4191 | .name = "tasks", |
| 4192 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4193 | .open = cgroup_tasks_open, |
| 4194 | .write_u64 = cgroup_tasks_write, |
| 4195 | .release = cgroup_pidlist_release, |
| 4196 | .mode = S_IRUGO | S_IWUSR, |
| 4197 | }, |
| 4198 | { |
| 4199 | .name = "notify_on_release", |
| 4200 | .flags = CFTYPE_INSANE, |
| 4201 | .read_u64 = cgroup_read_notify_on_release, |
| 4202 | .write_u64 = cgroup_write_notify_on_release, |
| 4203 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4204 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4205 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4206 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4207 | .read_seq_string = cgroup_release_agent_show, |
| 4208 | .write_string = cgroup_release_agent_write, |
| 4209 | .max_write_len = PATH_MAX, |
| 4210 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4211 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4212 | }; |
| 4213 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4214 | /** |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4215 | * cgroup_populate_dir - create subsys files in a cgroup directory |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4216 | * @cgrp: target cgroup |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4217 | * @subsys_mask: mask of the subsystem ids whose files should be added |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4218 | * |
| 4219 | * On failure, no file is added. |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4220 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4221 | static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4222 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4223 | struct cgroup_subsys *ss; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4224 | int i, ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4225 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4226 | /* process cftsets of each subsystem */ |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4227 | for_each_subsys(ss, i) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4228 | struct cftype_set *set; |
Tejun Heo | b420ba7 | 2013-07-12 12:34:02 -0700 | [diff] [blame] | 4229 | |
| 4230 | if (!test_bit(i, &subsys_mask)) |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4231 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4232 | |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4233 | list_for_each_entry(set, &ss->cftsets, node) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4234 | ret = cgroup_addrm_files(cgrp, set->cfts, true); |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4235 | if (ret < 0) |
| 4236 | goto err; |
| 4237 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4238 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4239 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4240 | /* This cgroup is ready now */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4241 | for_each_root_subsys(cgrp->root, ss) { |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4242 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4243 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 4244 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4245 | /* |
| 4246 | * Update id->css pointer and make this css visible from |
| 4247 | * CSS ID functions. This pointer will be dereferened |
| 4248 | * from RCU-read-side without locks. |
| 4249 | */ |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4250 | if (id) |
| 4251 | rcu_assign_pointer(id->css, css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4252 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4253 | |
| 4254 | return 0; |
Tejun Heo | bee5509 | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4255 | err: |
| 4256 | cgroup_clear_dir(cgrp, subsys_mask); |
| 4257 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4260 | static void css_dput_fn(struct work_struct *work) |
| 4261 | { |
| 4262 | struct cgroup_subsys_state *css = |
| 4263 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4264 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4265 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4268 | static void css_release(struct percpu_ref *ref) |
| 4269 | { |
| 4270 | struct cgroup_subsys_state *css = |
| 4271 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4272 | |
| 4273 | schedule_work(&css->dput_work); |
| 4274 | } |
| 4275 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4276 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4277 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4278 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4279 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4280 | css->cgroup = cgrp; |
Tejun Heo | 72c97e5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 4281 | css->ss = ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4282 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4283 | css->id = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4284 | if (cgrp == cgroup_dummy_top) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4285 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4286 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4287 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4288 | |
| 4289 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4290 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4291 | * css_put(). dput() requires process context, which css_put() may |
| 4292 | * be called without. @css->dput_work will be used to invoke |
| 4293 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4294 | */ |
| 4295 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4296 | } |
| 4297 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4298 | /* 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] | 4299 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4300 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4301 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4302 | int ret = 0; |
| 4303 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4304 | lockdep_assert_held(&cgroup_mutex); |
| 4305 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4306 | if (ss->css_online) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4307 | ret = ss->css_online(css); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4308 | if (!ret) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4309 | css->flags |= CSS_ONLINE; |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4310 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4311 | } |
| 4312 | |
Li Zefan | 2a4ac63 | 2013-07-31 16:16:40 +0800 | [diff] [blame] | 4313 | /* 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] | 4314 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4315 | { |
| 4316 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4317 | |
| 4318 | lockdep_assert_held(&cgroup_mutex); |
| 4319 | |
| 4320 | if (!(css->flags & CSS_ONLINE)) |
| 4321 | return; |
| 4322 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4323 | if (ss->css_offline) |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4324 | ss->css_offline(css); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4325 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4326 | css->flags &= ~CSS_ONLINE; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4327 | } |
| 4328 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4329 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4330 | * cgroup_create - create a cgroup |
| 4331 | * @parent: cgroup that will be parent of the new cgroup |
| 4332 | * @dentry: dentry of the new cgroup |
| 4333 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4334 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4335 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4336 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4337 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4338 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4339 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4340 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4341 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4342 | struct cgroupfs_root *root = parent->root; |
| 4343 | int err = 0; |
| 4344 | struct cgroup_subsys *ss; |
| 4345 | struct super_block *sb = root->sb; |
| 4346 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4347 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4348 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4349 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4350 | return -ENOMEM; |
| 4351 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4352 | name = cgroup_alloc_name(dentry); |
| 4353 | if (!name) |
| 4354 | goto err_free_cgrp; |
| 4355 | rcu_assign_pointer(cgrp->name, name); |
| 4356 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4357 | /* |
| 4358 | * Temporarily set the pointer to NULL, so idr_find() won't return |
| 4359 | * a half-baked cgroup. |
| 4360 | */ |
| 4361 | cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4362 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4363 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4364 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4365 | /* |
| 4366 | * Only live parents can have children. Note that the liveliness |
| 4367 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4368 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4369 | * anyway so that locking is contained inside cgroup proper and we |
| 4370 | * don't get nasty surprises if we ever grow another caller. |
| 4371 | */ |
| 4372 | if (!cgroup_lock_live_group(parent)) { |
| 4373 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4374 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4375 | } |
| 4376 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4377 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4378 | * get deleted on unmount if there are child cgroups. This |
| 4379 | * can be done outside cgroup_mutex, since the sb can't |
| 4380 | * disappear while someone has an open control file on the |
| 4381 | * fs */ |
| 4382 | atomic_inc(&sb->s_active); |
| 4383 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4384 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4385 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4386 | dentry->d_fsdata = cgrp; |
| 4387 | cgrp->dentry = dentry; |
| 4388 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4389 | cgrp->parent = parent; |
| 4390 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4391 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4392 | if (notify_on_release(parent)) |
| 4393 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4394 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4395 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4396 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4397 | |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4398 | for_each_root_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4399 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4400 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4401 | css = ss->css_alloc(parent->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4402 | if (IS_ERR(css)) { |
| 4403 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4404 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4405 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4406 | |
| 4407 | err = percpu_ref_init(&css->refcnt, css_release); |
Li Zefan | da0a12c | 2013-07-31 16:16:28 +0800 | [diff] [blame] | 4408 | if (err) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4409 | ss->css_free(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4410 | goto err_free_all; |
Li Zefan | da0a12c | 2013-07-31 16:16:28 +0800 | [diff] [blame] | 4411 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4412 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4413 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4414 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4415 | if (ss->use_id) { |
| 4416 | err = alloc_css_id(ss, parent, cgrp); |
| 4417 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4418 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4419 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4422 | /* |
| 4423 | * Create directory. cgroup_create_file() returns with the new |
| 4424 | * directory locked on success so that it can be populated without |
| 4425 | * dropping cgroup_mutex. |
| 4426 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4427 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4428 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4429 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4430 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4431 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4432 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4433 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4434 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4435 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4436 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4437 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4438 | /* each css holds a ref to the cgroup's dentry */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4439 | for_each_root_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4440 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4441 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4442 | /* hold a ref to the parent's dentry */ |
| 4443 | dget(parent->dentry); |
| 4444 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4445 | /* creation succeeded, notify subsystems */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4446 | for_each_root_subsys(root, ss) { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4447 | err = online_css(ss, cgrp); |
| 4448 | if (err) |
| 4449 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4450 | |
| 4451 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4452 | parent->parent) { |
| 4453 | 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", |
| 4454 | current->comm, current->pid, ss->name); |
| 4455 | if (!strcmp(ss->name, "memory")) |
| 4456 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4457 | ss->warned_broken_hierarchy = true; |
| 4458 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4459 | } |
| 4460 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4461 | idr_replace(&root->cgroup_idr, cgrp, cgrp->id); |
| 4462 | |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4463 | err = cgroup_addrm_files(cgrp, cgroup_base_files, true); |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4464 | if (err) |
| 4465 | goto err_destroy; |
| 4466 | |
| 4467 | err = cgroup_populate_dir(cgrp, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4468 | if (err) |
| 4469 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4470 | |
| 4471 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4472 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4473 | |
| 4474 | return 0; |
| 4475 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4476 | err_free_all: |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4477 | for_each_root_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4478 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4479 | |
| 4480 | if (css) { |
| 4481 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4482 | ss->css_free(css); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4483 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4484 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4485 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4486 | /* Release the reference count that we took on the superblock */ |
| 4487 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4488 | err_free_id: |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4489 | idr_remove(&root->cgroup_idr, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4490 | err_free_name: |
| 4491 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4492 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4493 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4494 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4495 | |
| 4496 | err_destroy: |
| 4497 | cgroup_destroy_locked(cgrp); |
| 4498 | mutex_unlock(&cgroup_mutex); |
| 4499 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4500 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4503 | 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] | 4504 | { |
| 4505 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4506 | |
| 4507 | /* the vfs holds inode->i_mutex already */ |
| 4508 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4509 | } |
| 4510 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4511 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4512 | { |
| 4513 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4514 | return; |
| 4515 | |
| 4516 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4517 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4518 | schedule_work(&cgrp->destroy_work); |
| 4519 | } |
| 4520 | |
| 4521 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4522 | { |
| 4523 | struct cgroup_subsys_state *css = |
| 4524 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4525 | |
| 4526 | cgroup_css_killed(css->cgroup); |
| 4527 | } |
| 4528 | |
| 4529 | /** |
| 4530 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4531 | * @cgrp: cgroup to be destroyed |
| 4532 | * |
| 4533 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4534 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4535 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4536 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4537 | * the following two steps. |
| 4538 | * |
| 4539 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4540 | * userland visible parts and start killing the percpu refcnts of |
| 4541 | * css's. Set up so that the next stage will be kicked off once all |
| 4542 | * the percpu refcnts are confirmed to be killed. |
| 4543 | * |
| 4544 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4545 | * rest of destruction. Once all cgroup references are gone, the |
| 4546 | * cgroup is RCU-freed. |
| 4547 | * |
| 4548 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4549 | * the userland is concerned and a new cgroup with the same name may be |
| 4550 | * created. As cgroup doesn't care about the names internally, this |
| 4551 | * doesn't cause any problem. |
| 4552 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4553 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4554 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4555 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4556 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4557 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4558 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4559 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4560 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4561 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4562 | lockdep_assert_held(&cgroup_mutex); |
| 4563 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4564 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4565 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4566 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4567 | */ |
| 4568 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4569 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4570 | read_unlock(&css_set_lock); |
| 4571 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4572 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4573 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4574 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4575 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4576 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4577 | * css reference will be given out via css_tryget(). We can't |
| 4578 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4579 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4580 | * as killed on all CPUs on return. |
| 4581 | * |
| 4582 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4583 | * css is confirmed to be seen as killed on all CPUs. The |
| 4584 | * notification callback keeps track of the number of css's to be |
| 4585 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4586 | * destruction once the percpu refs of all css's are confirmed to |
| 4587 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4588 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4589 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4590 | for_each_root_subsys(cgrp->root, ss) { |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4591 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4592 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4593 | /* |
| 4594 | * Killing would put the base ref, but we need to keep it |
| 4595 | * alive until after ->css_offline. |
| 4596 | */ |
| 4597 | percpu_ref_get(&css->refcnt); |
| 4598 | |
| 4599 | atomic_inc(&cgrp->css_kill_cnt); |
| 4600 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4601 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4602 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4603 | |
| 4604 | /* |
| 4605 | * Mark @cgrp dead. This prevents further task migration and child |
| 4606 | * creation by disabling cgroup_lock_live_group(). Note that |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4607 | * CGRP_DEAD assertion is depended upon by css_next_child() to |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4608 | * resume iteration after dropping RCU read lock. See |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 4609 | * css_next_child() for details. |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4610 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4611 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4612 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4613 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4614 | raw_spin_lock(&release_list_lock); |
| 4615 | if (!list_empty(&cgrp->release_list)) |
| 4616 | list_del_init(&cgrp->release_list); |
| 4617 | raw_spin_unlock(&release_list_lock); |
| 4618 | |
| 4619 | /* |
Tejun Heo | 8f89140 | 2013-06-28 16:24:10 -0700 | [diff] [blame] | 4620 | * Clear and remove @cgrp directory. The removal puts the base ref |
| 4621 | * 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] | 4622 | */ |
Tejun Heo | 628f7cd | 2013-06-28 16:24:11 -0700 | [diff] [blame] | 4623 | cgroup_clear_dir(cgrp, cgrp->root->subsys_mask); |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4624 | cgroup_addrm_files(cgrp, cgroup_base_files, false); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4625 | dget(d); |
| 4626 | cgroup_d_remove_dir(d); |
| 4627 | |
| 4628 | /* |
| 4629 | * Unregister events and notify userspace. |
| 4630 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4631 | * directory to avoid race between userspace and kernelspace. |
| 4632 | */ |
| 4633 | spin_lock(&cgrp->event_list_lock); |
| 4634 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4635 | list_del_init(&event->list); |
| 4636 | schedule_work(&event->remove); |
| 4637 | } |
| 4638 | spin_unlock(&cgrp->event_list_lock); |
| 4639 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4640 | return 0; |
| 4641 | }; |
| 4642 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4643 | /** |
| 4644 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4645 | * @work: cgroup->destroy_free_work |
| 4646 | * |
| 4647 | * This function is invoked from a work item for a cgroup which is being |
| 4648 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4649 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4650 | * is the second step of destruction described in the comment above |
| 4651 | * cgroup_destroy_locked(). |
| 4652 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4653 | static void cgroup_offline_fn(struct work_struct *work) |
| 4654 | { |
| 4655 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4656 | struct cgroup *parent = cgrp->parent; |
| 4657 | struct dentry *d = cgrp->dentry; |
| 4658 | struct cgroup_subsys *ss; |
| 4659 | |
| 4660 | mutex_lock(&cgroup_mutex); |
| 4661 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4662 | /* |
| 4663 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4664 | * initate destruction. |
| 4665 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4666 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4667 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4668 | |
| 4669 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4670 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4671 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4672 | * proceeds regardless of css refs. On the last put of each css, |
| 4673 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4674 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4675 | */ |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 4676 | for_each_root_subsys(cgrp->root, ss) |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4677 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4678 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4679 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4680 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4681 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 4682 | /* |
| 4683 | * We should remove the cgroup object from idr before its grace |
| 4684 | * period starts, so we won't be looking up a cgroup while the |
| 4685 | * cgroup is being freed. |
| 4686 | */ |
| 4687 | idr_remove(&cgrp->root->cgroup_idr, cgrp->id); |
| 4688 | cgrp->id = -1; |
| 4689 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4690 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4691 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4692 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4693 | check_for_release(parent); |
| 4694 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4695 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4698 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4699 | { |
| 4700 | int ret; |
| 4701 | |
| 4702 | mutex_lock(&cgroup_mutex); |
| 4703 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4704 | mutex_unlock(&cgroup_mutex); |
| 4705 | |
| 4706 | return ret; |
| 4707 | } |
| 4708 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4709 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4710 | { |
| 4711 | INIT_LIST_HEAD(&ss->cftsets); |
| 4712 | |
| 4713 | /* |
| 4714 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4715 | * deregistration. |
| 4716 | */ |
| 4717 | if (ss->base_cftypes) { |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4718 | struct cftype *cft; |
| 4719 | |
| 4720 | for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++) |
| 4721 | cft->ss = ss; |
| 4722 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4723 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4724 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4725 | } |
| 4726 | } |
| 4727 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4728 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4729 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4730 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4731 | |
| 4732 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4733 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4734 | mutex_lock(&cgroup_mutex); |
| 4735 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4736 | /* init base cftset */ |
| 4737 | cgroup_init_cftsets(ss); |
| 4738 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4739 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4740 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4741 | ss->root = &cgroup_dummy_root; |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4742 | css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4743 | /* We don't handle early failures gracefully */ |
| 4744 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4745 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4746 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4747 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4748 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4749 | * newly registered, all tasks and hence the |
| 4750 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4751 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4752 | |
| 4753 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4754 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4755 | /* At system boot, before all subsystems have been |
| 4756 | * registered, no tasks have been forked, so we don't |
| 4757 | * need to invoke fork callbacks here. */ |
| 4758 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4759 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4760 | BUG_ON(online_css(ss, cgroup_dummy_top)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4761 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4762 | mutex_unlock(&cgroup_mutex); |
| 4763 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4764 | /* this function shouldn't be used with modular subsystems, since they |
| 4765 | * need to register a subsys_id, among other things */ |
| 4766 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4770 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4771 | * @ss: the subsystem to load |
| 4772 | * |
| 4773 | * 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] | 4774 | * 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] | 4775 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4776 | * simpler cgroup_init_subsys. |
| 4777 | */ |
| 4778 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4779 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4780 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4781 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4782 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4783 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4784 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4785 | |
| 4786 | /* check name and function validity */ |
| 4787 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4788 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4789 | return -EINVAL; |
| 4790 | |
| 4791 | /* |
| 4792 | * we don't support callbacks in modular subsystems. this check is |
| 4793 | * before the ss->module check for consistency; a subsystem that could |
| 4794 | * be a module should still have no callbacks even if the user isn't |
| 4795 | * compiling it as one. |
| 4796 | */ |
| 4797 | if (ss->fork || ss->exit) |
| 4798 | return -EINVAL; |
| 4799 | |
| 4800 | /* |
| 4801 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4802 | * since cgroup_init_subsys will have already taken care of it. |
| 4803 | */ |
| 4804 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4805 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4806 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4807 | return 0; |
| 4808 | } |
| 4809 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4810 | /* init base cftset */ |
| 4811 | cgroup_init_cftsets(ss); |
| 4812 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4813 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4814 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4815 | |
| 4816 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4817 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4818 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4819 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4820 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4821 | css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4822 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4823 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4824 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4825 | mutex_unlock(&cgroup_mutex); |
| 4826 | return PTR_ERR(css); |
| 4827 | } |
| 4828 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4829 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4830 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4831 | |
| 4832 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4833 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4834 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4835 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4836 | ret = cgroup_init_idr(ss, css); |
| 4837 | if (ret) |
| 4838 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | /* |
| 4842 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4843 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4844 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4845 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4846 | * table state, so each changed css_set will need its hash recomputed. |
| 4847 | * this is all done under the css_set_lock. |
| 4848 | */ |
| 4849 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4850 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4851 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4852 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4853 | continue; |
| 4854 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4855 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4856 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4857 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4858 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4859 | key = css_set_hash(cset->subsys); |
| 4860 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4861 | } |
| 4862 | write_unlock(&css_set_lock); |
| 4863 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4864 | ret = online_css(ss, cgroup_dummy_top); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4865 | if (ret) |
| 4866 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4867 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4868 | /* success! */ |
| 4869 | mutex_unlock(&cgroup_mutex); |
| 4870 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4871 | |
| 4872 | err_unload: |
| 4873 | mutex_unlock(&cgroup_mutex); |
| 4874 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4875 | cgroup_unload_subsys(ss); |
| 4876 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4877 | } |
| 4878 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4879 | |
| 4880 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4881 | * cgroup_unload_subsys: unload a modular subsystem |
| 4882 | * @ss: the subsystem to unload |
| 4883 | * |
| 4884 | * This function should be called in a modular subsystem's exitcall. When this |
| 4885 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4886 | * the subsystem will not be attached to any hierarchy. |
| 4887 | */ |
| 4888 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4889 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4890 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4891 | |
| 4892 | BUG_ON(ss->module == NULL); |
| 4893 | |
| 4894 | /* |
| 4895 | * 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] | 4896 | * try_module_get() in rebind_subsystems() should ensure that it |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4897 | * doesn't start being used while we're killing it off. |
| 4898 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4899 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4900 | |
| 4901 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4902 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4903 | offline_css(ss, cgroup_dummy_top); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4904 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4905 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4906 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4907 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4908 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4909 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4910 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4911 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4912 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4913 | |
| 4914 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4915 | * disentangle the css from all css_sets attached to the dummy |
| 4916 | * top. as in loading, we need to pay our respects to the hashtable |
| 4917 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4918 | */ |
| 4919 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4920 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4921 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4922 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4923 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4924 | hash_del(&cset->hlist); |
| 4925 | cset->subsys[ss->subsys_id] = NULL; |
| 4926 | key = css_set_hash(cset->subsys); |
| 4927 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4928 | } |
| 4929 | write_unlock(&css_set_lock); |
| 4930 | |
| 4931 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4932 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4933 | * need to free before marking as null because ss->css_free needs |
| 4934 | * the cgrp->subsys pointer to find their state. note that this |
| 4935 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4936 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 4937 | ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4938 | cgroup_dummy_top->subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4939 | |
| 4940 | mutex_unlock(&cgroup_mutex); |
| 4941 | } |
| 4942 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4943 | |
| 4944 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4945 | * cgroup_init_early - cgroup initialization at system boot |
| 4946 | * |
| 4947 | * Initialize cgroups at system boot, and initialize any |
| 4948 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4949 | */ |
| 4950 | int __init cgroup_init_early(void) |
| 4951 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4952 | struct cgroup_subsys *ss; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4953 | int i; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4954 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4955 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4956 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4957 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4958 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4959 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4960 | init_cgroup_root(&cgroup_dummy_root); |
| 4961 | cgroup_root_count = 1; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 4962 | RCU_INIT_POINTER(init_task.cgroups, &init_css_set); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4963 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4964 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4965 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4966 | 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] | 4967 | 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] | 4968 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4969 | /* at bootup time, we don't worry about modular subsystems */ |
| 4970 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4971 | BUG_ON(!ss->name); |
| 4972 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4973 | BUG_ON(!ss->css_alloc); |
| 4974 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4975 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4976 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4977 | ss->name, ss->subsys_id); |
| 4978 | BUG(); |
| 4979 | } |
| 4980 | |
| 4981 | if (ss->early_init) |
| 4982 | cgroup_init_subsys(ss); |
| 4983 | } |
| 4984 | return 0; |
| 4985 | } |
| 4986 | |
| 4987 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4988 | * cgroup_init - cgroup initialization |
| 4989 | * |
| 4990 | * Register cgroup filesystem and /proc file, and initialize |
| 4991 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4992 | */ |
| 4993 | int __init cgroup_init(void) |
| 4994 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4995 | struct cgroup_subsys *ss; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4996 | unsigned long key; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 4997 | int i, err; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4998 | |
| 4999 | err = bdi_init(&cgroup_backing_dev_info); |
| 5000 | if (err) |
| 5001 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5002 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5003 | for_each_builtin_subsys(ss, i) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5004 | if (!ss->early_init) |
| 5005 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5006 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5007 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 5010 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5011 | mutex_lock(&cgroup_mutex); |
| 5012 | mutex_lock(&cgroup_root_mutex); |
| 5013 | |
Tejun Heo | 82fe9b0 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5014 | /* Add init_css_set to the hash table */ |
| 5015 | key = css_set_hash(init_css_set.subsys); |
| 5016 | hash_add(css_set_table, &init_css_set.hlist, key); |
| 5017 | |
Tejun Heo | fc76df7 | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5018 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5019 | |
Li Zefan | 4e96ee8e | 2013-07-31 09:50:50 +0800 | [diff] [blame] | 5020 | err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top, |
| 5021 | 0, 1, GFP_KERNEL); |
| 5022 | BUG_ON(err < 0); |
| 5023 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 5024 | mutex_unlock(&cgroup_root_mutex); |
| 5025 | mutex_unlock(&cgroup_mutex); |
| 5026 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5027 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 5028 | if (!cgroup_kobj) { |
| 5029 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5030 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | err = register_filesystem(&cgroup_fs_type); |
| 5034 | if (err < 0) { |
| 5035 | kobject_put(cgroup_kobj); |
| 5036 | goto out; |
| 5037 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5038 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 5039 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5040 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5041 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5042 | if (err) |
| 5043 | bdi_destroy(&cgroup_backing_dev_info); |
| 5044 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 5045 | return err; |
| 5046 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5047 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5048 | /* |
| 5049 | * proc_cgroup_show() |
| 5050 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 5051 | * - Used for /proc/<pid>/cgroup. |
| 5052 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 5053 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 5054 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5055 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 5056 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 5057 | * cgroup to top_cgroup. |
| 5058 | */ |
| 5059 | |
| 5060 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 5061 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5062 | { |
| 5063 | struct pid *pid; |
| 5064 | struct task_struct *tsk; |
| 5065 | char *buf; |
| 5066 | int retval; |
| 5067 | struct cgroupfs_root *root; |
| 5068 | |
| 5069 | retval = -ENOMEM; |
| 5070 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 5071 | if (!buf) |
| 5072 | goto out; |
| 5073 | |
| 5074 | retval = -ESRCH; |
| 5075 | pid = m->private; |
| 5076 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 5077 | if (!tsk) |
| 5078 | goto out_free; |
| 5079 | |
| 5080 | retval = 0; |
| 5081 | |
| 5082 | mutex_lock(&cgroup_mutex); |
| 5083 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 5084 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5085 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5086 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5087 | int count = 0; |
| 5088 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5089 | seq_printf(m, "%d:", root->hierarchy_id); |
Tejun Heo | 5549c49 | 2013-06-24 15:21:48 -0700 | [diff] [blame] | 5090 | for_each_root_subsys(root, ss) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5091 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 5092 | if (strlen(root->name)) |
| 5093 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 5094 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5095 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5096 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5097 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5098 | if (retval < 0) |
| 5099 | goto out_unlock; |
| 5100 | seq_puts(m, buf); |
| 5101 | seq_putc(m, '\n'); |
| 5102 | } |
| 5103 | |
| 5104 | out_unlock: |
| 5105 | mutex_unlock(&cgroup_mutex); |
| 5106 | put_task_struct(tsk); |
| 5107 | out_free: |
| 5108 | kfree(buf); |
| 5109 | out: |
| 5110 | return retval; |
| 5111 | } |
| 5112 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5113 | /* Display information about each subsystem and each hierarchy */ |
| 5114 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 5115 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5116 | struct cgroup_subsys *ss; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5117 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5118 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5119 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5120 | /* |
| 5121 | * ideally we don't want subsystems moving around while we do this. |
| 5122 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5123 | * subsys/hierarchy state. |
| 5124 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5125 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5126 | |
| 5127 | for_each_subsys(ss, i) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5128 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5129 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5130 | ss->root->number_of_cgroups, !ss->disabled); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5131 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5132 | mutex_unlock(&cgroup_mutex); |
| 5133 | return 0; |
| 5134 | } |
| 5135 | |
| 5136 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5137 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5138 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5141 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5142 | .open = cgroupstats_open, |
| 5143 | .read = seq_read, |
| 5144 | .llseek = seq_lseek, |
| 5145 | .release = single_release, |
| 5146 | }; |
| 5147 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5148 | /** |
| 5149 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5150 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5151 | * |
| 5152 | * Description: A task inherits its parent's cgroup at fork(). |
| 5153 | * |
| 5154 | * A pointer to the shared css_set was automatically copied in |
| 5155 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5156 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5157 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5158 | * have already changed current->cgroups, allowing the previously |
| 5159 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5160 | * |
| 5161 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5162 | * task, and the passed argument 'child' points to the child task. |
| 5163 | */ |
| 5164 | void cgroup_fork(struct task_struct *child) |
| 5165 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5166 | task_lock(current); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5167 | get_css_set(task_css_set(current)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5168 | child->cgroups = current->cgroups; |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5169 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5170 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5174 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5175 | * @child: the task in question |
| 5176 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5177 | * Adds the task to the list running through its css_set if necessary and |
| 5178 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5179 | * visible on the task list in case we race with the first call to |
Tejun Heo | 0942eee | 2013-08-08 20:11:26 -0400 | [diff] [blame^] | 5180 | * cgroup_task_iter_start() - to guarantee that the new task ends up on its |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5181 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5182 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5183 | void cgroup_post_fork(struct task_struct *child) |
| 5184 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5185 | struct cgroup_subsys *ss; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5186 | int i; |
| 5187 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5188 | /* |
| 5189 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5190 | * under the tasklist_lock and we read it here after we added the child |
| 5191 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5192 | * yet in the tasklist when we walked through it from |
| 5193 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5194 | * should be visible now due to the paired locking and barriers implied |
| 5195 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5196 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5197 | * lock on fork. |
| 5198 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5199 | if (use_task_css_set_links) { |
| 5200 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5201 | task_lock(child); |
| 5202 | if (list_empty(&child->cg_list)) |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5203 | list_add(&child->cg_list, &task_css_set(child)->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5204 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5205 | write_unlock(&css_set_lock); |
| 5206 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5207 | |
| 5208 | /* |
| 5209 | * Call ss->fork(). This must happen after @child is linked on |
| 5210 | * css_set; otherwise, @child might change state between ->fork() |
| 5211 | * and addition to css_set. |
| 5212 | */ |
| 5213 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5214 | /* |
| 5215 | * fork/exit callbacks are supported only for builtin |
| 5216 | * subsystems, and the builtin section of the subsys |
| 5217 | * array is immutable, so we don't need to lock the |
| 5218 | * subsys array here. On the other hand, modular section |
| 5219 | * of the array can be freed at module unload, so we |
| 5220 | * can't touch that. |
| 5221 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5222 | for_each_builtin_subsys(ss, i) |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5223 | if (ss->fork) |
| 5224 | ss->fork(child); |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5225 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5226 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5227 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5228 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5229 | * cgroup_exit - detach cgroup from exiting task |
| 5230 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5231 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5232 | * |
| 5233 | * Description: Detach cgroup from @tsk and release it. |
| 5234 | * |
| 5235 | * Note that cgroups marked notify_on_release force every task in |
| 5236 | * them to take the global cgroup_mutex mutex when exiting. |
| 5237 | * This could impact scaling on very large systems. Be reluctant to |
| 5238 | * use notify_on_release cgroups where very high task exit scaling |
| 5239 | * is required on large systems. |
| 5240 | * |
| 5241 | * the_top_cgroup_hack: |
| 5242 | * |
| 5243 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5244 | * |
| 5245 | * We call cgroup_exit() while the task is still competent to |
| 5246 | * handle notify_on_release(), then leave the task attached to the |
| 5247 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5248 | * |
| 5249 | * To do this properly, we would increment the reference count on |
| 5250 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5251 | * code we would add a second cgroup function call, to drop that |
| 5252 | * reference. This would just create an unnecessary hot spot on |
| 5253 | * the top_cgroup reference count, to no avail. |
| 5254 | * |
| 5255 | * Normally, holding a reference to a cgroup without bumping its |
| 5256 | * count is unsafe. The cgroup could go away, or someone could |
| 5257 | * attach us to a different cgroup, decrementing the count on |
| 5258 | * the first cgroup that we never incremented. But in this case, |
| 5259 | * 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] | 5260 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5261 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5262 | */ |
| 5263 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5264 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5265 | struct cgroup_subsys *ss; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5266 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5267 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5268 | |
| 5269 | /* |
| 5270 | * Unlink from the css_set task list if necessary. |
| 5271 | * Optimistically check cg_list before taking |
| 5272 | * css_set_lock |
| 5273 | */ |
| 5274 | if (!list_empty(&tsk->cg_list)) { |
| 5275 | write_lock(&css_set_lock); |
| 5276 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5277 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5278 | write_unlock(&css_set_lock); |
| 5279 | } |
| 5280 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5281 | /* Reassign the task to the init_css_set. */ |
| 5282 | task_lock(tsk); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5283 | cset = task_css_set(tsk); |
| 5284 | RCU_INIT_POINTER(tsk->cgroups, &init_css_set); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5285 | |
| 5286 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5287 | /* |
| 5288 | * fork/exit callbacks are supported only for builtin |
| 5289 | * subsystems, see cgroup_post_fork() for details. |
| 5290 | */ |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5291 | for_each_builtin_subsys(ss, i) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5292 | if (ss->exit) { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5293 | struct cgroup_subsys_state *old_css = cset->subsys[i]; |
| 5294 | struct cgroup_subsys_state *css = task_css(tsk, i); |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5295 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5296 | ss->exit(css, old_css, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5300 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5301 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5302 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5303 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5304 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5305 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5306 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5307 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5308 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5309 | /* |
| 5310 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5311 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5312 | * it now |
| 5313 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5314 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5315 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5316 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5317 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5318 | list_empty(&cgrp->release_list)) { |
| 5319 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5320 | need_schedule_work = 1; |
| 5321 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5322 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5323 | if (need_schedule_work) |
| 5324 | schedule_work(&release_agent_work); |
| 5325 | } |
| 5326 | } |
| 5327 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5328 | /* |
| 5329 | * Notify userspace when a cgroup is released, by running the |
| 5330 | * configured release agent with the name of the cgroup (path |
| 5331 | * relative to the root of cgroup file system) as the argument. |
| 5332 | * |
| 5333 | * Most likely, this user command will try to rmdir this cgroup. |
| 5334 | * |
| 5335 | * This races with the possibility that some other task will be |
| 5336 | * attached to this cgroup before it is removed, or that some other |
| 5337 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5338 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5339 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5340 | * to continue to serve a useful existence. Next time it's released, |
| 5341 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5342 | * |
| 5343 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5344 | * means only wait until the task is successfully execve()'d. The |
| 5345 | * separate release agent task is forked by call_usermodehelper(), |
| 5346 | * then control in this thread returns here, without waiting for the |
| 5347 | * release agent task. We don't bother to wait because the caller of |
| 5348 | * this routine has no use for the exit status of the release agent |
| 5349 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5350 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5351 | static void cgroup_release_agent(struct work_struct *work) |
| 5352 | { |
| 5353 | BUG_ON(work != &release_agent_work); |
| 5354 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5355 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5356 | while (!list_empty(&release_list)) { |
| 5357 | char *argv[3], *envp[3]; |
| 5358 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5359 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5360 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5361 | struct cgroup, |
| 5362 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5363 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5364 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5365 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5366 | if (!pathbuf) |
| 5367 | goto continue_free; |
| 5368 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5369 | goto continue_free; |
| 5370 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5371 | if (!agentbuf) |
| 5372 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5373 | |
| 5374 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5375 | argv[i++] = agentbuf; |
| 5376 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5377 | argv[i] = NULL; |
| 5378 | |
| 5379 | i = 0; |
| 5380 | /* minimal command environment */ |
| 5381 | envp[i++] = "HOME=/"; |
| 5382 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5383 | envp[i] = NULL; |
| 5384 | |
| 5385 | /* Drop the lock while we invoke the usermode helper, |
| 5386 | * since the exec could involve hitting disk and hence |
| 5387 | * be a slow process */ |
| 5388 | mutex_unlock(&cgroup_mutex); |
| 5389 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5390 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5391 | continue_free: |
| 5392 | kfree(pathbuf); |
| 5393 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5394 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5395 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5396 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5397 | mutex_unlock(&cgroup_mutex); |
| 5398 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5399 | |
| 5400 | static int __init cgroup_disable(char *str) |
| 5401 | { |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5402 | struct cgroup_subsys *ss; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5403 | char *token; |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5404 | int i; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5405 | |
| 5406 | while ((token = strsep(&str, ",")) != NULL) { |
| 5407 | if (!*token) |
| 5408 | continue; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5409 | |
Tejun Heo | 30159ec | 2013-06-25 11:53:37 -0700 | [diff] [blame] | 5410 | /* |
| 5411 | * cgroup_disable, being at boot time, can't know about |
| 5412 | * module subsystems, so we don't worry about them. |
| 5413 | */ |
| 5414 | for_each_builtin_subsys(ss, i) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5415 | if (!strcmp(token, ss->name)) { |
| 5416 | ss->disabled = 1; |
| 5417 | printk(KERN_INFO "Disabling %s control group" |
| 5418 | " subsystem\n", ss->name); |
| 5419 | break; |
| 5420 | } |
| 5421 | } |
| 5422 | } |
| 5423 | return 1; |
| 5424 | } |
| 5425 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5426 | |
| 5427 | /* |
| 5428 | * Functons for CSS ID. |
| 5429 | */ |
| 5430 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5431 | /* 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] | 5432 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5433 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5434 | struct css_id *cssid; |
| 5435 | |
| 5436 | /* |
| 5437 | * This css_id() can return correct value when somone has refcnt |
| 5438 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5439 | * it's unchanged until freed. |
| 5440 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5441 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5442 | |
| 5443 | if (cssid) |
| 5444 | return cssid->id; |
| 5445 | return 0; |
| 5446 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5447 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5448 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5449 | /** |
| 5450 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5451 | * @child: the css to be tested. |
| 5452 | * @root: the css supporsed to be an ancestor of the child. |
| 5453 | * |
| 5454 | * 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] | 5455 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5456 | * But, considering usual usage, the csses should be valid objects after test. |
| 5457 | * Assuming that the caller will do some action to the child if this returns |
| 5458 | * returns true, the caller must take "child";s reference count. |
| 5459 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5460 | */ |
| 5461 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5462 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5463 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5464 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5465 | struct css_id *child_id; |
| 5466 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5467 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5468 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5469 | if (!child_id) |
| 5470 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5471 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5472 | if (!root_id) |
| 5473 | return false; |
| 5474 | if (child_id->depth < root_id->depth) |
| 5475 | return false; |
| 5476 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5477 | return false; |
| 5478 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5481 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5482 | { |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5483 | struct css_id *id = rcu_dereference_protected(css->id, true); |
| 5484 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5485 | /* When this is called before css_id initialization, id can be NULL */ |
| 5486 | if (!id) |
| 5487 | return; |
| 5488 | |
| 5489 | BUG_ON(!ss->use_id); |
| 5490 | |
| 5491 | rcu_assign_pointer(id->css, NULL); |
| 5492 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5493 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5494 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5495 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5496 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5497 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5498 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5499 | |
| 5500 | /* |
| 5501 | * This is called by init or create(). Then, calls to this function are |
| 5502 | * always serialized (By cgroup_mutex() at create()). |
| 5503 | */ |
| 5504 | |
| 5505 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5506 | { |
| 5507 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5508 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5509 | |
| 5510 | BUG_ON(!ss->use_id); |
| 5511 | |
| 5512 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5513 | newid = kzalloc(size, GFP_KERNEL); |
| 5514 | if (!newid) |
| 5515 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5516 | |
| 5517 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5518 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5519 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5520 | 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] | 5521 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5522 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5523 | |
| 5524 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5525 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5526 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5527 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5528 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5529 | newid->depth = depth; |
| 5530 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5531 | err_out: |
| 5532 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5533 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5534 | |
| 5535 | } |
| 5536 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5537 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5538 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5539 | { |
| 5540 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5541 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5542 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5543 | idr_init(&ss->idr); |
| 5544 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5545 | newid = get_new_cssid(ss, 0); |
| 5546 | if (IS_ERR(newid)) |
| 5547 | return PTR_ERR(newid); |
| 5548 | |
| 5549 | newid->stack[0] = newid->id; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5550 | RCU_INIT_POINTER(newid->css, rootcss); |
| 5551 | RCU_INIT_POINTER(rootcss->id, newid); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5556 | struct cgroup *child) |
| 5557 | { |
| 5558 | int subsys_id, i, depth = 0; |
| 5559 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5560 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5561 | |
| 5562 | subsys_id = ss->subsys_id; |
| 5563 | parent_css = parent->subsys[subsys_id]; |
| 5564 | child_css = child->subsys[subsys_id]; |
Tejun Heo | a4ea1cc | 2013-06-21 15:52:33 -0700 | [diff] [blame] | 5565 | parent_id = rcu_dereference_protected(parent_css->id, true); |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5566 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5567 | |
| 5568 | child_id = get_new_cssid(ss, depth); |
| 5569 | if (IS_ERR(child_id)) |
| 5570 | return PTR_ERR(child_id); |
| 5571 | |
| 5572 | for (i = 0; i < depth; i++) |
| 5573 | child_id->stack[i] = parent_id->stack[i]; |
| 5574 | child_id->stack[depth] = child_id->id; |
| 5575 | /* |
| 5576 | * child_id->css pointer will be set after this cgroup is available |
| 5577 | * see cgroup_populate_dir() |
| 5578 | */ |
| 5579 | rcu_assign_pointer(child_css->id, child_id); |
| 5580 | |
| 5581 | return 0; |
| 5582 | } |
| 5583 | |
| 5584 | /** |
| 5585 | * css_lookup - lookup css by id |
| 5586 | * @ss: cgroup subsys to be looked into. |
| 5587 | * @id: the id |
| 5588 | * |
| 5589 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5590 | * NULL if not. Should be called under rcu_read_lock() |
| 5591 | */ |
| 5592 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5593 | { |
| 5594 | struct css_id *cssid = NULL; |
| 5595 | |
| 5596 | BUG_ON(!ss->use_id); |
| 5597 | cssid = idr_find(&ss->idr, id); |
| 5598 | |
| 5599 | if (unlikely(!cssid)) |
| 5600 | return NULL; |
| 5601 | |
| 5602 | return rcu_dereference(cssid->css); |
| 5603 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5604 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5605 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5606 | /* |
| 5607 | * get corresponding css from file open on cgroupfs directory |
| 5608 | */ |
| 5609 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5610 | { |
| 5611 | struct cgroup *cgrp; |
| 5612 | struct inode *inode; |
| 5613 | struct cgroup_subsys_state *css; |
| 5614 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5615 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5616 | /* check in cgroup filesystem dir */ |
| 5617 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5618 | return ERR_PTR(-EBADF); |
| 5619 | |
| 5620 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5621 | return ERR_PTR(-EINVAL); |
| 5622 | |
| 5623 | /* get cgroup */ |
| 5624 | cgrp = __d_cgrp(f->f_dentry); |
| 5625 | css = cgrp->subsys[id]; |
| 5626 | return css ? css : ERR_PTR(-ENOENT); |
| 5627 | } |
| 5628 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5629 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5630 | static struct cgroup_subsys_state * |
| 5631 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5632 | { |
| 5633 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5634 | |
| 5635 | if (!css) |
| 5636 | return ERR_PTR(-ENOMEM); |
| 5637 | |
| 5638 | return css; |
| 5639 | } |
| 5640 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5641 | static void debug_css_free(struct cgroup_subsys_state *css) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5642 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5643 | kfree(css); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5644 | } |
| 5645 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5646 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 5647 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5648 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5649 | return cgroup_task_count(css->cgroup); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5652 | static u64 current_css_set_read(struct cgroup_subsys_state *css, |
| 5653 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5654 | { |
| 5655 | return (u64)(unsigned long)current->cgroups; |
| 5656 | } |
| 5657 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5658 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5659 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5660 | { |
| 5661 | u64 count; |
| 5662 | |
| 5663 | rcu_read_lock(); |
Tejun Heo | a8ad805 | 2013-06-21 15:52:04 -0700 | [diff] [blame] | 5664 | count = atomic_read(&task_css_set(current)->refcount); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5665 | rcu_read_unlock(); |
| 5666 | return count; |
| 5667 | } |
| 5668 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5669 | static int current_css_set_cg_links_read(struct cgroup_subsys_state *css, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5670 | struct cftype *cft, |
| 5671 | struct seq_file *seq) |
| 5672 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5673 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5674 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5675 | |
| 5676 | read_lock(&css_set_lock); |
| 5677 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5678 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5679 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5680 | struct cgroup *c = link->cgrp; |
| 5681 | const char *name; |
| 5682 | |
| 5683 | if (c->dentry) |
| 5684 | name = c->dentry->d_name.name; |
| 5685 | else |
| 5686 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5687 | seq_printf(seq, "Root %d group %s\n", |
| 5688 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5689 | } |
| 5690 | rcu_read_unlock(); |
| 5691 | read_unlock(&css_set_lock); |
| 5692 | return 0; |
| 5693 | } |
| 5694 | |
| 5695 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5696 | static int cgroup_css_links_read(struct cgroup_subsys_state *css, |
| 5697 | struct cftype *cft, struct seq_file *seq) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5698 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5699 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5700 | |
| 5701 | read_lock(&css_set_lock); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5702 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5703 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5704 | struct task_struct *task; |
| 5705 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5706 | seq_printf(seq, "css_set %p\n", cset); |
| 5707 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5708 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5709 | seq_puts(seq, " ...\n"); |
| 5710 | break; |
| 5711 | } else { |
| 5712 | seq_printf(seq, " task %d\n", |
| 5713 | task_pid_vnr(task)); |
| 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | read_unlock(&css_set_lock); |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5721 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5722 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 5723 | return test_bit(CGRP_RELEASABLE, &css->cgroup->flags); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | static struct cftype debug_files[] = { |
| 5727 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5728 | .name = "taskcount", |
| 5729 | .read_u64 = debug_taskcount_read, |
| 5730 | }, |
| 5731 | |
| 5732 | { |
| 5733 | .name = "current_css_set", |
| 5734 | .read_u64 = current_css_set_read, |
| 5735 | }, |
| 5736 | |
| 5737 | { |
| 5738 | .name = "current_css_set_refcount", |
| 5739 | .read_u64 = current_css_set_refcount_read, |
| 5740 | }, |
| 5741 | |
| 5742 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5743 | .name = "current_css_set_cg_links", |
| 5744 | .read_seq_string = current_css_set_cg_links_read, |
| 5745 | }, |
| 5746 | |
| 5747 | { |
| 5748 | .name = "cgroup_css_links", |
| 5749 | .read_seq_string = cgroup_css_links_read, |
| 5750 | }, |
| 5751 | |
| 5752 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5753 | .name = "releasable", |
| 5754 | .read_u64 = releasable_read, |
| 5755 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5756 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5757 | { } /* terminate */ |
| 5758 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5759 | |
| 5760 | struct cgroup_subsys debug_subsys = { |
| 5761 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5762 | .css_alloc = debug_css_alloc, |
| 5763 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5764 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5765 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5766 | }; |
| 5767 | #endif /* CONFIG_CGROUP_DEBUG */ |