Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2 | * Generic process-grouping system. |
| 3 | * |
| 4 | * Based originally on the cpuset system, extracted by Paul Menage |
| 5 | * Copyright (C) 2006 Google, Inc |
| 6 | * |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 7 | * Notifications support |
| 8 | * Copyright (C) 2009 Nokia Corporation |
| 9 | * Author: Kirill A. Shutemov |
| 10 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 11 | * Copyright notices from the original cpuset code: |
| 12 | * -------------------------------------------------- |
| 13 | * Copyright (C) 2003 BULL SA. |
| 14 | * Copyright (C) 2004-2006 Silicon Graphics, Inc. |
| 15 | * |
| 16 | * Portions derived from Patrick Mochel's sysfs code. |
| 17 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 18 | * |
| 19 | * 2003-10-10 Written by Simon Derr. |
| 20 | * 2003-10-22 Updates by Stephen Hemminger. |
| 21 | * 2004 May-July Rework by Paul Jackson. |
| 22 | * --------------------------------------------------- |
| 23 | * |
| 24 | * This file is subject to the terms and conditions of the GNU General Public |
| 25 | * License. See the file COPYING in the main directory of the Linux |
| 26 | * distribution for more details. |
| 27 | */ |
| 28 | |
| 29 | #include <linux/cgroup.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 30 | #include <linux/cred.h> |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 31 | #include <linux/ctype.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 33 | #include <linux/init_task.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mm.h> |
| 37 | #include <linux/mutex.h> |
| 38 | #include <linux/mount.h> |
| 39 | #include <linux/pagemap.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/sched.h> |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 43 | #include <linux/backing-dev.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/magic.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/string.h> |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 49 | #include <linux/sort.h> |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 50 | #include <linux/kmod.h> |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 51 | #include <linux/module.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 52 | #include <linux/delayacct.h> |
| 53 | #include <linux/cgroupstats.h> |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 54 | #include <linux/hashtable.h> |
Al Viro | 3f8206d | 2008-07-26 03:46:43 -0400 | [diff] [blame] | 55 | #include <linux/namei.h> |
Li Zefan | 096b7fe | 2009-07-29 15:04:04 -0700 | [diff] [blame] | 56 | #include <linux/pid_namespace.h> |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 57 | #include <linux/idr.h> |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 58 | #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */ |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 59 | #include <linux/eventfd.h> |
| 60 | #include <linux/poll.h> |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 61 | #include <linux/flex_array.h> /* used in cgroup_attach_task */ |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 62 | #include <linux/kthread.h> |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 63 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 64 | #include <linux/atomic.h> |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 65 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 66 | /* |
| 67 | * cgroup_mutex is the master lock. Any modification to cgroup or its |
| 68 | * hierarchy must be performed while holding it. |
| 69 | * |
| 70 | * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify |
| 71 | * cgroupfs_root of any cgroup hierarchy - subsys list, flags, |
| 72 | * release_agent_path and so on. Modifying requires both cgroup_mutex and |
| 73 | * cgroup_root_mutex. Readers can acquire either of the two. This is to |
| 74 | * break the following locking order cycle. |
| 75 | * |
| 76 | * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem |
| 77 | * B. namespace_sem -> cgroup_mutex |
| 78 | * |
| 79 | * B happens only through cgroup_show_options() and using cgroup_root_mutex |
| 80 | * breaks it. |
| 81 | */ |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_PROVE_RCU |
| 83 | DEFINE_MUTEX(cgroup_mutex); |
| 84 | EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */ |
| 85 | #else |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 86 | static DEFINE_MUTEX(cgroup_mutex); |
Tejun Heo | 2219449 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 89 | static DEFINE_MUTEX(cgroup_root_mutex); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 90 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Generate an array of cgroup subsystem pointers. At boot time, this is |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 93 | * populated with the built in subsystems, and modular subsystems are |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 94 | * registered after that. The mutable section of this array is protected by |
| 95 | * cgroup_mutex. |
| 96 | */ |
Daniel Wagner | 80f4c87 | 2012-09-12 16:12:06 +0200 | [diff] [blame] | 97 | #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys, |
Daniel Wagner | 5fc0b02 | 2012-09-12 16:12:05 +0200 | [diff] [blame] | 98 | #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option) |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 99 | static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
| 104 | * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the |
| 105 | * subsystems that are otherwise unattached - it never has more than a |
| 106 | * single cgroup, and all tasks are part of that cgroup. |
| 107 | */ |
| 108 | static struct cgroupfs_root rootnode; |
| 109 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 110 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 111 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 112 | */ |
| 113 | struct cfent { |
| 114 | struct list_head node; |
| 115 | struct dentry *dentry; |
| 116 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 117 | |
| 118 | /* file xattrs */ |
| 119 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 123 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 124 | * cgroup_subsys->use_id != 0. |
| 125 | */ |
| 126 | #define CSS_ID_MAX (65535) |
| 127 | struct css_id { |
| 128 | /* |
| 129 | * The css to which this ID points. This pointer is set to valid value |
| 130 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 131 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 132 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 133 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 134 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 135 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 136 | /* |
| 137 | * ID of this css. |
| 138 | */ |
| 139 | unsigned short id; |
| 140 | /* |
| 141 | * Depth in hierarchy which this ID belongs to. |
| 142 | */ |
| 143 | unsigned short depth; |
| 144 | /* |
| 145 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 146 | */ |
| 147 | struct rcu_head rcu_head; |
| 148 | /* |
| 149 | * Hierarchy of CSS ID belongs to. |
| 150 | */ |
| 151 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 152 | }; |
| 153 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 154 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 155 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 156 | */ |
| 157 | struct cgroup_event { |
| 158 | /* |
| 159 | * Cgroup which the event belongs to. |
| 160 | */ |
| 161 | struct cgroup *cgrp; |
| 162 | /* |
| 163 | * Control file which the event associated. |
| 164 | */ |
| 165 | struct cftype *cft; |
| 166 | /* |
| 167 | * eventfd to signal userspace about the event. |
| 168 | */ |
| 169 | struct eventfd_ctx *eventfd; |
| 170 | /* |
| 171 | * Each of these stored in a list by the cgroup. |
| 172 | */ |
| 173 | struct list_head list; |
| 174 | /* |
| 175 | * All fields below needed to unregister event when |
| 176 | * userspace closes eventfd. |
| 177 | */ |
| 178 | poll_table pt; |
| 179 | wait_queue_head_t *wqh; |
| 180 | wait_queue_t wait; |
| 181 | struct work_struct remove; |
| 182 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 183 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 184 | /* The list of hierarchy roots */ |
| 185 | |
| 186 | static LIST_HEAD(roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 187 | static int root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 188 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 191 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 192 | * writes, either for reads. |
| 193 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 194 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 195 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 196 | /* dummytop is a shorthand for the dummy hierarchy's top cgroup */ |
| 197 | #define dummytop (&rootnode.top_cgroup) |
| 198 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 199 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 200 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame^] | 201 | /* |
| 202 | * Assign a monotonically increasing serial number to cgroups. It |
| 203 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 204 | * numbers. Also, as cgroups are always appended to the parent's |
| 205 | * ->children list, it guarantees that sibling cgroups are always sorted in |
| 206 | * the ascending serial number order on the list. |
| 207 | */ |
| 208 | static atomic64_t cgroup_serial_nr_cursor = ATOMIC64_INIT(0); |
| 209 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 210 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 211 | * check for fork/exit handlers to call. This avoids us having to do |
| 212 | * extra work in the fork/exit path if none of the subsystems need to |
| 213 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 214 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 215 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 216 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 217 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 218 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 219 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 220 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 221 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 222 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 223 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 224 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 225 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 228 | /** |
| 229 | * cgroup_is_descendant - test ancestry |
| 230 | * @cgrp: the cgroup to be tested |
| 231 | * @ancestor: possible ancestor of @cgrp |
| 232 | * |
| 233 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 234 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 235 | * and @ancestor are accessible. |
| 236 | */ |
| 237 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 238 | { |
| 239 | while (cgrp) { |
| 240 | if (cgrp == ancestor) |
| 241 | return true; |
| 242 | cgrp = cgrp->parent; |
| 243 | } |
| 244 | return false; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 247 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 248 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 249 | { |
| 250 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 251 | (1 << CGRP_RELEASABLE) | |
| 252 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 253 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 256 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 257 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 258 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 261 | /* |
| 262 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 263 | * an active hierarchy |
| 264 | */ |
| 265 | #define for_each_subsys(_root, _ss) \ |
| 266 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 267 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 268 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 269 | #define for_each_active_root(_root) \ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 270 | list_for_each_entry(_root, &roots, root_list) |
| 271 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 272 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 273 | { |
| 274 | return dentry->d_fsdata; |
| 275 | } |
| 276 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 277 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 278 | { |
| 279 | return dentry->d_fsdata; |
| 280 | } |
| 281 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 282 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 283 | { |
| 284 | return __d_cfe(dentry)->type; |
| 285 | } |
| 286 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 287 | /** |
| 288 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 289 | * @cgrp: the cgroup to be checked for liveness |
| 290 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 291 | * On success, returns true; the mutex should be later unlocked. On |
| 292 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 293 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | { |
| 296 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 297 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 298 | mutex_unlock(&cgroup_mutex); |
| 299 | return false; |
| 300 | } |
| 301 | return true; |
| 302 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 303 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 304 | /* the list of cgroups eligible for automatic release. Protected by |
| 305 | * release_list_lock */ |
| 306 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 307 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 308 | static void cgroup_release_agent(struct work_struct *work); |
| 309 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 310 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 311 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 312 | /* |
| 313 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 314 | * belong to different cgroups on different hierarchies. In the other |
| 315 | * direction, a css_set is naturally associated with multiple cgroups. |
| 316 | * This M:N relationship is represented by the following link structure |
| 317 | * which exists for each association and allows traversing the associations |
| 318 | * from both sides. |
| 319 | */ |
| 320 | struct cgrp_cset_link { |
| 321 | /* the cgroup and css_set this link associates */ |
| 322 | struct cgroup *cgrp; |
| 323 | struct css_set *cset; |
| 324 | |
| 325 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 326 | struct list_head cset_link; |
| 327 | |
| 328 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 329 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | /* The default css_set - used by init and its children prior to any |
| 333 | * hierarchies being mounted. It contains a pointer to the root state |
| 334 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 335 | * reference-counted, to improve performance when child cgroups |
| 336 | * haven't been created. |
| 337 | */ |
| 338 | |
| 339 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 340 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 341 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 342 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 343 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 344 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 345 | /* css_set_lock protects the list of css_set objects, and the |
| 346 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 347 | * due to cgroup_iter_start() */ |
| 348 | static DEFINE_RWLOCK(css_set_lock); |
| 349 | static int css_set_count; |
| 350 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 351 | /* |
| 352 | * hash table for cgroup groups. This improves the performance to find |
| 353 | * an existing css_set. This hash doesn't (currently) take into |
| 354 | * account cgroups in empty hierarchies. |
| 355 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 356 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 357 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 358 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 359 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 360 | { |
| 361 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 362 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 363 | |
| 364 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 365 | key += (unsigned long)css[i]; |
| 366 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 367 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 368 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 371 | /* We don't maintain the lists running through each css_set to its |
| 372 | * task until after the first call to cgroup_iter_start(). This |
| 373 | * reduces the fork()/exit() overhead for people who have cgroups |
| 374 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 375 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 376 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 377 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 378 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 379 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 380 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 381 | /* |
| 382 | * Ensure that the refcount doesn't hit zero while any readers |
| 383 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 384 | * rwlock |
| 385 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 386 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 387 | return; |
| 388 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 389 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 390 | write_unlock(&css_set_lock); |
| 391 | return; |
| 392 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 393 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 394 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 395 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 396 | css_set_count--; |
| 397 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 398 | 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] | 399 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 400 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 401 | list_del(&link->cset_link); |
| 402 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 403 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 404 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 405 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 406 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 407 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 408 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 409 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 410 | |
| 411 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 412 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 413 | |
| 414 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 415 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
| 419 | * refcounted get/put for css_set objects |
| 420 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 421 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 422 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 423 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 426 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 427 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 428 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 431 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 432 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 433 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 436 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 437 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 438 | * @cset: candidate css_set being tested |
| 439 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 440 | * @new_cgrp: cgroup that's being entered by the task |
| 441 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 442 | * |
| 443 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 444 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 445 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 446 | static bool compare_css_sets(struct css_set *cset, |
| 447 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 448 | struct cgroup *new_cgrp, |
| 449 | struct cgroup_subsys_state *template[]) |
| 450 | { |
| 451 | struct list_head *l1, *l2; |
| 452 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 453 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 454 | /* Not all subsystems matched */ |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Compare cgroup pointers in order to distinguish between |
| 460 | * different cgroups in heirarchies with no subsystems. We |
| 461 | * could get by with just this check alone (and skip the |
| 462 | * memcmp above) but on most setups the memcmp check will |
| 463 | * avoid the need for this more expensive check on almost all |
| 464 | * candidates. |
| 465 | */ |
| 466 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 467 | l1 = &cset->cgrp_links; |
| 468 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 469 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 470 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 471 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 472 | |
| 473 | l1 = l1->next; |
| 474 | l2 = l2->next; |
| 475 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 476 | if (l1 == &cset->cgrp_links) { |
| 477 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 478 | break; |
| 479 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 480 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 481 | } |
| 482 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 483 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 484 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 485 | cgrp1 = link1->cgrp; |
| 486 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 487 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 488 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * If this hierarchy is the hierarchy of the cgroup |
| 492 | * that's changing, then we need to check that this |
| 493 | * css_set points to the new cgroup; if it's any other |
| 494 | * hierarchy, then this css_set should point to the |
| 495 | * same cgroup as the old css_set. |
| 496 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 497 | if (cgrp1->root == new_cgrp->root) { |
| 498 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 499 | return false; |
| 500 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 501 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 509 | * find_existing_css_set() is a helper for |
| 510 | * find_css_set(), and checks to see whether an existing |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 511 | * css_set is suitable. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 512 | * |
| 513 | * oldcg: the cgroup group that we're using before the cgroup |
| 514 | * transition |
| 515 | * |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 516 | * cgrp: the cgroup that we're moving into |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 517 | * |
| 518 | * template: location in which to build the desired set of subsystem |
| 519 | * state objects for the new cgroup group |
| 520 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 521 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 522 | struct cgroup *cgrp, |
| 523 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 524 | { |
| 525 | int i; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 526 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 527 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 528 | unsigned long key; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 529 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 530 | /* |
| 531 | * Build the set of subsystem state objects that we want to see in the |
| 532 | * new css_set. while subsystems can change globally, the entries here |
| 533 | * won't change, so no need for locking. |
| 534 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 536 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 537 | /* Subsystem is in this hierarchy. So we want |
| 538 | * the subsystem state from the new |
| 539 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 540 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 541 | } else { |
| 542 | /* Subsystem is not in this hierarchy, so we |
| 543 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 544 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 548 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 549 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 550 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 551 | continue; |
| 552 | |
| 553 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 554 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 555 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 556 | |
| 557 | /* No existing cgroup group matched */ |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 561 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 562 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 563 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 564 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 565 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 566 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 567 | kfree(link); |
| 568 | } |
| 569 | } |
| 570 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 571 | /** |
| 572 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 573 | * @count: the number of links to allocate |
| 574 | * @tmp_links: list_head the allocated links are put on |
| 575 | * |
| 576 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 577 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 578 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 579 | 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] | 580 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 581 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 582 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | |
| 584 | INIT_LIST_HEAD(tmp_links); |
| 585 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 586 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 587 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 588 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 589 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 590 | return -ENOMEM; |
| 591 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 592 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 597 | /** |
| 598 | * 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] | 599 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 600 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 601 | * @cgrp: the destination cgroup |
| 602 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 603 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 604 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 605 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 607 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 608 | BUG_ON(list_empty(tmp_links)); |
| 609 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 610 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 611 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 612 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Always add links to the tail of the list so that the list |
| 615 | * is sorted by order of hierarchy creation |
| 616 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 617 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 620 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 621 | * find_css_set() takes an existing cgroup group and a |
| 622 | * cgroup object, and returns a css_set object that's |
| 623 | * equivalent to the old group, but with the given cgroup |
| 624 | * substituted into the appropriate hierarchy. Must be called with |
| 625 | * cgroup_mutex held |
| 626 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 627 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 628 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 629 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 630 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 631 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT]; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 632 | struct list_head tmp_links; |
| 633 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 634 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 635 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 636 | /* First see if we already have a cgroup group that matches |
| 637 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 638 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 639 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 640 | if (cset) |
| 641 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 642 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 643 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 644 | if (cset) |
| 645 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 646 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 647 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 648 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 649 | return NULL; |
| 650 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 651 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
| 652 | if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 653 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 657 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 658 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 659 | INIT_LIST_HEAD(&cset->tasks); |
| 660 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 661 | |
| 662 | /* Copy the set of subsystem state objects generated in |
| 663 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 664 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 665 | |
| 666 | write_lock(&css_set_lock); |
| 667 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 668 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 669 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 670 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 671 | if (c->root == cgrp->root) |
| 672 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 673 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 674 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 676 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 677 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 678 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 679 | |
| 680 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 681 | key = css_set_hash(cset->subsys); |
| 682 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 683 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 684 | write_unlock(&css_set_lock); |
| 685 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 686 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 689 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 690 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 691 | * called with cgroup_mutex held. |
| 692 | */ |
| 693 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 694 | struct cgroupfs_root *root) |
| 695 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 696 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 697 | struct cgroup *res = NULL; |
| 698 | |
| 699 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 700 | read_lock(&css_set_lock); |
| 701 | /* |
| 702 | * No need to lock the task - since we hold cgroup_mutex the |
| 703 | * task can't change groups, so the only thing that can happen |
| 704 | * is that it exits and its css is set back to init_css_set. |
| 705 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 706 | cset = task->cgroups; |
| 707 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 708 | res = &root->top_cgroup; |
| 709 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 710 | struct cgrp_cset_link *link; |
| 711 | |
| 712 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 713 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 714 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 715 | if (c->root == root) { |
| 716 | res = c; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | read_unlock(&css_set_lock); |
| 722 | BUG_ON(!res); |
| 723 | return res; |
| 724 | } |
| 725 | |
| 726 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 727 | * There is one global cgroup mutex. We also require taking |
| 728 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 729 | * See "The task_lock() exception", at the end of this comment. |
| 730 | * |
| 731 | * A task must hold cgroup_mutex to modify cgroups. |
| 732 | * |
| 733 | * Any task can increment and decrement the count field without lock. |
| 734 | * So in general, code holding cgroup_mutex can't rely on the count |
| 735 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 736 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 737 | * means that no tasks are currently attached, therefore there is no |
| 738 | * way a task attached to that cgroup can fork (the other way to |
| 739 | * increment the count). So code holding cgroup_mutex can safely |
| 740 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 741 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 742 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 743 | * needs that mutex. |
| 744 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 745 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 746 | * (usually) take cgroup_mutex. These are the two most performance |
| 747 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 748 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 749 | * 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] | 750 | * to the release agent with the name of the cgroup (path relative to |
| 751 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 752 | * |
| 753 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 754 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 755 | * tasks in the system use _some_ cgroup, and since there is always at |
| 756 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 757 | * always has either children cgroups and/or using tasks. So we don't |
| 758 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 759 | * |
| 760 | * The task_lock() exception |
| 761 | * |
| 762 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 763 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 764 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 765 | * several performance critical places that need to reference |
| 766 | * task->cgroup without the expense of grabbing a system global |
| 767 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 768 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 769 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 770 | * the task_struct routinely used for such matters. |
| 771 | * |
| 772 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 773 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 774 | */ |
| 775 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 776 | /* |
| 777 | * A couple of forward declarations required, due to cyclic reference loop: |
| 778 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 779 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 780 | * -> cgroup_mkdir. |
| 781 | */ |
| 782 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 783 | 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] | 784 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 785 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 786 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 787 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 788 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 789 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 790 | |
| 791 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 792 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 793 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 794 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 795 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 796 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 797 | struct cgroup *parent, struct cgroup *child); |
| 798 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 799 | 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] | 800 | { |
| 801 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 802 | |
| 803 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 804 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 805 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 806 | inode->i_uid = current_fsuid(); |
| 807 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 808 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 809 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 810 | } |
| 811 | return inode; |
| 812 | } |
| 813 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 814 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 815 | { |
| 816 | struct cgroup_name *name; |
| 817 | |
| 818 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 819 | if (!name) |
| 820 | return NULL; |
| 821 | strcpy(name->name, dentry->d_name.name); |
| 822 | return name; |
| 823 | } |
| 824 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 825 | static void cgroup_free_fn(struct work_struct *work) |
| 826 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 827 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 828 | struct cgroup_subsys *ss; |
| 829 | |
| 830 | mutex_lock(&cgroup_mutex); |
| 831 | /* |
| 832 | * Release the subsystem state objects. |
| 833 | */ |
| 834 | for_each_subsys(cgrp->root, ss) |
| 835 | ss->css_free(cgrp); |
| 836 | |
| 837 | cgrp->root->number_of_cgroups--; |
| 838 | mutex_unlock(&cgroup_mutex); |
| 839 | |
| 840 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 841 | * We get a ref to the parent's dentry, and put the ref when |
| 842 | * this cgroup is being freed, so it's guaranteed that the |
| 843 | * parent won't be destroyed before its children. |
| 844 | */ |
| 845 | dput(cgrp->parent->dentry); |
| 846 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 847 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 848 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 849 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 850 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 851 | * created the cgroup. This will free cgrp->root, if we are |
| 852 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 853 | */ |
| 854 | deactivate_super(cgrp->root->sb); |
| 855 | |
| 856 | /* |
| 857 | * if we're getting rid of the cgroup, refcount should ensure |
| 858 | * that there are no pidlists left. |
| 859 | */ |
| 860 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 861 | |
| 862 | simple_xattrs_free(&cgrp->xattrs); |
| 863 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 864 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 865 | kfree(cgrp); |
| 866 | } |
| 867 | |
| 868 | static void cgroup_free_rcu(struct rcu_head *head) |
| 869 | { |
| 870 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 871 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 872 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 873 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 874 | } |
| 875 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 876 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 877 | { |
| 878 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 879 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 880 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 881 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 882 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 883 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 884 | } else { |
| 885 | struct cfent *cfe = __d_cfe(dentry); |
| 886 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 887 | |
| 888 | WARN_ONCE(!list_empty(&cfe->node) && |
| 889 | cgrp != &cgrp->root->top_cgroup, |
| 890 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 891 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 892 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 893 | } |
| 894 | iput(inode); |
| 895 | } |
| 896 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 897 | static int cgroup_delete(const struct dentry *d) |
| 898 | { |
| 899 | return 1; |
| 900 | } |
| 901 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 902 | static void remove_dir(struct dentry *d) |
| 903 | { |
| 904 | struct dentry *parent = dget(d->d_parent); |
| 905 | |
| 906 | d_delete(d); |
| 907 | simple_rmdir(parent->d_inode, d); |
| 908 | dput(parent); |
| 909 | } |
| 910 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 911 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 912 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 913 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 914 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 915 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 916 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 917 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 918 | /* |
| 919 | * If we're doing cleanup due to failure of cgroup_create(), |
| 920 | * the corresponding @cfe may not exist. |
| 921 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 922 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 923 | struct dentry *d = cfe->dentry; |
| 924 | |
| 925 | if (cft && cfe->type != cft) |
| 926 | continue; |
| 927 | |
| 928 | dget(d); |
| 929 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 930 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 931 | list_del_init(&cfe->node); |
| 932 | dput(d); |
| 933 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 934 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 935 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 938 | /** |
| 939 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 940 | * @dir: directory containing the files |
| 941 | * @base_files: true if the base files should be removed |
| 942 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 943 | */ |
| 944 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 945 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 946 | { |
| 947 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 948 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 949 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 950 | for_each_subsys(cgrp->root, ss) { |
| 951 | struct cftype_set *set; |
| 952 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 953 | continue; |
| 954 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 955 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 956 | } |
| 957 | if (base_files) { |
| 958 | while (!list_empty(&cgrp->files)) |
| 959 | cgroup_rm_file(cgrp, NULL); |
| 960 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | * NOTE : the dentry must have been dget()'ed |
| 965 | */ |
| 966 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 967 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 968 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 969 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 970 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 971 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 972 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 973 | parent = dentry->d_parent; |
| 974 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 975 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 977 | spin_unlock(&dentry->d_lock); |
| 978 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 979 | remove_dir(dentry); |
| 980 | } |
| 981 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 982 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 983 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 984 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 985 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 986 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 987 | static int rebind_subsystems(struct cgroupfs_root *root, |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 988 | unsigned long final_subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 989 | { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 990 | unsigned long added_mask, removed_mask; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 991 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 992 | int i; |
| 993 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 994 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 995 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 996 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 997 | removed_mask = root->actual_subsys_mask & ~final_subsys_mask; |
| 998 | added_mask = final_subsys_mask & ~root->actual_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 999 | /* Check that any added subsystems are currently free */ |
| 1000 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 1001 | unsigned long bit = 1UL << i; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1002 | struct cgroup_subsys *ss = subsys[i]; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1003 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1004 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1005 | /* |
| 1006 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1007 | * parse_cgroupfs_options should catch that case and refcounts |
| 1008 | * ensure that subsystems won't disappear once selected. |
| 1009 | */ |
| 1010 | BUG_ON(ss == NULL); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1011 | if (ss->root != &rootnode) { |
| 1012 | /* Subsystem isn't free */ |
| 1013 | return -EBUSY; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* Currently we don't handle adding/removing subsystems when |
| 1018 | * any child cgroups exist. This is theoretically supportable |
| 1019 | * but involves complex error handling, so it's being left until |
| 1020 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1021 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1022 | return -EBUSY; |
| 1023 | |
| 1024 | /* Process each subsystem */ |
| 1025 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1026 | struct cgroup_subsys *ss = subsys[i]; |
| 1027 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1028 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1029 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1030 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1031 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1032 | BUG_ON(!dummytop->subsys[i]); |
| 1033 | BUG_ON(dummytop->subsys[i]->cgroup != dummytop); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1034 | cgrp->subsys[i] = dummytop->subsys[i]; |
| 1035 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1036 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1037 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1038 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1039 | ss->bind(cgrp); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1040 | /* refcount was already taken, and we're keeping it */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1041 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1042 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1043 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1044 | BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]); |
| 1045 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1046 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1047 | ss->bind(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1048 | dummytop->subsys[i]->cgroup = dummytop; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1049 | cgrp->subsys[i] = NULL; |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1050 | subsys[i]->root = &rootnode; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1051 | list_move(&ss->sibling, &rootnode.subsys_list); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1052 | /* subsystem is now free - drop reference on module */ |
| 1053 | module_put(ss->module); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1054 | } else if (bit & final_subsys_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1055 | /* Subsystem state should already exist */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1056 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1057 | BUG_ON(!cgrp->subsys[i]); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1058 | /* |
| 1059 | * a refcount was taken, but we already had one, so |
| 1060 | * drop the extra reference. |
| 1061 | */ |
| 1062 | module_put(ss->module); |
| 1063 | #ifdef CONFIG_MODULE_UNLOAD |
| 1064 | BUG_ON(ss->module && !module_refcount(ss->module)); |
| 1065 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | /* Subsystem state shouldn't exist */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1068 | BUG_ON(cgrp->subsys[i]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1069 | } |
| 1070 | } |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1071 | root->subsys_mask = root->actual_subsys_mask = final_subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1076 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1077 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1078 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1079 | struct cgroup_subsys *ss; |
| 1080 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1081 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1082 | for_each_subsys(root, ss) |
| 1083 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1084 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1085 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1086 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1087 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1088 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1089 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1090 | if (strlen(root->release_agent_path)) |
| 1091 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1092 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1093 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1094 | if (strlen(root->name)) |
| 1095 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1096 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1101 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1102 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1103 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1104 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1105 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1106 | /* User explicitly requested empty subsystem */ |
| 1107 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1108 | |
| 1109 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1110 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1111 | }; |
| 1112 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1113 | /* |
| 1114 | * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1115 | * with cgroup_mutex held to protect the subsys[] array. This function takes |
| 1116 | * refcounts on subsystems to be used, unless it returns error, in which case |
| 1117 | * no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1118 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1119 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1120 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1121 | char *token, *o = data; |
| 1122 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1123 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1124 | int i; |
| 1125 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1126 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1127 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1128 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1129 | #ifdef CONFIG_CPUSETS |
| 1130 | mask = ~(1UL << cpuset_subsys_id); |
| 1131 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1132 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1133 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1134 | |
| 1135 | while ((token = strsep(&o, ",")) != NULL) { |
| 1136 | if (!*token) |
| 1137 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1138 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1139 | /* Explicitly have no subsystems */ |
| 1140 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1141 | continue; |
| 1142 | } |
| 1143 | if (!strcmp(token, "all")) { |
| 1144 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1145 | if (one_ss) |
| 1146 | return -EINVAL; |
| 1147 | all_ss = true; |
| 1148 | continue; |
| 1149 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1150 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1151 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1152 | continue; |
| 1153 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1154 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1155 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1156 | continue; |
| 1157 | } |
| 1158 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1159 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1160 | continue; |
| 1161 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1162 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1163 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1164 | continue; |
| 1165 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1166 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1167 | /* Specifying two release agents is forbidden */ |
| 1168 | if (opts->release_agent) |
| 1169 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1170 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1171 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1172 | if (!opts->release_agent) |
| 1173 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1174 | continue; |
| 1175 | } |
| 1176 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1177 | const char *name = token + 5; |
| 1178 | /* Can't specify an empty name */ |
| 1179 | if (!strlen(name)) |
| 1180 | return -EINVAL; |
| 1181 | /* Must match [\w.-]+ */ |
| 1182 | for (i = 0; i < strlen(name); i++) { |
| 1183 | char c = name[i]; |
| 1184 | if (isalnum(c)) |
| 1185 | continue; |
| 1186 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1187 | continue; |
| 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | /* Specifying two names is forbidden */ |
| 1191 | if (opts->name) |
| 1192 | return -EINVAL; |
| 1193 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1194 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1195 | GFP_KERNEL); |
| 1196 | if (!opts->name) |
| 1197 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1198 | |
| 1199 | continue; |
| 1200 | } |
| 1201 | |
| 1202 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1203 | struct cgroup_subsys *ss = subsys[i]; |
| 1204 | if (ss == NULL) |
| 1205 | continue; |
| 1206 | if (strcmp(token, ss->name)) |
| 1207 | continue; |
| 1208 | if (ss->disabled) |
| 1209 | continue; |
| 1210 | |
| 1211 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1212 | if (all_ss) |
| 1213 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1214 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1215 | one_ss = true; |
| 1216 | |
| 1217 | break; |
| 1218 | } |
| 1219 | if (i == CGROUP_SUBSYS_COUNT) |
| 1220 | return -ENOENT; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1225 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1226 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1227 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1228 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1229 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 1230 | struct cgroup_subsys *ss = subsys[i]; |
| 1231 | if (ss == NULL) |
| 1232 | continue; |
| 1233 | if (ss->disabled) |
| 1234 | continue; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1235 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1239 | /* Consistency checks */ |
| 1240 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1241 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1242 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1243 | |
| 1244 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1245 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
| 1249 | if (opts->cpuset_clone_children) { |
| 1250 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1255 | /* |
| 1256 | * Option noprefix was introduced just for backward compatibility |
| 1257 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1258 | * the cpuset subsystem. |
| 1259 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1260 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1261 | return -EINVAL; |
| 1262 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1263 | |
| 1264 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1265 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1266 | return -EINVAL; |
| 1267 | |
| 1268 | /* |
| 1269 | * We either have to specify by name or by subsystems. (So all |
| 1270 | * empty hierarchies must have a name). |
| 1271 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1272 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1273 | return -EINVAL; |
| 1274 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1275 | /* |
| 1276 | * Grab references on all the modules we'll need, so the subsystems |
| 1277 | * don't dance around before rebind_subsystems attaches them. This may |
| 1278 | * take duplicate reference counts on a subsystem that's already used, |
| 1279 | * but rebind_subsystems handles this case. |
| 1280 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1281 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1282 | unsigned long bit = 1UL << i; |
| 1283 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1284 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1285 | continue; |
| 1286 | if (!try_module_get(subsys[i]->module)) { |
| 1287 | module_pin_failed = true; |
| 1288 | break; |
| 1289 | } |
| 1290 | } |
| 1291 | if (module_pin_failed) { |
| 1292 | /* |
| 1293 | * oops, one of the modules was going away. this means that we |
| 1294 | * raced with a module_delete call, and to the user this is |
| 1295 | * essentially a "subsystem doesn't exist" case. |
| 1296 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1297 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1298 | /* drop refcounts only on the ones we took */ |
| 1299 | unsigned long bit = 1UL << i; |
| 1300 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1301 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1302 | continue; |
| 1303 | module_put(subsys[i]->module); |
| 1304 | } |
| 1305 | return -ENOENT; |
| 1306 | } |
| 1307 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1311 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1312 | { |
| 1313 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1314 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1315 | unsigned long bit = 1UL << i; |
| 1316 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1317 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1318 | continue; |
| 1319 | module_put(subsys[i]->module); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1323 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1324 | { |
| 1325 | int ret = 0; |
| 1326 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1327 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1328 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1329 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1330 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1331 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1332 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1333 | return -EINVAL; |
| 1334 | } |
| 1335 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1336 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1337 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1338 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1339 | |
| 1340 | /* See what subsystems are wanted */ |
| 1341 | ret = parse_cgroupfs_options(data, &opts); |
| 1342 | if (ret) |
| 1343 | goto out_unlock; |
| 1344 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1345 | if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1346 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1347 | task_tgid_nr(current), current->comm); |
| 1348 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1349 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1350 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1351 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1352 | /* Don't allow flags or name to change at remount */ |
| 1353 | if (opts.flags != root->flags || |
| 1354 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1355 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1356 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1357 | goto out_unlock; |
| 1358 | } |
| 1359 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1360 | /* |
| 1361 | * Clear out the files of subsystems that should be removed, do |
| 1362 | * this before rebind_subsystems, since rebind_subsystems may |
| 1363 | * change this hierarchy's subsys_list. |
| 1364 | */ |
| 1365 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1366 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1367 | ret = rebind_subsystems(root, opts.subsys_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1368 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1369 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1370 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1371 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1372 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1373 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1374 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1375 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1376 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1377 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1378 | if (opts.release_agent) |
| 1379 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1380 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1381 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1382 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1383 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1384 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1385 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1386 | return ret; |
| 1387 | } |
| 1388 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1389 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1390 | .statfs = simple_statfs, |
| 1391 | .drop_inode = generic_delete_inode, |
| 1392 | .show_options = cgroup_show_options, |
| 1393 | .remount_fs = cgroup_remount, |
| 1394 | }; |
| 1395 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1396 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1397 | { |
| 1398 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1399 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1400 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1401 | INIT_LIST_HEAD(&cgrp->cset_links); |
Tejun Heo | 2243076 | 2012-11-19 08:13:35 -0800 | [diff] [blame] | 1402 | INIT_LIST_HEAD(&cgrp->allcg_node); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1403 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1404 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1405 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1406 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1407 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1408 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1409 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1410 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1411 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1412 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1413 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1414 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1415 | INIT_LIST_HEAD(&root->subsys_list); |
| 1416 | INIT_LIST_HEAD(&root->root_list); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1417 | INIT_LIST_HEAD(&root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1418 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1419 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1420 | cgrp->name = &root_cgroup_name; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1421 | init_cgroup_housekeeping(cgrp); |
Li Zhong | fddfb02 | 2012-11-28 17:15:21 +0800 | [diff] [blame] | 1422 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1425 | static int cgroup_init_root_id(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1426 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1427 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1428 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1429 | lockdep_assert_held(&cgroup_mutex); |
| 1430 | lockdep_assert_held(&cgroup_root_mutex); |
| 1431 | |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1432 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL); |
| 1433 | if (id < 0) |
| 1434 | return id; |
| 1435 | |
| 1436 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1441 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1442 | lockdep_assert_held(&cgroup_mutex); |
| 1443 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1444 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1445 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1446 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1447 | root->hierarchy_id = 0; |
| 1448 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1451 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1452 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1453 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1454 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1455 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1456 | /* If we asked for a name then it must match */ |
| 1457 | if (opts->name && strcmp(opts->name, root->name)) |
| 1458 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1459 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * If we asked for subsystems (or explicitly for no |
| 1462 | * subsystems) then they must match |
| 1463 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1464 | if ((opts->subsys_mask || opts->none) |
| 1465 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1466 | return 0; |
| 1467 | |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1471 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1472 | { |
| 1473 | struct cgroupfs_root *root; |
| 1474 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1475 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1476 | return NULL; |
| 1477 | |
| 1478 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1479 | if (!root) |
| 1480 | return ERR_PTR(-ENOMEM); |
| 1481 | |
| 1482 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1483 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1484 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1485 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1486 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1487 | if (opts->release_agent) |
| 1488 | strcpy(root->release_agent_path, opts->release_agent); |
| 1489 | if (opts->name) |
| 1490 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1491 | if (opts->cpuset_clone_children) |
| 1492 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1493 | return root; |
| 1494 | } |
| 1495 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1496 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1497 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1498 | if (root) { |
| 1499 | /* hierarhcy ID shoulid already have been released */ |
| 1500 | WARN_ON_ONCE(root->hierarchy_id); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1501 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1502 | ida_destroy(&root->cgroup_ida); |
| 1503 | kfree(root); |
| 1504 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1507 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1508 | { |
| 1509 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1510 | struct cgroup_sb_opts *opts = data; |
| 1511 | |
| 1512 | /* If we don't have a new root, we can't set up a new sb */ |
| 1513 | if (!opts->new_root) |
| 1514 | return -EINVAL; |
| 1515 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1516 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1517 | |
| 1518 | ret = set_anon_super(sb, NULL); |
| 1519 | if (ret) |
| 1520 | return ret; |
| 1521 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1522 | sb->s_fs_info = opts->new_root; |
| 1523 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1524 | |
| 1525 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1526 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1527 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1528 | sb->s_op = &cgroup_ops; |
| 1529 | |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1534 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1535 | static const struct dentry_operations cgroup_dops = { |
| 1536 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1537 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1538 | }; |
| 1539 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1540 | struct inode *inode = |
| 1541 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1542 | |
| 1543 | if (!inode) |
| 1544 | return -ENOMEM; |
| 1545 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1546 | inode->i_fop = &simple_dir_operations; |
| 1547 | inode->i_op = &cgroup_dir_inode_operations; |
| 1548 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1549 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1550 | sb->s_root = d_make_root(inode); |
| 1551 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1552 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1553 | /* for everything else we want ->d_op set */ |
| 1554 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
| 1557 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1558 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1559 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1560 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1561 | { |
| 1562 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1563 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 | int ret = 0; |
| 1565 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1566 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1567 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1570 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1571 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1572 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1573 | if (ret) |
| 1574 | goto out_err; |
| 1575 | |
| 1576 | /* |
| 1577 | * Allocate a new cgroup root. We may not need it if we're |
| 1578 | * reusing an existing hierarchy. |
| 1579 | */ |
| 1580 | new_root = cgroup_root_from_opts(&opts); |
| 1581 | if (IS_ERR(new_root)) { |
| 1582 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1583 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1584 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1585 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1586 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1587 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1588 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1590 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1591 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1592 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1595 | root = sb->s_fs_info; |
| 1596 | BUG_ON(!root); |
| 1597 | if (root == opts.new_root) { |
| 1598 | /* We used the new root structure, so this is a new hierarchy */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1599 | struct list_head tmp_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1600 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1601 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1602 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1603 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1604 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1605 | |
| 1606 | BUG_ON(sb->s_root != NULL); |
| 1607 | |
| 1608 | ret = cgroup_get_rootdir(sb); |
| 1609 | if (ret) |
| 1610 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1611 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1613 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1614 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1615 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1616 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1617 | /* Check for name clashes with existing mounts */ |
| 1618 | ret = -EBUSY; |
| 1619 | if (strlen(root->name)) |
| 1620 | for_each_active_root(existing_root) |
| 1621 | if (!strcmp(existing_root->name, root->name)) |
| 1622 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1623 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1624 | /* |
| 1625 | * We're accessing css_set_count without locking |
| 1626 | * css_set_lock here, but that's OK - it can only be |
| 1627 | * increased by someone holding cgroup_lock, and |
| 1628 | * that's us. The worst that can happen is that we |
| 1629 | * have some link structures left over |
| 1630 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1631 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1632 | if (ret) |
| 1633 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1634 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1635 | ret = cgroup_init_root_id(root); |
| 1636 | if (ret) |
| 1637 | goto unlock_drop; |
| 1638 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1639 | ret = rebind_subsystems(root, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1640 | if (ret == -EBUSY) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1641 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1642 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1643 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1644 | /* |
| 1645 | * There must be no failure case after here, since rebinding |
| 1646 | * takes care of subsystems' refcounts, which are explicitly |
| 1647 | * dropped in the failure exit path. |
| 1648 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1649 | |
| 1650 | /* EBUSY should be the only error here */ |
| 1651 | BUG_ON(ret); |
| 1652 | |
| 1653 | list_add(&root->root_list, &roots); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1654 | root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1655 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1656 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1657 | root->top_cgroup.dentry = sb->s_root; |
| 1658 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1659 | /* Link the top cgroup in this hierarchy into all |
| 1660 | * the css_set objects */ |
| 1661 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1662 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1663 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1664 | write_unlock(&css_set_lock); |
| 1665 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1666 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1667 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1668 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1669 | BUG_ON(root->number_of_cgroups != 1); |
| 1670 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1671 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1672 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1673 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1674 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1675 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1676 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1677 | } else { |
| 1678 | /* |
| 1679 | * We re-used an existing hierarchy - the new root (if |
| 1680 | * any) is not needed |
| 1681 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1682 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1683 | |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1684 | if (root->flags != opts.flags) { |
| 1685 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1686 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1687 | ret = -EINVAL; |
| 1688 | goto drop_new_super; |
| 1689 | } else { |
| 1690 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1691 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1694 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1695 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1698 | kfree(opts.release_agent); |
| 1699 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1700 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1701 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1702 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1703 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1704 | mutex_unlock(&cgroup_root_mutex); |
| 1705 | mutex_unlock(&cgroup_mutex); |
| 1706 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1707 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1708 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1709 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1710 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1711 | out_err: |
| 1712 | kfree(opts.release_agent); |
| 1713 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1714 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1718 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1719 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1720 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1721 | int ret; |
| 1722 | |
| 1723 | BUG_ON(!root); |
| 1724 | |
| 1725 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1726 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1727 | |
| 1728 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1729 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* Rebind all subsystems back to the default hierarchy */ |
| 1732 | ret = rebind_subsystems(root, 0); |
| 1733 | /* Shouldn't be able to fail ... */ |
| 1734 | BUG_ON(ret); |
| 1735 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1736 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1737 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1738 | * root cgroup |
| 1739 | */ |
| 1740 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1741 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1742 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1743 | list_del(&link->cset_link); |
| 1744 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1745 | kfree(link); |
| 1746 | } |
| 1747 | write_unlock(&css_set_lock); |
| 1748 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1749 | if (!list_empty(&root->root_list)) { |
| 1750 | list_del(&root->root_list); |
| 1751 | root_count--; |
| 1752 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1753 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1754 | cgroup_exit_root_id(root); |
| 1755 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1756 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | mutex_unlock(&cgroup_mutex); |
| 1758 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1759 | simple_xattrs_free(&cgrp->xattrs); |
| 1760 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1761 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1762 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | static struct file_system_type cgroup_fs_type = { |
| 1766 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1767 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1768 | .kill_sb = cgroup_kill_sb, |
| 1769 | }; |
| 1770 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1771 | static struct kobject *cgroup_kobj; |
| 1772 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1773 | /** |
| 1774 | * cgroup_path - generate the path of a cgroup |
| 1775 | * @cgrp: the cgroup in question |
| 1776 | * @buf: the buffer to write the path into |
| 1777 | * @buflen: the length of the buffer |
| 1778 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1779 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1780 | * |
| 1781 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1782 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1783 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1784 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1786 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1787 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1788 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1789 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1790 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1791 | if (!cgrp->parent) { |
| 1792 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1793 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1794 | return 0; |
| 1795 | } |
| 1796 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1797 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1798 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1799 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1800 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1801 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1802 | const char *name = cgroup_name(cgrp); |
| 1803 | int len; |
| 1804 | |
| 1805 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1806 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1807 | goto out; |
| 1808 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1809 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1810 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1811 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1812 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1813 | |
| 1814 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1815 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1816 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1817 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1818 | out: |
| 1819 | rcu_read_unlock(); |
| 1820 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1821 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1822 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1823 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1824 | /** |
| 1825 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1826 | * @task: target task |
| 1827 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1828 | * @buf: the buffer to write the path into |
| 1829 | * @buflen: the length of the buffer |
| 1830 | * |
| 1831 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1832 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1833 | * be used inside locks used by cgroup controller callbacks. |
| 1834 | */ |
| 1835 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1836 | char *buf, size_t buflen) |
| 1837 | { |
| 1838 | struct cgroupfs_root *root; |
| 1839 | struct cgroup *cgrp = NULL; |
| 1840 | int ret = -ENOENT; |
| 1841 | |
| 1842 | mutex_lock(&cgroup_mutex); |
| 1843 | |
| 1844 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1845 | if (root) { |
| 1846 | cgrp = task_cgroup_from_root(task, root); |
| 1847 | ret = cgroup_path(cgrp, buf, buflen); |
| 1848 | } |
| 1849 | |
| 1850 | mutex_unlock(&cgroup_mutex); |
| 1851 | |
| 1852 | return ret; |
| 1853 | } |
| 1854 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1855 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1856 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1857 | * Control Group taskset |
| 1858 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1859 | struct task_and_cgroup { |
| 1860 | struct task_struct *task; |
| 1861 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1862 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1863 | }; |
| 1864 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1865 | struct cgroup_taskset { |
| 1866 | struct task_and_cgroup single; |
| 1867 | struct flex_array *tc_array; |
| 1868 | int tc_array_len; |
| 1869 | int idx; |
| 1870 | struct cgroup *cur_cgrp; |
| 1871 | }; |
| 1872 | |
| 1873 | /** |
| 1874 | * cgroup_taskset_first - reset taskset and return the first task |
| 1875 | * @tset: taskset of interest |
| 1876 | * |
| 1877 | * @tset iteration is initialized and the first task is returned. |
| 1878 | */ |
| 1879 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1880 | { |
| 1881 | if (tset->tc_array) { |
| 1882 | tset->idx = 0; |
| 1883 | return cgroup_taskset_next(tset); |
| 1884 | } else { |
| 1885 | tset->cur_cgrp = tset->single.cgrp; |
| 1886 | return tset->single.task; |
| 1887 | } |
| 1888 | } |
| 1889 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1890 | |
| 1891 | /** |
| 1892 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1893 | * @tset: taskset of interest |
| 1894 | * |
| 1895 | * Return the next task in @tset. Iteration must have been initialized |
| 1896 | * with cgroup_taskset_first(). |
| 1897 | */ |
| 1898 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1899 | { |
| 1900 | struct task_and_cgroup *tc; |
| 1901 | |
| 1902 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1903 | return NULL; |
| 1904 | |
| 1905 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1906 | tset->cur_cgrp = tc->cgrp; |
| 1907 | return tc->task; |
| 1908 | } |
| 1909 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1910 | |
| 1911 | /** |
| 1912 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1913 | * @tset: taskset of interest |
| 1914 | * |
| 1915 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1916 | * function must be preceded by either cgroup_taskset_first() or |
| 1917 | * cgroup_taskset_next(). |
| 1918 | */ |
| 1919 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1920 | { |
| 1921 | return tset->cur_cgrp; |
| 1922 | } |
| 1923 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1924 | |
| 1925 | /** |
| 1926 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1927 | * @tset: taskset of interest |
| 1928 | */ |
| 1929 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1930 | { |
| 1931 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1932 | } |
| 1933 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1934 | |
| 1935 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | /* |
| 1937 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1938 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1939 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1940 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1941 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1942 | struct task_struct *tsk, |
| 1943 | struct css_set *new_cset) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1944 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1945 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1946 | |
| 1947 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1948 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1949 | * setting such that we can't race against cgroup_exit() changing the |
| 1950 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1952 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1953 | old_cset = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1954 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1955 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1956 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1957 | task_unlock(tsk); |
| 1958 | |
| 1959 | /* Update the css_set linked lists if we're using them */ |
| 1960 | write_lock(&css_set_lock); |
| 1961 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1962 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1963 | write_unlock(&css_set_lock); |
| 1964 | |
| 1965 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1966 | * We just gained a reference on old_cset by taking it from the |
| 1967 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1968 | * 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] | 1969 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1970 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1971 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1974 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1975 | * 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] | 1976 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1977 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1978 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1979 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1980 | * 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] | 1981 | * 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] | 1982 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1983 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1984 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1985 | { |
| 1986 | int retval, i, group_size; |
| 1987 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1988 | struct cgroupfs_root *root = cgrp->root; |
| 1989 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1990 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1991 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1992 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1993 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1994 | |
| 1995 | /* |
| 1996 | * step 0: in order to do expensive, possibly blocking operations for |
| 1997 | * every thread, we cannot iterate the thread group list, since it needs |
| 1998 | * 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] | 1999 | * group - group_rwsem prevents new threads from appearing, and if |
| 2000 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2001 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2002 | if (threadgroup) |
| 2003 | group_size = get_nr_threads(tsk); |
| 2004 | else |
| 2005 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2006 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2007 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2008 | if (!group) |
| 2009 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2010 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2011 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2012 | if (retval) |
| 2013 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2014 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2015 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2016 | /* |
| 2017 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2018 | * already PF_EXITING could be freed from underneath us unless we |
| 2019 | * take an rcu_read_lock. |
| 2020 | */ |
| 2021 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2022 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2023 | struct task_and_cgroup ent; |
| 2024 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2025 | /* @tsk either already exited or can't exit until the end */ |
| 2026 | if (tsk->flags & PF_EXITING) |
| 2027 | continue; |
| 2028 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2029 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2030 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2031 | ent.task = tsk; |
| 2032 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2033 | /* nothing to do if this task is already in the cgroup */ |
| 2034 | if (ent.cgrp == cgrp) |
| 2035 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2036 | /* |
| 2037 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2038 | * earlier, but it's good form to communicate our expectations. |
| 2039 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2040 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2041 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2043 | |
| 2044 | if (!threadgroup) |
| 2045 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2046 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2047 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | /* remember the number of threads in the array for later. */ |
| 2049 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2050 | tset.tc_array = group; |
| 2051 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2052 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2053 | /* methods shouldn't be called if no task is actually migrating */ |
| 2054 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2055 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2056 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2057 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2058 | /* |
| 2059 | * step 1: check that we can legitimately attach to the cgroup. |
| 2060 | */ |
| 2061 | for_each_subsys(root, ss) { |
| 2062 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2063 | retval = ss->can_attach(cgrp, &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 | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2076 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2077 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2078 | if (!tc->cg) { |
| 2079 | retval = -ENOMEM; |
| 2080 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2085 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2086 | * proceed to move all tasks to the new cgroup. There are no |
| 2087 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2089 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2090 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2091 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2092 | } |
| 2093 | /* nothing is sensitive to fork() after this point. */ |
| 2094 | |
| 2095 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2096 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2097 | */ |
| 2098 | for_each_subsys(root, ss) { |
| 2099 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2100 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * step 5: success! and cleanup |
| 2105 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2106 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2107 | out_put_css_set_refs: |
| 2108 | if (retval) { |
| 2109 | for (i = 0; i < group_size; i++) { |
| 2110 | tc = flex_array_get(group, i); |
| 2111 | if (!tc->cg) |
| 2112 | break; |
| 2113 | put_css_set(tc->cg); |
| 2114 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2115 | } |
| 2116 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2117 | if (retval) { |
| 2118 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2119 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2120 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2121 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2122 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2123 | } |
| 2124 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2125 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2126 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2127 | return retval; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * 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] | 2132 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2133 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2134 | */ |
| 2135 | 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] | 2136 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2137 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2138 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2139 | int ret; |
| 2140 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2141 | if (!cgroup_lock_live_group(cgrp)) |
| 2142 | return -ENODEV; |
| 2143 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2144 | retry_find_task: |
| 2145 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2146 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2147 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2148 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2149 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2150 | ret= -ESRCH; |
| 2151 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2152 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2153 | /* |
| 2154 | * even if we're attaching all tasks in the thread group, we |
| 2155 | * only need to check permissions on one of them. |
| 2156 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2157 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2158 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2159 | !uid_eq(cred->euid, tcred->uid) && |
| 2160 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2161 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2162 | ret = -EACCES; |
| 2163 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2164 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2165 | } else |
| 2166 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2167 | |
| 2168 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2169 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2170 | |
| 2171 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2172 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2173 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2174 | * with no rt_runtime allocated. Just say no. |
| 2175 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2176 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2177 | ret = -EINVAL; |
| 2178 | rcu_read_unlock(); |
| 2179 | goto out_unlock_cgroup; |
| 2180 | } |
| 2181 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2182 | get_task_struct(tsk); |
| 2183 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2184 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2185 | threadgroup_lock(tsk); |
| 2186 | if (threadgroup) { |
| 2187 | if (!thread_group_leader(tsk)) { |
| 2188 | /* |
| 2189 | * a race with de_thread from another thread's exec() |
| 2190 | * may strip us of our leadership, if this happens, |
| 2191 | * there is no choice but to throw this task away and |
| 2192 | * try again; this is |
| 2193 | * "double-double-toil-and-trouble-check locking". |
| 2194 | */ |
| 2195 | threadgroup_unlock(tsk); |
| 2196 | put_task_struct(tsk); |
| 2197 | goto retry_find_task; |
| 2198 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2202 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2203 | threadgroup_unlock(tsk); |
| 2204 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2205 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2206 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2207 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2208 | return ret; |
| 2209 | } |
| 2210 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2211 | /** |
| 2212 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2213 | * @from: attach to all cgroups of a given task |
| 2214 | * @tsk: the task to be attached |
| 2215 | */ |
| 2216 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2217 | { |
| 2218 | struct cgroupfs_root *root; |
| 2219 | int retval = 0; |
| 2220 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2221 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2222 | for_each_active_root(root) { |
| 2223 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2224 | |
| 2225 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2226 | if (retval) |
| 2227 | break; |
| 2228 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2229 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2230 | |
| 2231 | return retval; |
| 2232 | } |
| 2233 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2234 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2235 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2236 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2237 | return attach_task_by_pid(cgrp, pid, false); |
| 2238 | } |
| 2239 | |
| 2240 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2241 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2242 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2245 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2246 | const char *buffer) |
| 2247 | { |
| 2248 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2249 | if (strlen(buffer) >= PATH_MAX) |
| 2250 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2251 | if (!cgroup_lock_live_group(cgrp)) |
| 2252 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2253 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2254 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2255 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2256 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2257 | return 0; |
| 2258 | } |
| 2259 | |
| 2260 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2261 | struct seq_file *seq) |
| 2262 | { |
| 2263 | if (!cgroup_lock_live_group(cgrp)) |
| 2264 | return -ENODEV; |
| 2265 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2266 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2267 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2268 | return 0; |
| 2269 | } |
| 2270 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2271 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2272 | struct seq_file *seq) |
| 2273 | { |
| 2274 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2275 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2278 | /* A buffer size big enough for numbers or short strings */ |
| 2279 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2280 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2281 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2282 | struct file *file, |
| 2283 | const char __user *userbuf, |
| 2284 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2285 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2286 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2287 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2288 | char *end; |
| 2289 | |
| 2290 | if (!nbytes) |
| 2291 | return -EINVAL; |
| 2292 | if (nbytes >= sizeof(buffer)) |
| 2293 | return -E2BIG; |
| 2294 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2295 | return -EFAULT; |
| 2296 | |
| 2297 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2298 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2299 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2300 | if (*end) |
| 2301 | return -EINVAL; |
| 2302 | retval = cft->write_u64(cgrp, cft, val); |
| 2303 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2304 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2305 | if (*end) |
| 2306 | return -EINVAL; |
| 2307 | retval = cft->write_s64(cgrp, cft, val); |
| 2308 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2309 | if (!retval) |
| 2310 | retval = nbytes; |
| 2311 | return retval; |
| 2312 | } |
| 2313 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2314 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2315 | struct file *file, |
| 2316 | const char __user *userbuf, |
| 2317 | size_t nbytes, loff_t *unused_ppos) |
| 2318 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2319 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2320 | int retval = 0; |
| 2321 | size_t max_bytes = cft->max_write_len; |
| 2322 | char *buffer = local_buffer; |
| 2323 | |
| 2324 | if (!max_bytes) |
| 2325 | max_bytes = sizeof(local_buffer) - 1; |
| 2326 | if (nbytes >= max_bytes) |
| 2327 | return -E2BIG; |
| 2328 | /* Allocate a dynamic buffer if we need one */ |
| 2329 | if (nbytes >= sizeof(local_buffer)) { |
| 2330 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2331 | if (buffer == NULL) |
| 2332 | return -ENOMEM; |
| 2333 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2334 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2335 | retval = -EFAULT; |
| 2336 | goto out; |
| 2337 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2338 | |
| 2339 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2340 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2341 | if (!retval) |
| 2342 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2343 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2344 | if (buffer != local_buffer) |
| 2345 | kfree(buffer); |
| 2346 | return retval; |
| 2347 | } |
| 2348 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2349 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2350 | size_t nbytes, loff_t *ppos) |
| 2351 | { |
| 2352 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2353 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2354 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2355 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2356 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2357 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2358 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2359 | if (cft->write_u64 || cft->write_s64) |
| 2360 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2361 | if (cft->write_string) |
| 2362 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2363 | if (cft->trigger) { |
| 2364 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2365 | return ret ? ret : nbytes; |
| 2366 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2367 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2370 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2371 | struct file *file, |
| 2372 | char __user *buf, size_t nbytes, |
| 2373 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2374 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2375 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2376 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2377 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2378 | |
| 2379 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2380 | } |
| 2381 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2382 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2383 | struct file *file, |
| 2384 | char __user *buf, size_t nbytes, |
| 2385 | loff_t *ppos) |
| 2386 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2387 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2388 | s64 val = cft->read_s64(cgrp, cft); |
| 2389 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2390 | |
| 2391 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2392 | } |
| 2393 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2394 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2395 | size_t nbytes, loff_t *ppos) |
| 2396 | { |
| 2397 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2398 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2399 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2400 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2401 | return -ENODEV; |
| 2402 | |
| 2403 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2404 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2405 | if (cft->read_u64) |
| 2406 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2407 | if (cft->read_s64) |
| 2408 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2409 | return -EINVAL; |
| 2410 | } |
| 2411 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2412 | /* |
| 2413 | * seqfile ops/methods for returning structured data. Currently just |
| 2414 | * supports string->u64 maps, but can be extended in future. |
| 2415 | */ |
| 2416 | |
| 2417 | struct cgroup_seqfile_state { |
| 2418 | struct cftype *cft; |
| 2419 | struct cgroup *cgroup; |
| 2420 | }; |
| 2421 | |
| 2422 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2423 | { |
| 2424 | struct seq_file *sf = cb->state; |
| 2425 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2426 | } |
| 2427 | |
| 2428 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2429 | { |
| 2430 | struct cgroup_seqfile_state *state = m->private; |
| 2431 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2432 | if (cft->read_map) { |
| 2433 | struct cgroup_map_cb cb = { |
| 2434 | .fill = cgroup_map_add, |
| 2435 | .state = m, |
| 2436 | }; |
| 2437 | return cft->read_map(state->cgroup, cft, &cb); |
| 2438 | } |
| 2439 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2442 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2443 | { |
| 2444 | struct seq_file *seq = file->private_data; |
| 2445 | kfree(seq->private); |
| 2446 | return single_release(inode, file); |
| 2447 | } |
| 2448 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2449 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2450 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2451 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2452 | .llseek = seq_lseek, |
| 2453 | .release = cgroup_seqfile_release, |
| 2454 | }; |
| 2455 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2456 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2457 | { |
| 2458 | int err; |
| 2459 | struct cftype *cft; |
| 2460 | |
| 2461 | err = generic_file_open(inode, file); |
| 2462 | if (err) |
| 2463 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2464 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2465 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2466 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2467 | struct cgroup_seqfile_state *state; |
| 2468 | |
| 2469 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2470 | if (!state) |
| 2471 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2472 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2473 | state->cft = cft; |
| 2474 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2475 | file->f_op = &cgroup_seqfile_operations; |
| 2476 | err = single_open(file, cgroup_seqfile_show, state); |
| 2477 | if (err < 0) |
| 2478 | kfree(state); |
| 2479 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2480 | err = cft->open(inode, file); |
| 2481 | else |
| 2482 | err = 0; |
| 2483 | |
| 2484 | return err; |
| 2485 | } |
| 2486 | |
| 2487 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2488 | { |
| 2489 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2490 | if (cft->release) |
| 2491 | return cft->release(inode, file); |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | /* |
| 2496 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2497 | */ |
| 2498 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2499 | struct inode *new_dir, struct dentry *new_dentry) |
| 2500 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2501 | int ret; |
| 2502 | struct cgroup_name *name, *old_name; |
| 2503 | struct cgroup *cgrp; |
| 2504 | |
| 2505 | /* |
| 2506 | * It's convinient to use parent dir's i_mutex to protected |
| 2507 | * cgrp->name. |
| 2508 | */ |
| 2509 | lockdep_assert_held(&old_dir->i_mutex); |
| 2510 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2511 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2512 | return -ENOTDIR; |
| 2513 | if (new_dentry->d_inode) |
| 2514 | return -EEXIST; |
| 2515 | if (old_dir != new_dir) |
| 2516 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2517 | |
| 2518 | cgrp = __d_cgrp(old_dentry); |
| 2519 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2520 | /* |
| 2521 | * This isn't a proper migration and its usefulness is very |
| 2522 | * limited. Disallow if sane_behavior. |
| 2523 | */ |
| 2524 | if (cgroup_sane_behavior(cgrp)) |
| 2525 | return -EPERM; |
| 2526 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2527 | name = cgroup_alloc_name(new_dentry); |
| 2528 | if (!name) |
| 2529 | return -ENOMEM; |
| 2530 | |
| 2531 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2532 | if (ret) { |
| 2533 | kfree(name); |
| 2534 | return ret; |
| 2535 | } |
| 2536 | |
| 2537 | old_name = cgrp->name; |
| 2538 | rcu_assign_pointer(cgrp->name, name); |
| 2539 | |
| 2540 | kfree_rcu(old_name, rcu_head); |
| 2541 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2544 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2545 | { |
| 2546 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2547 | return &__d_cgrp(dentry)->xattrs; |
| 2548 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2549 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | static inline int xattr_enabled(struct dentry *dentry) |
| 2553 | { |
| 2554 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2555 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | static bool is_valid_xattr(const char *name) |
| 2559 | { |
| 2560 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2561 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2562 | return true; |
| 2563 | return false; |
| 2564 | } |
| 2565 | |
| 2566 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2567 | const void *val, size_t size, int flags) |
| 2568 | { |
| 2569 | if (!xattr_enabled(dentry)) |
| 2570 | return -EOPNOTSUPP; |
| 2571 | if (!is_valid_xattr(name)) |
| 2572 | return -EINVAL; |
| 2573 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2574 | } |
| 2575 | |
| 2576 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2577 | { |
| 2578 | if (!xattr_enabled(dentry)) |
| 2579 | return -EOPNOTSUPP; |
| 2580 | if (!is_valid_xattr(name)) |
| 2581 | return -EINVAL; |
| 2582 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2583 | } |
| 2584 | |
| 2585 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2586 | void *buf, size_t size) |
| 2587 | { |
| 2588 | if (!xattr_enabled(dentry)) |
| 2589 | return -EOPNOTSUPP; |
| 2590 | if (!is_valid_xattr(name)) |
| 2591 | return -EINVAL; |
| 2592 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2593 | } |
| 2594 | |
| 2595 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2596 | { |
| 2597 | if (!xattr_enabled(dentry)) |
| 2598 | return -EOPNOTSUPP; |
| 2599 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2600 | } |
| 2601 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2602 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2603 | .read = cgroup_file_read, |
| 2604 | .write = cgroup_file_write, |
| 2605 | .llseek = generic_file_llseek, |
| 2606 | .open = cgroup_file_open, |
| 2607 | .release = cgroup_file_release, |
| 2608 | }; |
| 2609 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2610 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2611 | .setxattr = cgroup_setxattr, |
| 2612 | .getxattr = cgroup_getxattr, |
| 2613 | .listxattr = cgroup_listxattr, |
| 2614 | .removexattr = cgroup_removexattr, |
| 2615 | }; |
| 2616 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2617 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2618 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2619 | .mkdir = cgroup_mkdir, |
| 2620 | .rmdir = cgroup_rmdir, |
| 2621 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2622 | .setxattr = cgroup_setxattr, |
| 2623 | .getxattr = cgroup_getxattr, |
| 2624 | .listxattr = cgroup_listxattr, |
| 2625 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2626 | }; |
| 2627 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2628 | 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] | 2629 | { |
| 2630 | if (dentry->d_name.len > NAME_MAX) |
| 2631 | return ERR_PTR(-ENAMETOOLONG); |
| 2632 | d_add(dentry, NULL); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2636 | /* |
| 2637 | * Check if a file is a control file |
| 2638 | */ |
| 2639 | static inline struct cftype *__file_cft(struct file *file) |
| 2640 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2641 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2642 | return ERR_PTR(-EINVAL); |
| 2643 | return __d_cft(file->f_dentry); |
| 2644 | } |
| 2645 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2646 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2647 | struct super_block *sb) |
| 2648 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2649 | struct inode *inode; |
| 2650 | |
| 2651 | if (!dentry) |
| 2652 | return -ENOENT; |
| 2653 | if (dentry->d_inode) |
| 2654 | return -EEXIST; |
| 2655 | |
| 2656 | inode = cgroup_new_inode(mode, sb); |
| 2657 | if (!inode) |
| 2658 | return -ENOMEM; |
| 2659 | |
| 2660 | if (S_ISDIR(mode)) { |
| 2661 | inode->i_op = &cgroup_dir_inode_operations; |
| 2662 | inode->i_fop = &simple_dir_operations; |
| 2663 | |
| 2664 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2665 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2666 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2667 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2668 | /* |
| 2669 | * Control reaches here with cgroup_mutex held. |
| 2670 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2671 | * want to populate it immediately without releasing |
| 2672 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2673 | * yet, trylock will always succeed without affecting |
| 2674 | * lockdep checks. |
| 2675 | */ |
| 2676 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2677 | } else if (S_ISREG(mode)) { |
| 2678 | inode->i_size = 0; |
| 2679 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2680 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2681 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2682 | d_instantiate(dentry, inode); |
| 2683 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2687 | /** |
| 2688 | * cgroup_file_mode - deduce file mode of a control file |
| 2689 | * @cft: the control file in question |
| 2690 | * |
| 2691 | * returns cft->mode if ->mode is not 0 |
| 2692 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2693 | * returns S_IRUGO if it has only a read handler |
| 2694 | * returns S_IWUSR if it has only a write hander |
| 2695 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2696 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2697 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2698 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2699 | |
| 2700 | if (cft->mode) |
| 2701 | return cft->mode; |
| 2702 | |
| 2703 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2704 | cft->read_map || cft->read_seq_string) |
| 2705 | mode |= S_IRUGO; |
| 2706 | |
| 2707 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2708 | cft->write_string || cft->trigger) |
| 2709 | mode |= S_IWUSR; |
| 2710 | |
| 2711 | return mode; |
| 2712 | } |
| 2713 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2714 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2715 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2716 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2717 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2718 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2719 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2720 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2721 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2722 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2723 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2724 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2725 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2726 | strcpy(name, subsys->name); |
| 2727 | strcat(name, "."); |
| 2728 | } |
| 2729 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2730 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2731 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2732 | |
| 2733 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2734 | if (!cfe) |
| 2735 | return -ENOMEM; |
| 2736 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2737 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2738 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2739 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2740 | goto out; |
| 2741 | } |
| 2742 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2743 | cfe->type = (void *)cft; |
| 2744 | cfe->dentry = dentry; |
| 2745 | dentry->d_fsdata = cfe; |
| 2746 | simple_xattrs_init(&cfe->xattrs); |
| 2747 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2748 | mode = cgroup_file_mode(cft); |
| 2749 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2750 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2751 | list_add_tail(&cfe->node, &parent->files); |
| 2752 | cfe = NULL; |
| 2753 | } |
| 2754 | dput(dentry); |
| 2755 | out: |
| 2756 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2757 | return error; |
| 2758 | } |
| 2759 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2760 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2761 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2762 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2763 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2764 | int err, ret = 0; |
| 2765 | |
| 2766 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2767 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2768 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2769 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2770 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2771 | continue; |
| 2772 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2773 | continue; |
| 2774 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2775 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2776 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2777 | if (err) |
| 2778 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2779 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2780 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2781 | } else { |
| 2782 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2783 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2784 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2785 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2788 | static DEFINE_MUTEX(cgroup_cft_mutex); |
| 2789 | |
| 2790 | static void cgroup_cfts_prepare(void) |
| 2791 | __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex) |
| 2792 | { |
| 2793 | /* |
| 2794 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2795 | * the existing cgroups under cgroup_mutex and create files. |
| 2796 | * Instead, we increment reference on all cgroups and build list of |
| 2797 | * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure |
| 2798 | * exclusive access to the field. |
| 2799 | */ |
| 2800 | mutex_lock(&cgroup_cft_mutex); |
| 2801 | mutex_lock(&cgroup_mutex); |
| 2802 | } |
| 2803 | |
| 2804 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2805 | struct cftype *cfts, bool is_add) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2806 | __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex) |
| 2807 | { |
| 2808 | LIST_HEAD(pending); |
| 2809 | struct cgroup *cgrp, *n; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2810 | struct super_block *sb = ss->root->sb; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2811 | |
| 2812 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2813 | if (cfts && ss->root != &rootnode && |
| 2814 | atomic_inc_not_zero(sb->s_active)) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2815 | list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) { |
| 2816 | dget(cgrp->dentry); |
| 2817 | list_add_tail(&cgrp->cft_q_node, &pending); |
| 2818 | } |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2819 | } else { |
| 2820 | sb = NULL; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | mutex_unlock(&cgroup_mutex); |
| 2824 | |
| 2825 | /* |
| 2826 | * All new cgroups will see @cfts update on @ss->cftsets. Add/rm |
| 2827 | * files for all cgroups which were created before. |
| 2828 | */ |
| 2829 | list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) { |
| 2830 | struct inode *inode = cgrp->dentry->d_inode; |
| 2831 | |
| 2832 | mutex_lock(&inode->i_mutex); |
| 2833 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2834 | if (!cgroup_is_dead(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2835 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2836 | mutex_unlock(&cgroup_mutex); |
| 2837 | mutex_unlock(&inode->i_mutex); |
| 2838 | |
| 2839 | list_del_init(&cgrp->cft_q_node); |
| 2840 | dput(cgrp->dentry); |
| 2841 | } |
| 2842 | |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2843 | if (sb) |
| 2844 | deactivate_super(sb); |
| 2845 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2846 | mutex_unlock(&cgroup_cft_mutex); |
| 2847 | } |
| 2848 | |
| 2849 | /** |
| 2850 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2851 | * @ss: target cgroup subsystem |
| 2852 | * @cfts: zero-length name terminated array of cftypes |
| 2853 | * |
| 2854 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2855 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2856 | * have them too. This function can be called anytime whether @ss is |
| 2857 | * attached or not. |
| 2858 | * |
| 2859 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2860 | * function currently returns 0 as long as @cfts registration is successful |
| 2861 | * even if some file creation attempts on existing cgroups fail. |
| 2862 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2863 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2864 | { |
| 2865 | struct cftype_set *set; |
| 2866 | |
| 2867 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2868 | if (!set) |
| 2869 | return -ENOMEM; |
| 2870 | |
| 2871 | cgroup_cfts_prepare(); |
| 2872 | set->cfts = cfts; |
| 2873 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2874 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2875 | |
| 2876 | return 0; |
| 2877 | } |
| 2878 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2879 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2880 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2881 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2882 | * @ss: target cgroup subsystem |
| 2883 | * @cfts: zero-length name terminated array of cftypes |
| 2884 | * |
| 2885 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2886 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2887 | * won't have them either. This function can be called anytime whether @ss |
| 2888 | * is attached or not. |
| 2889 | * |
| 2890 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2891 | * registered with @ss. |
| 2892 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2893 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2894 | { |
| 2895 | struct cftype_set *set; |
| 2896 | |
| 2897 | cgroup_cfts_prepare(); |
| 2898 | |
| 2899 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2900 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2901 | list_del(&set->node); |
| 2902 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2903 | cgroup_cfts_commit(ss, cfts, false); |
| 2904 | return 0; |
| 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | cgroup_cfts_commit(ss, NULL, false); |
| 2909 | return -ENOENT; |
| 2910 | } |
| 2911 | |
| 2912 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2913 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2914 | * @cgrp: the cgroup in question |
| 2915 | * |
| 2916 | * Return the number of tasks in the cgroup. |
| 2917 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2918 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2919 | { |
| 2920 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2921 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2922 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2923 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2924 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2925 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2926 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2927 | return count; |
| 2928 | } |
| 2929 | |
| 2930 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2931 | * Advance a list_head iterator. The iterator should be positioned at |
| 2932 | * the start of a css_set |
| 2933 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2934 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2935 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2936 | struct list_head *l = it->cset_link; |
| 2937 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2938 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2939 | |
| 2940 | /* Advance to the next non-empty css_set */ |
| 2941 | do { |
| 2942 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2943 | if (l == &cgrp->cset_links) { |
| 2944 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2945 | return; |
| 2946 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2947 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2948 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2949 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2950 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2951 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2954 | /* |
| 2955 | * To reduce the fork() overhead for systems that are not actually |
| 2956 | * using their cgroups capability, we don't maintain the lists running |
| 2957 | * through each css_set to its tasks until we see the list actually |
| 2958 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2959 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2960 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2961 | { |
| 2962 | struct task_struct *p, *g; |
| 2963 | write_lock(&css_set_lock); |
| 2964 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2965 | /* |
| 2966 | * We need tasklist_lock because RCU is not safe against |
| 2967 | * while_each_thread(). Besides, a forking task that has passed |
| 2968 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2969 | * is not guaranteed to have its child immediately visible in the |
| 2970 | * tasklist if we walk through it with RCU. |
| 2971 | */ |
| 2972 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2973 | do_each_thread(g, p) { |
| 2974 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2975 | /* |
| 2976 | * We should check if the process is exiting, otherwise |
| 2977 | * it will race with cgroup_exit() in that the list |
| 2978 | * entry won't be deleted though the process has exited. |
| 2979 | */ |
| 2980 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2981 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2982 | task_unlock(p); |
| 2983 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2984 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2985 | write_unlock(&css_set_lock); |
| 2986 | } |
| 2987 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2988 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2989 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 2990 | * @pos: the current cgroup |
| 2991 | * |
| 2992 | * This function returns the next sibling of @pos and should be called |
| 2993 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 2994 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 2995 | * state. |
| 2996 | */ |
| 2997 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 2998 | { |
| 2999 | struct cgroup *next; |
| 3000 | |
| 3001 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3002 | |
| 3003 | /* |
| 3004 | * @pos could already have been removed. Once a cgroup is removed, |
| 3005 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3006 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3007 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3008 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3009 | * finished its grace period even if it's already removed, and thus |
| 3010 | * safe to dereference from this RCU critical section. If |
| 3011 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3012 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3013 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3014 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3015 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3016 | if (&next->sibling != &pos->parent->children) |
| 3017 | return next; |
| 3018 | return NULL; |
| 3019 | } |
| 3020 | |
| 3021 | /* |
| 3022 | * Can't dereference the next pointer. Each cgroup is given a |
| 3023 | * monotonically increasing unique serial number and always |
| 3024 | * appended to the sibling list, so the next one can be found by |
| 3025 | * walking the parent's children until we see a cgroup with higher |
| 3026 | * serial number than @pos's. |
| 3027 | * |
| 3028 | * While this path can be slow, it's taken only when either the |
| 3029 | * current cgroup is removed or iteration and removal race. |
| 3030 | */ |
| 3031 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3032 | if (next->serial_nr > pos->serial_nr) |
| 3033 | return next; |
| 3034 | return NULL; |
| 3035 | } |
| 3036 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3037 | |
| 3038 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3039 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3040 | * @pos: the current position (%NULL to initiate traversal) |
| 3041 | * @cgroup: cgroup whose descendants to walk |
| 3042 | * |
| 3043 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3044 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3045 | * |
| 3046 | * While this function requires RCU read locking, it doesn't require the |
| 3047 | * whole traversal to be contained in a single RCU critical section. This |
| 3048 | * function will return the correct next descendant as long as both @pos |
| 3049 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3050 | */ |
| 3051 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3052 | struct cgroup *cgroup) |
| 3053 | { |
| 3054 | struct cgroup *next; |
| 3055 | |
| 3056 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3057 | |
| 3058 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3059 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3060 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3061 | |
| 3062 | /* visit the first child if exists */ |
| 3063 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3064 | if (next) |
| 3065 | return next; |
| 3066 | |
| 3067 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3068 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3069 | next = cgroup_next_sibling(pos); |
| 3070 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3071 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3072 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3073 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3074 | |
| 3075 | return NULL; |
| 3076 | } |
| 3077 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3078 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3079 | /** |
| 3080 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3081 | * @pos: cgroup of interest |
| 3082 | * |
| 3083 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3084 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3085 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3086 | * |
| 3087 | * While this function requires RCU read locking, it doesn't require the |
| 3088 | * whole traversal to be contained in a single RCU critical section. This |
| 3089 | * function will return the correct rightmost descendant as long as @pos is |
| 3090 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3091 | */ |
| 3092 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3093 | { |
| 3094 | struct cgroup *last, *tmp; |
| 3095 | |
| 3096 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3097 | |
| 3098 | do { |
| 3099 | last = pos; |
| 3100 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3101 | pos = NULL; |
| 3102 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3103 | pos = tmp; |
| 3104 | } while (pos); |
| 3105 | |
| 3106 | return last; |
| 3107 | } |
| 3108 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3109 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3110 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3111 | { |
| 3112 | struct cgroup *last; |
| 3113 | |
| 3114 | do { |
| 3115 | last = pos; |
| 3116 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3117 | sibling); |
| 3118 | } while (pos); |
| 3119 | |
| 3120 | return last; |
| 3121 | } |
| 3122 | |
| 3123 | /** |
| 3124 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3125 | * @pos: the current position (%NULL to initiate traversal) |
| 3126 | * @cgroup: cgroup whose descendants to walk |
| 3127 | * |
| 3128 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3129 | * descendant to visit for post-order traversal of @cgroup's descendants. |
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 next descendant as long as both @pos |
| 3134 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3135 | */ |
| 3136 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3137 | struct cgroup *cgroup) |
| 3138 | { |
| 3139 | struct cgroup *next; |
| 3140 | |
| 3141 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3142 | |
| 3143 | /* if first iteration, visit the leftmost descendant */ |
| 3144 | if (!pos) { |
| 3145 | next = cgroup_leftmost_descendant(cgroup); |
| 3146 | return next != cgroup ? next : NULL; |
| 3147 | } |
| 3148 | |
| 3149 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3150 | next = cgroup_next_sibling(pos); |
| 3151 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3152 | return cgroup_leftmost_descendant(next); |
| 3153 | |
| 3154 | /* no sibling left, visit parent */ |
| 3155 | next = pos->parent; |
| 3156 | return next != cgroup ? next : NULL; |
| 3157 | } |
| 3158 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3159 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3160 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3161 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3162 | { |
| 3163 | /* |
| 3164 | * The first time anyone tries to iterate across a cgroup, |
| 3165 | * we need to enable the list linking each css_set to its |
| 3166 | * tasks, and fix up all existing tasks. |
| 3167 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3168 | if (!use_task_css_set_links) |
| 3169 | cgroup_enable_task_cg_lists(); |
| 3170 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3171 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3172 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3173 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3176 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3177 | struct cgroup_iter *it) |
| 3178 | { |
| 3179 | struct task_struct *res; |
| 3180 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3181 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3182 | |
| 3183 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3184 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3185 | return NULL; |
| 3186 | res = list_entry(l, struct task_struct, cg_list); |
| 3187 | /* Advance iterator to find next entry */ |
| 3188 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3189 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3190 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3191 | /* We reached the end of this task list - move on to |
| 3192 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3193 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3194 | } else { |
| 3195 | it->task = l; |
| 3196 | } |
| 3197 | return res; |
| 3198 | } |
| 3199 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3200 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3201 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3202 | { |
| 3203 | read_unlock(&css_set_lock); |
| 3204 | } |
| 3205 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3206 | static inline int started_after_time(struct task_struct *t1, |
| 3207 | struct timespec *time, |
| 3208 | struct task_struct *t2) |
| 3209 | { |
| 3210 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3211 | if (start_diff > 0) { |
| 3212 | return 1; |
| 3213 | } else if (start_diff < 0) { |
| 3214 | return 0; |
| 3215 | } else { |
| 3216 | /* |
| 3217 | * Arbitrarily, if two processes started at the same |
| 3218 | * time, we'll say that the lower pointer value |
| 3219 | * started first. Note that t2 may have exited by now |
| 3220 | * so this may not be a valid pointer any longer, but |
| 3221 | * that's fine - it still serves to distinguish |
| 3222 | * between two tasks started (effectively) simultaneously. |
| 3223 | */ |
| 3224 | return t1 > t2; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * This function is a callback from heap_insert() and is used to order |
| 3230 | * the heap. |
| 3231 | * In this case we order the heap in descending task start time. |
| 3232 | */ |
| 3233 | static inline int started_after(void *p1, void *p2) |
| 3234 | { |
| 3235 | struct task_struct *t1 = p1; |
| 3236 | struct task_struct *t2 = p2; |
| 3237 | return started_after_time(t1, &t2->start_time, t2); |
| 3238 | } |
| 3239 | |
| 3240 | /** |
| 3241 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3242 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3243 | * |
| 3244 | * Arguments include pointers to callback functions test_task() and |
| 3245 | * process_task(). |
| 3246 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3247 | * and if it returns true, call process_task() for it also. |
| 3248 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3249 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3250 | * but does not lock css_set_lock for the call to process_task(). |
| 3251 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3252 | * creation. |
| 3253 | * It is guaranteed that process_task() will act on every task that |
| 3254 | * is a member of the cgroup for the duration of this call. This |
| 3255 | * function may or may not call process_task() for tasks that exit |
| 3256 | * or move to a different cgroup during the call, or are forked or |
| 3257 | * move into the cgroup during the call. |
| 3258 | * |
| 3259 | * Note that test_task() may be called with locks held, and may in some |
| 3260 | * situations be called multiple times for the same task, so it should |
| 3261 | * be cheap. |
| 3262 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3263 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3264 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3265 | * may cause this function to fail). |
| 3266 | */ |
| 3267 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3268 | { |
| 3269 | int retval, i; |
| 3270 | struct cgroup_iter it; |
| 3271 | struct task_struct *p, *dropped; |
| 3272 | /* Never dereference latest_task, since it's not refcounted */ |
| 3273 | struct task_struct *latest_task = NULL; |
| 3274 | struct ptr_heap tmp_heap; |
| 3275 | struct ptr_heap *heap; |
| 3276 | struct timespec latest_time = { 0, 0 }; |
| 3277 | |
| 3278 | if (scan->heap) { |
| 3279 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3280 | heap = scan->heap; |
| 3281 | heap->gt = &started_after; |
| 3282 | } else { |
| 3283 | /* We need to allocate our own heap memory */ |
| 3284 | heap = &tmp_heap; |
| 3285 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3286 | if (retval) |
| 3287 | /* cannot allocate the heap */ |
| 3288 | return retval; |
| 3289 | } |
| 3290 | |
| 3291 | again: |
| 3292 | /* |
| 3293 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3294 | * to determine which are of interest, and using the scanner's |
| 3295 | * "process_task" callback to process any of them that need an update. |
| 3296 | * Since we don't want to hold any locks during the task updates, |
| 3297 | * gather tasks to be processed in a heap structure. |
| 3298 | * The heap is sorted by descending task start time. |
| 3299 | * If the statically-sized heap fills up, we overflow tasks that |
| 3300 | * started later, and in future iterations only consider tasks that |
| 3301 | * started after the latest task in the previous pass. This |
| 3302 | * guarantees forward progress and that we don't miss any tasks. |
| 3303 | */ |
| 3304 | heap->size = 0; |
| 3305 | cgroup_iter_start(scan->cg, &it); |
| 3306 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3307 | /* |
| 3308 | * Only affect tasks that qualify per the caller's callback, |
| 3309 | * if he provided one |
| 3310 | */ |
| 3311 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3312 | continue; |
| 3313 | /* |
| 3314 | * Only process tasks that started after the last task |
| 3315 | * we processed |
| 3316 | */ |
| 3317 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3318 | continue; |
| 3319 | dropped = heap_insert(heap, p); |
| 3320 | if (dropped == NULL) { |
| 3321 | /* |
| 3322 | * The new task was inserted; the heap wasn't |
| 3323 | * previously full |
| 3324 | */ |
| 3325 | get_task_struct(p); |
| 3326 | } else if (dropped != p) { |
| 3327 | /* |
| 3328 | * The new task was inserted, and pushed out a |
| 3329 | * different task |
| 3330 | */ |
| 3331 | get_task_struct(p); |
| 3332 | put_task_struct(dropped); |
| 3333 | } |
| 3334 | /* |
| 3335 | * Else the new task was newer than anything already in |
| 3336 | * the heap and wasn't inserted |
| 3337 | */ |
| 3338 | } |
| 3339 | cgroup_iter_end(scan->cg, &it); |
| 3340 | |
| 3341 | if (heap->size) { |
| 3342 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3343 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3344 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3345 | latest_time = q->start_time; |
| 3346 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3347 | } |
| 3348 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3349 | scan->process_task(q, scan); |
| 3350 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3351 | } |
| 3352 | /* |
| 3353 | * If we had to process any tasks at all, scan again |
| 3354 | * in case some of them were in the middle of forking |
| 3355 | * children that didn't get processed. |
| 3356 | * Not the most efficient way to do it, but it avoids |
| 3357 | * having to take callback_mutex in the fork path |
| 3358 | */ |
| 3359 | goto again; |
| 3360 | } |
| 3361 | if (heap == &tmp_heap) |
| 3362 | heap_free(&tmp_heap); |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3366 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3367 | struct cgroup_scanner *scan) |
| 3368 | { |
| 3369 | struct cgroup *new_cgroup = scan->data; |
| 3370 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3371 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3372 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3373 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | /** |
| 3377 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3378 | * @to: cgroup to which the tasks will be moved |
| 3379 | * @from: cgroup in which the tasks currently reside |
| 3380 | */ |
| 3381 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3382 | { |
| 3383 | struct cgroup_scanner scan; |
| 3384 | |
| 3385 | scan.cg = from; |
| 3386 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3387 | scan.process_task = cgroup_transfer_one_task; |
| 3388 | scan.heap = NULL; |
| 3389 | scan.data = to; |
| 3390 | |
| 3391 | return cgroup_scan_tasks(&scan); |
| 3392 | } |
| 3393 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3394 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3395 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3396 | * |
| 3397 | * Reading this file can return large amounts of data if a cgroup has |
| 3398 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3399 | * but we cannot guarantee that the information we produce is correct |
| 3400 | * unless we produce it entirely atomically. |
| 3401 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3402 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3403 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3404 | /* which pidlist file are we talking about? */ |
| 3405 | enum cgroup_filetype { |
| 3406 | CGROUP_FILE_PROCS, |
| 3407 | CGROUP_FILE_TASKS, |
| 3408 | }; |
| 3409 | |
| 3410 | /* |
| 3411 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3412 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3413 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3414 | * to the cgroup. |
| 3415 | */ |
| 3416 | struct cgroup_pidlist { |
| 3417 | /* |
| 3418 | * used to find which pidlist is wanted. doesn't change as long as |
| 3419 | * this particular list stays in the list. |
| 3420 | */ |
| 3421 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3422 | /* array of xids */ |
| 3423 | pid_t *list; |
| 3424 | /* how many elements the above list has */ |
| 3425 | int length; |
| 3426 | /* how many files are using the current array */ |
| 3427 | int use_count; |
| 3428 | /* each of these stored in a list by its cgroup */ |
| 3429 | struct list_head links; |
| 3430 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3431 | struct cgroup *owner; |
| 3432 | /* protects the other fields */ |
| 3433 | struct rw_semaphore mutex; |
| 3434 | }; |
| 3435 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3436 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3437 | * The following two functions "fix" the issue where there are more pids |
| 3438 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3439 | * TODO: replace with a kernel-wide solution to this problem |
| 3440 | */ |
| 3441 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3442 | static void *pidlist_allocate(int count) |
| 3443 | { |
| 3444 | if (PIDLIST_TOO_LARGE(count)) |
| 3445 | return vmalloc(count * sizeof(pid_t)); |
| 3446 | else |
| 3447 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3448 | } |
| 3449 | static void pidlist_free(void *p) |
| 3450 | { |
| 3451 | if (is_vmalloc_addr(p)) |
| 3452 | vfree(p); |
| 3453 | else |
| 3454 | kfree(p); |
| 3455 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3456 | |
| 3457 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3458 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3459 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3460 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3461 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3462 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3463 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3464 | |
| 3465 | /* |
| 3466 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3467 | * edge cases first; no work needs to be done for either |
| 3468 | */ |
| 3469 | if (length == 0 || length == 1) |
| 3470 | return length; |
| 3471 | /* src and dest walk down the list; dest counts unique elements */ |
| 3472 | for (src = 1; src < length; src++) { |
| 3473 | /* find next unique element */ |
| 3474 | while (list[src] == list[src-1]) { |
| 3475 | src++; |
| 3476 | if (src == length) |
| 3477 | goto after; |
| 3478 | } |
| 3479 | /* dest always points to where the next unique element goes */ |
| 3480 | list[dest] = list[src]; |
| 3481 | dest++; |
| 3482 | } |
| 3483 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3484 | return dest; |
| 3485 | } |
| 3486 | |
| 3487 | static int cmppid(const void *a, const void *b) |
| 3488 | { |
| 3489 | return *(pid_t *)a - *(pid_t *)b; |
| 3490 | } |
| 3491 | |
| 3492 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3493 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3494 | * returns with the lock on that pidlist already held, and takes care |
| 3495 | * of the use count, or returns NULL with no locks held if we're out of |
| 3496 | * memory. |
| 3497 | */ |
| 3498 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3499 | enum cgroup_filetype type) |
| 3500 | { |
| 3501 | struct cgroup_pidlist *l; |
| 3502 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3503 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3504 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3505 | /* |
| 3506 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3507 | * the last ref-holder is trying to remove l from the list at the same |
| 3508 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3509 | * list we find out from under us - compare release_pid_array(). |
| 3510 | */ |
| 3511 | mutex_lock(&cgrp->pidlist_mutex); |
| 3512 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3513 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3514 | /* make sure l doesn't vanish out from under us */ |
| 3515 | down_write(&l->mutex); |
| 3516 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3517 | return l; |
| 3518 | } |
| 3519 | } |
| 3520 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3521 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3522 | if (!l) { |
| 3523 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3524 | return l; |
| 3525 | } |
| 3526 | init_rwsem(&l->mutex); |
| 3527 | down_write(&l->mutex); |
| 3528 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3529 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3530 | l->owner = cgrp; |
| 3531 | list_add(&l->links, &cgrp->pidlists); |
| 3532 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3533 | return l; |
| 3534 | } |
| 3535 | |
| 3536 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3537 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3538 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3539 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3540 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3541 | { |
| 3542 | pid_t *array; |
| 3543 | int length; |
| 3544 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3545 | struct cgroup_iter it; |
| 3546 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3547 | struct cgroup_pidlist *l; |
| 3548 | |
| 3549 | /* |
| 3550 | * If cgroup gets more users after we read count, we won't have |
| 3551 | * enough space - tough. This race is indistinguishable to the |
| 3552 | * caller from the case that the additional cgroup users didn't |
| 3553 | * show up until sometime later on. |
| 3554 | */ |
| 3555 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3556 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3557 | if (!array) |
| 3558 | return -ENOMEM; |
| 3559 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3560 | cgroup_iter_start(cgrp, &it); |
| 3561 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3562 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3563 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3564 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3565 | if (type == CGROUP_FILE_PROCS) |
| 3566 | pid = task_tgid_vnr(tsk); |
| 3567 | else |
| 3568 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3569 | if (pid > 0) /* make sure to only use valid results */ |
| 3570 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3571 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3572 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3573 | length = n; |
| 3574 | /* now sort & (if procs) strip out duplicates */ |
| 3575 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3576 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3577 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3578 | l = cgroup_pidlist_find(cgrp, type); |
| 3579 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3580 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3581 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3582 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3583 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3584 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3585 | l->list = array; |
| 3586 | l->length = length; |
| 3587 | l->use_count++; |
| 3588 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3589 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3590 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3593 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3594 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3595 | * @stats: cgroupstats to fill information into |
| 3596 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3597 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3598 | * |
| 3599 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3600 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3601 | */ |
| 3602 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3603 | { |
| 3604 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3605 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3606 | struct cgroup_iter it; |
| 3607 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3608 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3609 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3610 | * Validate dentry by checking the superblock operations, |
| 3611 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3612 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3613 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3614 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3615 | goto err; |
| 3616 | |
| 3617 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3618 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3619 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3620 | cgroup_iter_start(cgrp, &it); |
| 3621 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3622 | switch (tsk->state) { |
| 3623 | case TASK_RUNNING: |
| 3624 | stats->nr_running++; |
| 3625 | break; |
| 3626 | case TASK_INTERRUPTIBLE: |
| 3627 | stats->nr_sleeping++; |
| 3628 | break; |
| 3629 | case TASK_UNINTERRUPTIBLE: |
| 3630 | stats->nr_uninterruptible++; |
| 3631 | break; |
| 3632 | case TASK_STOPPED: |
| 3633 | stats->nr_stopped++; |
| 3634 | break; |
| 3635 | default: |
| 3636 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3637 | stats->nr_io_wait++; |
| 3638 | break; |
| 3639 | } |
| 3640 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3641 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3642 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3643 | err: |
| 3644 | return ret; |
| 3645 | } |
| 3646 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3647 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3648 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3649 | * 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] | 3650 | * 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] | 3651 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3652 | */ |
| 3653 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3654 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3655 | { |
| 3656 | /* |
| 3657 | * Initially we receive a position value that corresponds to |
| 3658 | * one more than the last pid shown (or 0 on the first call or |
| 3659 | * after a seek to the start). Use a binary-search to find the |
| 3660 | * next pid to display, if any |
| 3661 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3662 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3663 | int index = 0, pid = *pos; |
| 3664 | int *iter; |
| 3665 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3666 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3667 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3668 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3669 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3670 | while (index < end) { |
| 3671 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3672 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3673 | index = mid; |
| 3674 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | index = mid + 1; |
| 3677 | else |
| 3678 | end = mid; |
| 3679 | } |
| 3680 | } |
| 3681 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3682 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3683 | return NULL; |
| 3684 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3685 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3686 | *pos = *iter; |
| 3687 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3688 | } |
| 3689 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3690 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3691 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3692 | struct cgroup_pidlist *l = s->private; |
| 3693 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3696 | 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] | 3697 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3698 | struct cgroup_pidlist *l = s->private; |
| 3699 | pid_t *p = v; |
| 3700 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3701 | /* |
| 3702 | * Advance to the next pid in the array. If this goes off the |
| 3703 | * end, we're done |
| 3704 | */ |
| 3705 | p++; |
| 3706 | if (p >= end) { |
| 3707 | return NULL; |
| 3708 | } else { |
| 3709 | *pos = *p; |
| 3710 | return p; |
| 3711 | } |
| 3712 | } |
| 3713 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3714 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3715 | { |
| 3716 | return seq_printf(s, "%d\n", *(int *)v); |
| 3717 | } |
| 3718 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3719 | /* |
| 3720 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3721 | * independent of whether it's tasks or procs |
| 3722 | */ |
| 3723 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3724 | .start = cgroup_pidlist_start, |
| 3725 | .stop = cgroup_pidlist_stop, |
| 3726 | .next = cgroup_pidlist_next, |
| 3727 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3728 | }; |
| 3729 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3730 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3731 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3732 | /* |
| 3733 | * the case where we're the last user of this particular pidlist will |
| 3734 | * have us remove it from the cgroup's list, which entails taking the |
| 3735 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3736 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3737 | */ |
| 3738 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3739 | down_write(&l->mutex); |
| 3740 | BUG_ON(!l->use_count); |
| 3741 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3742 | /* we're the last user if refcount is 0; remove and free */ |
| 3743 | list_del(&l->links); |
| 3744 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3745 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3746 | put_pid_ns(l->key.ns); |
| 3747 | up_write(&l->mutex); |
| 3748 | kfree(l); |
| 3749 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3750 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3751 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3752 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3755 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3756 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3757 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3758 | if (!(file->f_mode & FMODE_READ)) |
| 3759 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3760 | /* |
| 3761 | * the seq_file will only be initialized if the file was opened for |
| 3762 | * reading; hence we check if it's not null only in that case. |
| 3763 | */ |
| 3764 | l = ((struct seq_file *)file->private_data)->private; |
| 3765 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3766 | return seq_release(inode, file); |
| 3767 | } |
| 3768 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3769 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3770 | .read = seq_read, |
| 3771 | .llseek = seq_lseek, |
| 3772 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3773 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3774 | }; |
| 3775 | |
| 3776 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3777 | * The following functions handle opens on a file that displays a pidlist |
| 3778 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3779 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3780 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3781 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3782 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3783 | { |
| 3784 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3785 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3786 | int retval; |
| 3787 | |
| 3788 | /* Nothing to do for write-only files */ |
| 3789 | if (!(file->f_mode & FMODE_READ)) |
| 3790 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3791 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3792 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3793 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3794 | if (retval) |
| 3795 | return retval; |
| 3796 | /* configure file information */ |
| 3797 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3798 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3799 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3800 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3801 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3802 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3803 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3804 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3805 | return 0; |
| 3806 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3807 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3808 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3809 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3810 | } |
| 3811 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3812 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3813 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3814 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3815 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3816 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3817 | struct cftype *cft) |
| 3818 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3819 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3820 | } |
| 3821 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3822 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3823 | struct cftype *cft, |
| 3824 | u64 val) |
| 3825 | { |
| 3826 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3827 | if (val) |
| 3828 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3829 | else |
| 3830 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3831 | return 0; |
| 3832 | } |
| 3833 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3834 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3835 | * When dput() is called asynchronously, if umount has been done and |
| 3836 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3837 | * there's a small window that vfs will see the root dentry with non-zero |
| 3838 | * refcnt and trigger BUG(). |
| 3839 | * |
| 3840 | * That's why we hold a reference before dput() and drop it right after. |
| 3841 | */ |
| 3842 | static void cgroup_dput(struct cgroup *cgrp) |
| 3843 | { |
| 3844 | struct super_block *sb = cgrp->root->sb; |
| 3845 | |
| 3846 | atomic_inc(&sb->s_active); |
| 3847 | dput(cgrp->dentry); |
| 3848 | deactivate_super(sb); |
| 3849 | } |
| 3850 | |
| 3851 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3852 | * Unregister event and free resources. |
| 3853 | * |
| 3854 | * Gets called from workqueue. |
| 3855 | */ |
| 3856 | static void cgroup_event_remove(struct work_struct *work) |
| 3857 | { |
| 3858 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3859 | remove); |
| 3860 | struct cgroup *cgrp = event->cgrp; |
| 3861 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3862 | remove_wait_queue(event->wqh, &event->wait); |
| 3863 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3864 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3865 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3866 | /* Notify userspace the event is going away. */ |
| 3867 | eventfd_signal(event->eventfd, 1); |
| 3868 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3869 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3870 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3871 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | /* |
| 3875 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3876 | * |
| 3877 | * Called with wqh->lock held and interrupts disabled. |
| 3878 | */ |
| 3879 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3880 | int sync, void *key) |
| 3881 | { |
| 3882 | struct cgroup_event *event = container_of(wait, |
| 3883 | struct cgroup_event, wait); |
| 3884 | struct cgroup *cgrp = event->cgrp; |
| 3885 | unsigned long flags = (unsigned long)key; |
| 3886 | |
| 3887 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3888 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3889 | * If the event has been detached at cgroup removal, we |
| 3890 | * can simply return knowing the other side will cleanup |
| 3891 | * for us. |
| 3892 | * |
| 3893 | * We can't race against event freeing since the other |
| 3894 | * side will require wqh->lock via remove_wait_queue(), |
| 3895 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3896 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3897 | spin_lock(&cgrp->event_list_lock); |
| 3898 | if (!list_empty(&event->list)) { |
| 3899 | list_del_init(&event->list); |
| 3900 | /* |
| 3901 | * We are in atomic context, but cgroup_event_remove() |
| 3902 | * may sleep, so we have to call it in workqueue. |
| 3903 | */ |
| 3904 | schedule_work(&event->remove); |
| 3905 | } |
| 3906 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | return 0; |
| 3910 | } |
| 3911 | |
| 3912 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3913 | wait_queue_head_t *wqh, poll_table *pt) |
| 3914 | { |
| 3915 | struct cgroup_event *event = container_of(pt, |
| 3916 | struct cgroup_event, pt); |
| 3917 | |
| 3918 | event->wqh = wqh; |
| 3919 | add_wait_queue(wqh, &event->wait); |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * Parse input and register new cgroup event handler. |
| 3924 | * |
| 3925 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3926 | * Interpretation of args is defined by control file implementation. |
| 3927 | */ |
| 3928 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3929 | const char *buffer) |
| 3930 | { |
| 3931 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3932 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3933 | unsigned int efd, cfd; |
| 3934 | struct file *efile = NULL; |
| 3935 | struct file *cfile = NULL; |
| 3936 | char *endp; |
| 3937 | int ret; |
| 3938 | |
| 3939 | efd = simple_strtoul(buffer, &endp, 10); |
| 3940 | if (*endp != ' ') |
| 3941 | return -EINVAL; |
| 3942 | buffer = endp + 1; |
| 3943 | |
| 3944 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3945 | if ((*endp != ' ') && (*endp != '\0')) |
| 3946 | return -EINVAL; |
| 3947 | buffer = endp + 1; |
| 3948 | |
| 3949 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3950 | if (!event) |
| 3951 | return -ENOMEM; |
| 3952 | event->cgrp = cgrp; |
| 3953 | INIT_LIST_HEAD(&event->list); |
| 3954 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3955 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3956 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3957 | |
| 3958 | efile = eventfd_fget(efd); |
| 3959 | if (IS_ERR(efile)) { |
| 3960 | ret = PTR_ERR(efile); |
| 3961 | goto fail; |
| 3962 | } |
| 3963 | |
| 3964 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3965 | if (IS_ERR(event->eventfd)) { |
| 3966 | ret = PTR_ERR(event->eventfd); |
| 3967 | goto fail; |
| 3968 | } |
| 3969 | |
| 3970 | cfile = fget(cfd); |
| 3971 | if (!cfile) { |
| 3972 | ret = -EBADF; |
| 3973 | goto fail; |
| 3974 | } |
| 3975 | |
| 3976 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3977 | /* 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] | 3978 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3979 | if (ret < 0) |
| 3980 | goto fail; |
| 3981 | |
| 3982 | event->cft = __file_cft(cfile); |
| 3983 | if (IS_ERR(event->cft)) { |
| 3984 | ret = PTR_ERR(event->cft); |
| 3985 | goto fail; |
| 3986 | } |
| 3987 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3988 | /* |
| 3989 | * The file to be monitored must be in the same cgroup as |
| 3990 | * cgroup.event_control is. |
| 3991 | */ |
| 3992 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 3993 | if (cgrp_cfile != cgrp) { |
| 3994 | ret = -EINVAL; |
| 3995 | goto fail; |
| 3996 | } |
| 3997 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3998 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 3999 | ret = -EINVAL; |
| 4000 | goto fail; |
| 4001 | } |
| 4002 | |
| 4003 | ret = event->cft->register_event(cgrp, event->cft, |
| 4004 | event->eventfd, buffer); |
| 4005 | if (ret) |
| 4006 | goto fail; |
| 4007 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4008 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4009 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4010 | /* |
| 4011 | * Events should be removed after rmdir of cgroup directory, but before |
| 4012 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4013 | * directory dentry to do that. |
| 4014 | */ |
| 4015 | dget(cgrp->dentry); |
| 4016 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4017 | spin_lock(&cgrp->event_list_lock); |
| 4018 | list_add(&event->list, &cgrp->event_list); |
| 4019 | spin_unlock(&cgrp->event_list_lock); |
| 4020 | |
| 4021 | fput(cfile); |
| 4022 | fput(efile); |
| 4023 | |
| 4024 | return 0; |
| 4025 | |
| 4026 | fail: |
| 4027 | if (cfile) |
| 4028 | fput(cfile); |
| 4029 | |
| 4030 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4031 | eventfd_ctx_put(event->eventfd); |
| 4032 | |
| 4033 | if (!IS_ERR_OR_NULL(efile)) |
| 4034 | fput(efile); |
| 4035 | |
| 4036 | kfree(event); |
| 4037 | |
| 4038 | return ret; |
| 4039 | } |
| 4040 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4041 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4042 | struct cftype *cft) |
| 4043 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4044 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4048 | struct cftype *cft, |
| 4049 | u64 val) |
| 4050 | { |
| 4051 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4052 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4053 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4054 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4055 | return 0; |
| 4056 | } |
| 4057 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4058 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4059 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4060 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4061 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4062 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4063 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4064 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4065 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4066 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4067 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4068 | .write_string = cgroup_write_event_control, |
| 4069 | .mode = S_IWUGO, |
| 4070 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4071 | { |
| 4072 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4073 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4074 | .read_u64 = cgroup_clone_children_read, |
| 4075 | .write_u64 = cgroup_clone_children_write, |
| 4076 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4077 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4078 | .name = "cgroup.sane_behavior", |
| 4079 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4080 | .read_seq_string = cgroup_sane_behavior_show, |
| 4081 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4082 | |
| 4083 | /* |
| 4084 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4085 | * don't exist if sane_behavior. If you're depending on these, be |
| 4086 | * prepared to be burned. |
| 4087 | */ |
| 4088 | { |
| 4089 | .name = "tasks", |
| 4090 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4091 | .open = cgroup_tasks_open, |
| 4092 | .write_u64 = cgroup_tasks_write, |
| 4093 | .release = cgroup_pidlist_release, |
| 4094 | .mode = S_IRUGO | S_IWUSR, |
| 4095 | }, |
| 4096 | { |
| 4097 | .name = "notify_on_release", |
| 4098 | .flags = CFTYPE_INSANE, |
| 4099 | .read_u64 = cgroup_read_notify_on_release, |
| 4100 | .write_u64 = cgroup_write_notify_on_release, |
| 4101 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4102 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4103 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4104 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4105 | .read_seq_string = cgroup_release_agent_show, |
| 4106 | .write_string = cgroup_release_agent_write, |
| 4107 | .max_write_len = PATH_MAX, |
| 4108 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4109 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4110 | }; |
| 4111 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4112 | /** |
| 4113 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4114 | * @cgrp: target cgroup |
| 4115 | * @base_files: true if the base files should be added |
| 4116 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4117 | */ |
| 4118 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4119 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4120 | { |
| 4121 | int err; |
| 4122 | struct cgroup_subsys *ss; |
| 4123 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4124 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4125 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4126 | if (err < 0) |
| 4127 | return err; |
| 4128 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4129 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4130 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4131 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4132 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4133 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4134 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4135 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4136 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4137 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4138 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4139 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4140 | /* This cgroup is ready now */ |
| 4141 | for_each_subsys(cgrp->root, ss) { |
| 4142 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4143 | /* |
| 4144 | * Update id->css pointer and make this css visible from |
| 4145 | * CSS ID functions. This pointer will be dereferened |
| 4146 | * from RCU-read-side without locks. |
| 4147 | */ |
| 4148 | if (css->id) |
| 4149 | rcu_assign_pointer(css->id->css, css); |
| 4150 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4151 | |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4155 | static void css_dput_fn(struct work_struct *work) |
| 4156 | { |
| 4157 | struct cgroup_subsys_state *css = |
| 4158 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4159 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4160 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4161 | } |
| 4162 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4163 | static void css_release(struct percpu_ref *ref) |
| 4164 | { |
| 4165 | struct cgroup_subsys_state *css = |
| 4166 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4167 | |
| 4168 | schedule_work(&css->dput_work); |
| 4169 | } |
| 4170 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4171 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4172 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4173 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4174 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4175 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4176 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4177 | css->id = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4178 | if (cgrp == dummytop) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4179 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4180 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4181 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4182 | |
| 4183 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4184 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4185 | * css_put(). dput() requires process context, which css_put() may |
| 4186 | * be called without. @css->dput_work will be used to invoke |
| 4187 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4188 | */ |
| 4189 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4190 | } |
| 4191 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4192 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4193 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4194 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4195 | int ret = 0; |
| 4196 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4197 | lockdep_assert_held(&cgroup_mutex); |
| 4198 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4199 | if (ss->css_online) |
| 4200 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4201 | if (!ret) |
| 4202 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4203 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4207 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4208 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4209 | { |
| 4210 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4211 | |
| 4212 | lockdep_assert_held(&cgroup_mutex); |
| 4213 | |
| 4214 | if (!(css->flags & CSS_ONLINE)) |
| 4215 | return; |
| 4216 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4217 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4218 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4219 | |
| 4220 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4221 | } |
| 4222 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4223 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4224 | * cgroup_create - create a cgroup |
| 4225 | * @parent: cgroup that will be parent of the new cgroup |
| 4226 | * @dentry: dentry of the new cgroup |
| 4227 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4228 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4229 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4230 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4231 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4232 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4233 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4234 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4235 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4236 | struct cgroupfs_root *root = parent->root; |
| 4237 | int err = 0; |
| 4238 | struct cgroup_subsys *ss; |
| 4239 | struct super_block *sb = root->sb; |
| 4240 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4241 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4242 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4243 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4244 | return -ENOMEM; |
| 4245 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4246 | name = cgroup_alloc_name(dentry); |
| 4247 | if (!name) |
| 4248 | goto err_free_cgrp; |
| 4249 | rcu_assign_pointer(cgrp->name, name); |
| 4250 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4251 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4252 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4253 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4254 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4255 | /* |
| 4256 | * Only live parents can have children. Note that the liveliness |
| 4257 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4258 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4259 | * anyway so that locking is contained inside cgroup proper and we |
| 4260 | * don't get nasty surprises if we ever grow another caller. |
| 4261 | */ |
| 4262 | if (!cgroup_lock_live_group(parent)) { |
| 4263 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4264 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4267 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4268 | * get deleted on unmount if there are child cgroups. This |
| 4269 | * can be done outside cgroup_mutex, since the sb can't |
| 4270 | * disappear while someone has an open control file on the |
| 4271 | * fs */ |
| 4272 | atomic_inc(&sb->s_active); |
| 4273 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4274 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4275 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4276 | dentry->d_fsdata = cgrp; |
| 4277 | cgrp->dentry = dentry; |
| 4278 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4279 | cgrp->parent = parent; |
| 4280 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4281 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4282 | if (notify_on_release(parent)) |
| 4283 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4284 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4285 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4286 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4287 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4288 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4289 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4290 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4291 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4292 | if (IS_ERR(css)) { |
| 4293 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4294 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4295 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4296 | |
| 4297 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4298 | if (err) |
| 4299 | goto err_free_all; |
| 4300 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4301 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4302 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4303 | if (ss->use_id) { |
| 4304 | err = alloc_css_id(ss, parent, cgrp); |
| 4305 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4306 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4307 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4308 | } |
| 4309 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4310 | /* |
| 4311 | * Create directory. cgroup_create_file() returns with the new |
| 4312 | * directory locked on success so that it can be populated without |
| 4313 | * dropping cgroup_mutex. |
| 4314 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4315 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4316 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4317 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4318 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4319 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame^] | 4320 | cgrp->serial_nr = atomic64_inc_return(&cgroup_serial_nr_cursor); |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4321 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4322 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4323 | list_add_tail(&cgrp->allcg_node, &root->allcg_list); |
| 4324 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4325 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4326 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4327 | /* each css holds a ref to the cgroup's dentry */ |
| 4328 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4329 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4330 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4331 | /* hold a ref to the parent's dentry */ |
| 4332 | dget(parent->dentry); |
| 4333 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4334 | /* creation succeeded, notify subsystems */ |
| 4335 | for_each_subsys(root, ss) { |
| 4336 | err = online_css(ss, cgrp); |
| 4337 | if (err) |
| 4338 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4339 | |
| 4340 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4341 | parent->parent) { |
| 4342 | 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", |
| 4343 | current->comm, current->pid, ss->name); |
| 4344 | if (!strcmp(ss->name, "memory")) |
| 4345 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4346 | ss->warned_broken_hierarchy = true; |
| 4347 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4348 | } |
| 4349 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4350 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4351 | if (err) |
| 4352 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4353 | |
| 4354 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4355 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4356 | |
| 4357 | return 0; |
| 4358 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4359 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4360 | for_each_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4361 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4362 | |
| 4363 | if (css) { |
| 4364 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4365 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4366 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4367 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4368 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4369 | /* Release the reference count that we took on the superblock */ |
| 4370 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4371 | err_free_id: |
| 4372 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4373 | err_free_name: |
| 4374 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4375 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4376 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4377 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4378 | |
| 4379 | err_destroy: |
| 4380 | cgroup_destroy_locked(cgrp); |
| 4381 | mutex_unlock(&cgroup_mutex); |
| 4382 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4383 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4386 | 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] | 4387 | { |
| 4388 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4389 | |
| 4390 | /* the vfs holds inode->i_mutex already */ |
| 4391 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4392 | } |
| 4393 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4394 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4395 | { |
| 4396 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4397 | return; |
| 4398 | |
| 4399 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4400 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4401 | schedule_work(&cgrp->destroy_work); |
| 4402 | } |
| 4403 | |
| 4404 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4405 | { |
| 4406 | struct cgroup_subsys_state *css = |
| 4407 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4408 | |
| 4409 | cgroup_css_killed(css->cgroup); |
| 4410 | } |
| 4411 | |
| 4412 | /** |
| 4413 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4414 | * @cgrp: cgroup to be destroyed |
| 4415 | * |
| 4416 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4417 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4418 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4419 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4420 | * the following two steps. |
| 4421 | * |
| 4422 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4423 | * userland visible parts and start killing the percpu refcnts of |
| 4424 | * css's. Set up so that the next stage will be kicked off once all |
| 4425 | * the percpu refcnts are confirmed to be killed. |
| 4426 | * |
| 4427 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4428 | * rest of destruction. Once all cgroup references are gone, the |
| 4429 | * cgroup is RCU-freed. |
| 4430 | * |
| 4431 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4432 | * the userland is concerned and a new cgroup with the same name may be |
| 4433 | * created. As cgroup doesn't care about the names internally, this |
| 4434 | * doesn't cause any problem. |
| 4435 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4436 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4437 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4438 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4439 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4440 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4441 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4442 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4443 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4444 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4445 | lockdep_assert_held(&cgroup_mutex); |
| 4446 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4447 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4448 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4449 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4450 | */ |
| 4451 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4452 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4453 | read_unlock(&css_set_lock); |
| 4454 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4455 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4456 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4457 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4458 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4459 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4460 | * css reference will be given out via css_tryget(). We can't |
| 4461 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4462 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4463 | * as killed on all CPUs on return. |
| 4464 | * |
| 4465 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4466 | * css is confirmed to be seen as killed on all CPUs. The |
| 4467 | * notification callback keeps track of the number of css's to be |
| 4468 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4469 | * destruction once the percpu refs of all css's are confirmed to |
| 4470 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4471 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4472 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4473 | for_each_subsys(cgrp->root, ss) { |
| 4474 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4475 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4476 | /* |
| 4477 | * Killing would put the base ref, but we need to keep it |
| 4478 | * alive until after ->css_offline. |
| 4479 | */ |
| 4480 | percpu_ref_get(&css->refcnt); |
| 4481 | |
| 4482 | atomic_inc(&cgrp->css_kill_cnt); |
| 4483 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4484 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4485 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4486 | |
| 4487 | /* |
| 4488 | * Mark @cgrp dead. This prevents further task migration and child |
| 4489 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4490 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4491 | * resume iteration after dropping RCU read lock. See |
| 4492 | * cgroup_next_sibling() for details. |
| 4493 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4494 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4495 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4496 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4497 | raw_spin_lock(&release_list_lock); |
| 4498 | if (!list_empty(&cgrp->release_list)) |
| 4499 | list_del_init(&cgrp->release_list); |
| 4500 | raw_spin_unlock(&release_list_lock); |
| 4501 | |
| 4502 | /* |
| 4503 | * Remove @cgrp directory. The removal puts the base ref but we |
| 4504 | * aren't quite done with @cgrp yet, so hold onto it. |
| 4505 | */ |
| 4506 | dget(d); |
| 4507 | cgroup_d_remove_dir(d); |
| 4508 | |
| 4509 | /* |
| 4510 | * Unregister events and notify userspace. |
| 4511 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4512 | * directory to avoid race between userspace and kernelspace. |
| 4513 | */ |
| 4514 | spin_lock(&cgrp->event_list_lock); |
| 4515 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4516 | list_del_init(&event->list); |
| 4517 | schedule_work(&event->remove); |
| 4518 | } |
| 4519 | spin_unlock(&cgrp->event_list_lock); |
| 4520 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4521 | return 0; |
| 4522 | }; |
| 4523 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4524 | /** |
| 4525 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4526 | * @work: cgroup->destroy_free_work |
| 4527 | * |
| 4528 | * This function is invoked from a work item for a cgroup which is being |
| 4529 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4530 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4531 | * is the second step of destruction described in the comment above |
| 4532 | * cgroup_destroy_locked(). |
| 4533 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4534 | static void cgroup_offline_fn(struct work_struct *work) |
| 4535 | { |
| 4536 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4537 | struct cgroup *parent = cgrp->parent; |
| 4538 | struct dentry *d = cgrp->dentry; |
| 4539 | struct cgroup_subsys *ss; |
| 4540 | |
| 4541 | mutex_lock(&cgroup_mutex); |
| 4542 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4543 | /* |
| 4544 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4545 | * initate destruction. |
| 4546 | */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4547 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4548 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4549 | |
| 4550 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4551 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4552 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4553 | * proceeds regardless of css refs. On the last put of each css, |
| 4554 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4555 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4556 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4557 | for_each_subsys(cgrp->root, ss) |
| 4558 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4559 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4560 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4561 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4562 | list_del_init(&cgrp->allcg_node); |
| 4563 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4564 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4565 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4566 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4567 | check_for_release(parent); |
| 4568 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4569 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4570 | } |
| 4571 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4572 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4573 | { |
| 4574 | int ret; |
| 4575 | |
| 4576 | mutex_lock(&cgroup_mutex); |
| 4577 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4578 | mutex_unlock(&cgroup_mutex); |
| 4579 | |
| 4580 | return ret; |
| 4581 | } |
| 4582 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4583 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4584 | { |
| 4585 | INIT_LIST_HEAD(&ss->cftsets); |
| 4586 | |
| 4587 | /* |
| 4588 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4589 | * deregistration. |
| 4590 | */ |
| 4591 | if (ss->base_cftypes) { |
| 4592 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4593 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4594 | } |
| 4595 | } |
| 4596 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4597 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4598 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4599 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4600 | |
| 4601 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4602 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4603 | mutex_lock(&cgroup_mutex); |
| 4604 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4605 | /* init base cftset */ |
| 4606 | cgroup_init_cftsets(ss); |
| 4607 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4608 | /* Create the top cgroup state for this subsystem */ |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 4609 | list_add(&ss->sibling, &rootnode.subsys_list); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4610 | ss->root = &rootnode; |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4611 | css = ss->css_alloc(dummytop); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4612 | /* We don't handle early failures gracefully */ |
| 4613 | BUG_ON(IS_ERR(css)); |
| 4614 | init_cgroup_css(css, ss, dummytop); |
| 4615 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4616 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4617 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4618 | * newly registered, all tasks and hence the |
| 4619 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4620 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4621 | |
| 4622 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4623 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4624 | /* At system boot, before all subsystems have been |
| 4625 | * registered, no tasks have been forked, so we don't |
| 4626 | * need to invoke fork callbacks here. */ |
| 4627 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4628 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4629 | BUG_ON(online_css(ss, dummytop)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4630 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4631 | mutex_unlock(&cgroup_mutex); |
| 4632 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4633 | /* this function shouldn't be used with modular subsystems, since they |
| 4634 | * need to register a subsys_id, among other things */ |
| 4635 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4639 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4640 | * @ss: the subsystem to load |
| 4641 | * |
| 4642 | * 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] | 4643 | * 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] | 4644 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4645 | * simpler cgroup_init_subsys. |
| 4646 | */ |
| 4647 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4648 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4649 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4650 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4651 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4652 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4653 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4654 | |
| 4655 | /* check name and function validity */ |
| 4656 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4657 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4658 | return -EINVAL; |
| 4659 | |
| 4660 | /* |
| 4661 | * we don't support callbacks in modular subsystems. this check is |
| 4662 | * before the ss->module check for consistency; a subsystem that could |
| 4663 | * be a module should still have no callbacks even if the user isn't |
| 4664 | * compiling it as one. |
| 4665 | */ |
| 4666 | if (ss->fork || ss->exit) |
| 4667 | return -EINVAL; |
| 4668 | |
| 4669 | /* |
| 4670 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4671 | * since cgroup_init_subsys will have already taken care of it. |
| 4672 | */ |
| 4673 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4674 | /* a sanity check */ |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4675 | BUG_ON(subsys[ss->subsys_id] != ss); |
| 4676 | return 0; |
| 4677 | } |
| 4678 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4679 | /* init base cftset */ |
| 4680 | cgroup_init_cftsets(ss); |
| 4681 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4682 | mutex_lock(&cgroup_mutex); |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4683 | subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4684 | |
| 4685 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4686 | * no ss->css_alloc seems to need anything important in the ss |
| 4687 | * struct, so this can happen first (i.e. before the rootnode |
| 4688 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4689 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4690 | css = ss->css_alloc(dummytop); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4691 | if (IS_ERR(css)) { |
| 4692 | /* failure case - need to deassign the subsys[] slot. */ |
Daniel Wagner | 8a8e04d | 2012-09-12 16:12:07 +0200 | [diff] [blame] | 4693 | subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4694 | mutex_unlock(&cgroup_mutex); |
| 4695 | return PTR_ERR(css); |
| 4696 | } |
| 4697 | |
| 4698 | list_add(&ss->sibling, &rootnode.subsys_list); |
| 4699 | ss->root = &rootnode; |
| 4700 | |
| 4701 | /* our new subsystem will be attached to the dummy hierarchy. */ |
| 4702 | init_cgroup_css(css, ss, dummytop); |
| 4703 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4704 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4705 | ret = cgroup_init_idr(ss, css); |
| 4706 | if (ret) |
| 4707 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | /* |
| 4711 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4712 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4713 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4714 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4715 | * table state, so each changed css_set will need its hash recomputed. |
| 4716 | * this is all done under the css_set_lock. |
| 4717 | */ |
| 4718 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4719 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4720 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4721 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4722 | continue; |
| 4723 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4724 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4725 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4726 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4727 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4728 | key = css_set_hash(cset->subsys); |
| 4729 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4730 | } |
| 4731 | write_unlock(&css_set_lock); |
| 4732 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4733 | ret = online_css(ss, dummytop); |
| 4734 | if (ret) |
| 4735 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4736 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4737 | /* success! */ |
| 4738 | mutex_unlock(&cgroup_mutex); |
| 4739 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4740 | |
| 4741 | err_unload: |
| 4742 | mutex_unlock(&cgroup_mutex); |
| 4743 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4744 | cgroup_unload_subsys(ss); |
| 4745 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4746 | } |
| 4747 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4748 | |
| 4749 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4750 | * cgroup_unload_subsys: unload a modular subsystem |
| 4751 | * @ss: the subsystem to unload |
| 4752 | * |
| 4753 | * This function should be called in a modular subsystem's exitcall. When this |
| 4754 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4755 | * the subsystem will not be attached to any hierarchy. |
| 4756 | */ |
| 4757 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4758 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4759 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4760 | |
| 4761 | BUG_ON(ss->module == NULL); |
| 4762 | |
| 4763 | /* |
| 4764 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4765 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4766 | * doesn't start being used while we're killing it off. |
| 4767 | */ |
| 4768 | BUG_ON(ss->root != &rootnode); |
| 4769 | |
| 4770 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4771 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4772 | offline_css(ss, dummytop); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4773 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4774 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4775 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4776 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4777 | /* deassign the subsys_id */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4778 | subsys[ss->subsys_id] = NULL; |
| 4779 | |
| 4780 | /* remove subsystem from rootnode's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4781 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4782 | |
| 4783 | /* |
| 4784 | * disentangle the css from all css_sets attached to the dummytop. as |
| 4785 | * in loading, we need to pay our respects to the hashtable gods. |
| 4786 | */ |
| 4787 | write_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4788 | list_for_each_entry(link, &dummytop->cset_links, cset_link) { |
| 4789 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4790 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4791 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4792 | hash_del(&cset->hlist); |
| 4793 | cset->subsys[ss->subsys_id] = NULL; |
| 4794 | key = css_set_hash(cset->subsys); |
| 4795 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4796 | } |
| 4797 | write_unlock(&css_set_lock); |
| 4798 | |
| 4799 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4800 | * remove subsystem's css from the dummytop and free it - need to |
| 4801 | * free before marking as null because ss->css_free needs the |
| 4802 | * cgrp->subsys pointer to find their state. note that this also |
| 4803 | * takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4804 | */ |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4805 | ss->css_free(dummytop); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4806 | dummytop->subsys[ss->subsys_id] = NULL; |
| 4807 | |
| 4808 | mutex_unlock(&cgroup_mutex); |
| 4809 | } |
| 4810 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4811 | |
| 4812 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4813 | * cgroup_init_early - cgroup initialization at system boot |
| 4814 | * |
| 4815 | * Initialize cgroups at system boot, and initialize any |
| 4816 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4817 | */ |
| 4818 | int __init cgroup_init_early(void) |
| 4819 | { |
| 4820 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4821 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4822 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4823 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4824 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4825 | css_set_count = 1; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4826 | init_cgroup_root(&rootnode); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4827 | root_count = 1; |
| 4828 | init_task.cgroups = &init_css_set; |
| 4829 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4830 | init_cgrp_cset_link.cset = &init_css_set; |
| 4831 | init_cgrp_cset_link.cgrp = dummytop; |
| 4832 | list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links); |
| 4833 | 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] | 4834 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4835 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4836 | struct cgroup_subsys *ss = subsys[i]; |
| 4837 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4838 | /* at bootup time, we don't worry about modular subsystems */ |
| 4839 | if (!ss || ss->module) |
| 4840 | continue; |
| 4841 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4842 | BUG_ON(!ss->name); |
| 4843 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4844 | BUG_ON(!ss->css_alloc); |
| 4845 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4846 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4847 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4848 | ss->name, ss->subsys_id); |
| 4849 | BUG(); |
| 4850 | } |
| 4851 | |
| 4852 | if (ss->early_init) |
| 4853 | cgroup_init_subsys(ss); |
| 4854 | } |
| 4855 | return 0; |
| 4856 | } |
| 4857 | |
| 4858 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4859 | * cgroup_init - cgroup initialization |
| 4860 | * |
| 4861 | * Register cgroup filesystem and /proc file, and initialize |
| 4862 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4863 | */ |
| 4864 | int __init cgroup_init(void) |
| 4865 | { |
| 4866 | int err; |
| 4867 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4868 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4869 | |
| 4870 | err = bdi_init(&cgroup_backing_dev_info); |
| 4871 | if (err) |
| 4872 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4873 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4874 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4875 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4876 | |
| 4877 | /* at bootup time, we don't worry about modular subsystems */ |
| 4878 | if (!ss || ss->module) |
| 4879 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4880 | if (!ss->early_init) |
| 4881 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4882 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4883 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4886 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4887 | key = css_set_hash(init_css_set.subsys); |
| 4888 | hash_add(css_set_table, &init_css_set.hlist, key); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4889 | |
| 4890 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4891 | mutex_lock(&cgroup_mutex); |
| 4892 | mutex_lock(&cgroup_root_mutex); |
| 4893 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4894 | BUG_ON(cgroup_init_root_id(&rootnode)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4895 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4896 | mutex_unlock(&cgroup_root_mutex); |
| 4897 | mutex_unlock(&cgroup_mutex); |
| 4898 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4899 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4900 | if (!cgroup_kobj) { |
| 4901 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4902 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | err = register_filesystem(&cgroup_fs_type); |
| 4906 | if (err < 0) { |
| 4907 | kobject_put(cgroup_kobj); |
| 4908 | goto out; |
| 4909 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4910 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4911 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4912 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4913 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4914 | if (err) |
| 4915 | bdi_destroy(&cgroup_backing_dev_info); |
| 4916 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4917 | return err; |
| 4918 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4919 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4920 | /* |
| 4921 | * proc_cgroup_show() |
| 4922 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4923 | * - Used for /proc/<pid>/cgroup. |
| 4924 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4925 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4926 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4927 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4928 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4929 | * cgroup to top_cgroup. |
| 4930 | */ |
| 4931 | |
| 4932 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4933 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4934 | { |
| 4935 | struct pid *pid; |
| 4936 | struct task_struct *tsk; |
| 4937 | char *buf; |
| 4938 | int retval; |
| 4939 | struct cgroupfs_root *root; |
| 4940 | |
| 4941 | retval = -ENOMEM; |
| 4942 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4943 | if (!buf) |
| 4944 | goto out; |
| 4945 | |
| 4946 | retval = -ESRCH; |
| 4947 | pid = m->private; |
| 4948 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4949 | if (!tsk) |
| 4950 | goto out_free; |
| 4951 | |
| 4952 | retval = 0; |
| 4953 | |
| 4954 | mutex_lock(&cgroup_mutex); |
| 4955 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4956 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4957 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4958 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4959 | int count = 0; |
| 4960 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4961 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4962 | for_each_subsys(root, ss) |
| 4963 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4964 | if (strlen(root->name)) |
| 4965 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4966 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4967 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4968 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4969 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4970 | if (retval < 0) |
| 4971 | goto out_unlock; |
| 4972 | seq_puts(m, buf); |
| 4973 | seq_putc(m, '\n'); |
| 4974 | } |
| 4975 | |
| 4976 | out_unlock: |
| 4977 | mutex_unlock(&cgroup_mutex); |
| 4978 | put_task_struct(tsk); |
| 4979 | out_free: |
| 4980 | kfree(buf); |
| 4981 | out: |
| 4982 | return retval; |
| 4983 | } |
| 4984 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4985 | /* Display information about each subsystem and each hierarchy */ |
| 4986 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4987 | { |
| 4988 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4989 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4990 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4991 | /* |
| 4992 | * ideally we don't want subsystems moving around while we do this. |
| 4993 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 4994 | * subsys/hierarchy state. |
| 4995 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4996 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4997 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
| 4998 | struct cgroup_subsys *ss = subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4999 | if (ss == NULL) |
| 5000 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5001 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5002 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5003 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5004 | } |
| 5005 | mutex_unlock(&cgroup_mutex); |
| 5006 | return 0; |
| 5007 | } |
| 5008 | |
| 5009 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5010 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5011 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5012 | } |
| 5013 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5014 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5015 | .open = cgroupstats_open, |
| 5016 | .read = seq_read, |
| 5017 | .llseek = seq_lseek, |
| 5018 | .release = single_release, |
| 5019 | }; |
| 5020 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5021 | /** |
| 5022 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5023 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5024 | * |
| 5025 | * Description: A task inherits its parent's cgroup at fork(). |
| 5026 | * |
| 5027 | * A pointer to the shared css_set was automatically copied in |
| 5028 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5029 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5030 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5031 | * have already changed current->cgroups, allowing the previously |
| 5032 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5033 | * |
| 5034 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5035 | * task, and the passed argument 'child' points to the child task. |
| 5036 | */ |
| 5037 | void cgroup_fork(struct task_struct *child) |
| 5038 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5039 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5040 | child->cgroups = current->cgroups; |
| 5041 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5042 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5043 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5047 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5048 | * @child: the task in question |
| 5049 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5050 | * Adds the task to the list running through its css_set if necessary and |
| 5051 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5052 | * visible on the task list in case we race with the first call to |
| 5053 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5054 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5055 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5056 | void cgroup_post_fork(struct task_struct *child) |
| 5057 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5058 | int i; |
| 5059 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5060 | /* |
| 5061 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5062 | * under the tasklist_lock and we read it here after we added the child |
| 5063 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5064 | * yet in the tasklist when we walked through it from |
| 5065 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5066 | * should be visible now due to the paired locking and barriers implied |
| 5067 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5068 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5069 | * lock on fork. |
| 5070 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5071 | if (use_task_css_set_links) { |
| 5072 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5073 | task_lock(child); |
| 5074 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5075 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5076 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5077 | write_unlock(&css_set_lock); |
| 5078 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5079 | |
| 5080 | /* |
| 5081 | * Call ss->fork(). This must happen after @child is linked on |
| 5082 | * css_set; otherwise, @child might change state between ->fork() |
| 5083 | * and addition to css_set. |
| 5084 | */ |
| 5085 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5086 | /* |
| 5087 | * fork/exit callbacks are supported only for builtin |
| 5088 | * subsystems, and the builtin section of the subsys |
| 5089 | * array is immutable, so we don't need to lock the |
| 5090 | * subsys array here. On the other hand, modular section |
| 5091 | * of the array can be freed at module unload, so we |
| 5092 | * can't touch that. |
| 5093 | */ |
| 5094 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5095 | struct cgroup_subsys *ss = subsys[i]; |
| 5096 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5097 | if (ss->fork) |
| 5098 | ss->fork(child); |
| 5099 | } |
| 5100 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5101 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5102 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5103 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5104 | * cgroup_exit - detach cgroup from exiting task |
| 5105 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5106 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5107 | * |
| 5108 | * Description: Detach cgroup from @tsk and release it. |
| 5109 | * |
| 5110 | * Note that cgroups marked notify_on_release force every task in |
| 5111 | * them to take the global cgroup_mutex mutex when exiting. |
| 5112 | * This could impact scaling on very large systems. Be reluctant to |
| 5113 | * use notify_on_release cgroups where very high task exit scaling |
| 5114 | * is required on large systems. |
| 5115 | * |
| 5116 | * the_top_cgroup_hack: |
| 5117 | * |
| 5118 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5119 | * |
| 5120 | * We call cgroup_exit() while the task is still competent to |
| 5121 | * handle notify_on_release(), then leave the task attached to the |
| 5122 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5123 | * |
| 5124 | * To do this properly, we would increment the reference count on |
| 5125 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5126 | * code we would add a second cgroup function call, to drop that |
| 5127 | * reference. This would just create an unnecessary hot spot on |
| 5128 | * the top_cgroup reference count, to no avail. |
| 5129 | * |
| 5130 | * Normally, holding a reference to a cgroup without bumping its |
| 5131 | * count is unsafe. The cgroup could go away, or someone could |
| 5132 | * attach us to a different cgroup, decrementing the count on |
| 5133 | * the first cgroup that we never incremented. But in this case, |
| 5134 | * 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] | 5135 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5136 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5137 | */ |
| 5138 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5139 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5140 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5141 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5142 | |
| 5143 | /* |
| 5144 | * Unlink from the css_set task list if necessary. |
| 5145 | * Optimistically check cg_list before taking |
| 5146 | * css_set_lock |
| 5147 | */ |
| 5148 | if (!list_empty(&tsk->cg_list)) { |
| 5149 | write_lock(&css_set_lock); |
| 5150 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5151 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5152 | write_unlock(&css_set_lock); |
| 5153 | } |
| 5154 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5155 | /* Reassign the task to the init_css_set. */ |
| 5156 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5157 | cset = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5158 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5159 | |
| 5160 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5161 | /* |
| 5162 | * fork/exit callbacks are supported only for builtin |
| 5163 | * subsystems, see cgroup_post_fork() for details. |
| 5164 | */ |
| 5165 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5166 | struct cgroup_subsys *ss = subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5167 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5168 | if (ss->exit) { |
| 5169 | struct cgroup *old_cgrp = |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5170 | rcu_dereference_raw(cset->subsys[i])->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5171 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5172 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5173 | } |
| 5174 | } |
| 5175 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5176 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5177 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5178 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5179 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5180 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5181 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5182 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5183 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5184 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5185 | /* |
| 5186 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5187 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5188 | * it now |
| 5189 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5190 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5191 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5192 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5193 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5194 | list_empty(&cgrp->release_list)) { |
| 5195 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5196 | need_schedule_work = 1; |
| 5197 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5198 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5199 | if (need_schedule_work) |
| 5200 | schedule_work(&release_agent_work); |
| 5201 | } |
| 5202 | } |
| 5203 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5204 | /* |
| 5205 | * Notify userspace when a cgroup is released, by running the |
| 5206 | * configured release agent with the name of the cgroup (path |
| 5207 | * relative to the root of cgroup file system) as the argument. |
| 5208 | * |
| 5209 | * Most likely, this user command will try to rmdir this cgroup. |
| 5210 | * |
| 5211 | * This races with the possibility that some other task will be |
| 5212 | * attached to this cgroup before it is removed, or that some other |
| 5213 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5214 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5215 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5216 | * to continue to serve a useful existence. Next time it's released, |
| 5217 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5218 | * |
| 5219 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5220 | * means only wait until the task is successfully execve()'d. The |
| 5221 | * separate release agent task is forked by call_usermodehelper(), |
| 5222 | * then control in this thread returns here, without waiting for the |
| 5223 | * release agent task. We don't bother to wait because the caller of |
| 5224 | * this routine has no use for the exit status of the release agent |
| 5225 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5226 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5227 | static void cgroup_release_agent(struct work_struct *work) |
| 5228 | { |
| 5229 | BUG_ON(work != &release_agent_work); |
| 5230 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5231 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5232 | while (!list_empty(&release_list)) { |
| 5233 | char *argv[3], *envp[3]; |
| 5234 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5235 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5236 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5237 | struct cgroup, |
| 5238 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5239 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5240 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5241 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5242 | if (!pathbuf) |
| 5243 | goto continue_free; |
| 5244 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5245 | goto continue_free; |
| 5246 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5247 | if (!agentbuf) |
| 5248 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5249 | |
| 5250 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5251 | argv[i++] = agentbuf; |
| 5252 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5253 | argv[i] = NULL; |
| 5254 | |
| 5255 | i = 0; |
| 5256 | /* minimal command environment */ |
| 5257 | envp[i++] = "HOME=/"; |
| 5258 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5259 | envp[i] = NULL; |
| 5260 | |
| 5261 | /* Drop the lock while we invoke the usermode helper, |
| 5262 | * since the exec could involve hitting disk and hence |
| 5263 | * be a slow process */ |
| 5264 | mutex_unlock(&cgroup_mutex); |
| 5265 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5266 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5267 | continue_free: |
| 5268 | kfree(pathbuf); |
| 5269 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5270 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5271 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5272 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5273 | mutex_unlock(&cgroup_mutex); |
| 5274 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5275 | |
| 5276 | static int __init cgroup_disable(char *str) |
| 5277 | { |
| 5278 | int i; |
| 5279 | char *token; |
| 5280 | |
| 5281 | while ((token = strsep(&str, ",")) != NULL) { |
| 5282 | if (!*token) |
| 5283 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5284 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5285 | struct cgroup_subsys *ss = subsys[i]; |
| 5286 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5287 | /* |
| 5288 | * cgroup_disable, being at boot time, can't |
| 5289 | * know about module subsystems, so we don't |
| 5290 | * worry about them. |
| 5291 | */ |
| 5292 | if (!ss || ss->module) |
| 5293 | continue; |
| 5294 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5295 | if (!strcmp(token, ss->name)) { |
| 5296 | ss->disabled = 1; |
| 5297 | printk(KERN_INFO "Disabling %s control group" |
| 5298 | " subsystem\n", ss->name); |
| 5299 | break; |
| 5300 | } |
| 5301 | } |
| 5302 | } |
| 5303 | return 1; |
| 5304 | } |
| 5305 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5306 | |
| 5307 | /* |
| 5308 | * Functons for CSS ID. |
| 5309 | */ |
| 5310 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5311 | /* 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] | 5312 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5313 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5314 | struct css_id *cssid; |
| 5315 | |
| 5316 | /* |
| 5317 | * This css_id() can return correct value when somone has refcnt |
| 5318 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5319 | * it's unchanged until freed. |
| 5320 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5321 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5322 | |
| 5323 | if (cssid) |
| 5324 | return cssid->id; |
| 5325 | return 0; |
| 5326 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5327 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5328 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5329 | /** |
| 5330 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5331 | * @child: the css to be tested. |
| 5332 | * @root: the css supporsed to be an ancestor of the child. |
| 5333 | * |
| 5334 | * 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] | 5335 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5336 | * But, considering usual usage, the csses should be valid objects after test. |
| 5337 | * Assuming that the caller will do some action to the child if this returns |
| 5338 | * returns true, the caller must take "child";s reference count. |
| 5339 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5340 | */ |
| 5341 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5342 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5343 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5344 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5345 | struct css_id *child_id; |
| 5346 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5347 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5348 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5349 | if (!child_id) |
| 5350 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5351 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5352 | if (!root_id) |
| 5353 | return false; |
| 5354 | if (child_id->depth < root_id->depth) |
| 5355 | return false; |
| 5356 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5357 | return false; |
| 5358 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5361 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5362 | { |
| 5363 | struct css_id *id = css->id; |
| 5364 | /* When this is called before css_id initialization, id can be NULL */ |
| 5365 | if (!id) |
| 5366 | return; |
| 5367 | |
| 5368 | BUG_ON(!ss->use_id); |
| 5369 | |
| 5370 | rcu_assign_pointer(id->css, NULL); |
| 5371 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5372 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5373 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5374 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5375 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5376 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5377 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5378 | |
| 5379 | /* |
| 5380 | * This is called by init or create(). Then, calls to this function are |
| 5381 | * always serialized (By cgroup_mutex() at create()). |
| 5382 | */ |
| 5383 | |
| 5384 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5385 | { |
| 5386 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5387 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5388 | |
| 5389 | BUG_ON(!ss->use_id); |
| 5390 | |
| 5391 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5392 | newid = kzalloc(size, GFP_KERNEL); |
| 5393 | if (!newid) |
| 5394 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5395 | |
| 5396 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5397 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5398 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5399 | 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] | 5400 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5401 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5402 | |
| 5403 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5404 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5405 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5406 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5407 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5408 | newid->depth = depth; |
| 5409 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5410 | err_out: |
| 5411 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5412 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5413 | |
| 5414 | } |
| 5415 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5416 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5417 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5418 | { |
| 5419 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5420 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5421 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5422 | idr_init(&ss->idr); |
| 5423 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5424 | newid = get_new_cssid(ss, 0); |
| 5425 | if (IS_ERR(newid)) |
| 5426 | return PTR_ERR(newid); |
| 5427 | |
| 5428 | newid->stack[0] = newid->id; |
| 5429 | newid->css = rootcss; |
| 5430 | rootcss->id = newid; |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
| 5434 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5435 | struct cgroup *child) |
| 5436 | { |
| 5437 | int subsys_id, i, depth = 0; |
| 5438 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5439 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5440 | |
| 5441 | subsys_id = ss->subsys_id; |
| 5442 | parent_css = parent->subsys[subsys_id]; |
| 5443 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5444 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5445 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5446 | |
| 5447 | child_id = get_new_cssid(ss, depth); |
| 5448 | if (IS_ERR(child_id)) |
| 5449 | return PTR_ERR(child_id); |
| 5450 | |
| 5451 | for (i = 0; i < depth; i++) |
| 5452 | child_id->stack[i] = parent_id->stack[i]; |
| 5453 | child_id->stack[depth] = child_id->id; |
| 5454 | /* |
| 5455 | * child_id->css pointer will be set after this cgroup is available |
| 5456 | * see cgroup_populate_dir() |
| 5457 | */ |
| 5458 | rcu_assign_pointer(child_css->id, child_id); |
| 5459 | |
| 5460 | return 0; |
| 5461 | } |
| 5462 | |
| 5463 | /** |
| 5464 | * css_lookup - lookup css by id |
| 5465 | * @ss: cgroup subsys to be looked into. |
| 5466 | * @id: the id |
| 5467 | * |
| 5468 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5469 | * NULL if not. Should be called under rcu_read_lock() |
| 5470 | */ |
| 5471 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5472 | { |
| 5473 | struct css_id *cssid = NULL; |
| 5474 | |
| 5475 | BUG_ON(!ss->use_id); |
| 5476 | cssid = idr_find(&ss->idr, id); |
| 5477 | |
| 5478 | if (unlikely(!cssid)) |
| 5479 | return NULL; |
| 5480 | |
| 5481 | return rcu_dereference(cssid->css); |
| 5482 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5483 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5484 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5485 | /* |
| 5486 | * get corresponding css from file open on cgroupfs directory |
| 5487 | */ |
| 5488 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5489 | { |
| 5490 | struct cgroup *cgrp; |
| 5491 | struct inode *inode; |
| 5492 | struct cgroup_subsys_state *css; |
| 5493 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5494 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5495 | /* check in cgroup filesystem dir */ |
| 5496 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5497 | return ERR_PTR(-EBADF); |
| 5498 | |
| 5499 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5500 | return ERR_PTR(-EINVAL); |
| 5501 | |
| 5502 | /* get cgroup */ |
| 5503 | cgrp = __d_cgrp(f->f_dentry); |
| 5504 | css = cgrp->subsys[id]; |
| 5505 | return css ? css : ERR_PTR(-ENOENT); |
| 5506 | } |
| 5507 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5508 | #ifdef CONFIG_CGROUP_DEBUG |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5509 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5510 | { |
| 5511 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5512 | |
| 5513 | if (!css) |
| 5514 | return ERR_PTR(-ENOMEM); |
| 5515 | |
| 5516 | return css; |
| 5517 | } |
| 5518 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5519 | static void debug_css_free(struct cgroup *cont) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5520 | { |
| 5521 | kfree(cont->subsys[debug_subsys_id]); |
| 5522 | } |
| 5523 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5524 | static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft) |
| 5525 | { |
| 5526 | return cgroup_task_count(cont); |
| 5527 | } |
| 5528 | |
| 5529 | static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft) |
| 5530 | { |
| 5531 | return (u64)(unsigned long)current->cgroups; |
| 5532 | } |
| 5533 | |
| 5534 | static u64 current_css_set_refcount_read(struct cgroup *cont, |
| 5535 | struct cftype *cft) |
| 5536 | { |
| 5537 | u64 count; |
| 5538 | |
| 5539 | rcu_read_lock(); |
| 5540 | count = atomic_read(¤t->cgroups->refcount); |
| 5541 | rcu_read_unlock(); |
| 5542 | return count; |
| 5543 | } |
| 5544 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5545 | static int current_css_set_cg_links_read(struct cgroup *cont, |
| 5546 | struct cftype *cft, |
| 5547 | struct seq_file *seq) |
| 5548 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5549 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5550 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5551 | |
| 5552 | read_lock(&css_set_lock); |
| 5553 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5554 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5555 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5556 | struct cgroup *c = link->cgrp; |
| 5557 | const char *name; |
| 5558 | |
| 5559 | if (c->dentry) |
| 5560 | name = c->dentry->d_name.name; |
| 5561 | else |
| 5562 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5563 | seq_printf(seq, "Root %d group %s\n", |
| 5564 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5565 | } |
| 5566 | rcu_read_unlock(); |
| 5567 | read_unlock(&css_set_lock); |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 5572 | static int cgroup_css_links_read(struct cgroup *cont, |
| 5573 | struct cftype *cft, |
| 5574 | struct seq_file *seq) |
| 5575 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5576 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5577 | |
| 5578 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5579 | list_for_each_entry(link, &cont->cset_links, cset_link) { |
| 5580 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5581 | struct task_struct *task; |
| 5582 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5583 | seq_printf(seq, "css_set %p\n", cset); |
| 5584 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5585 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5586 | seq_puts(seq, " ...\n"); |
| 5587 | break; |
| 5588 | } else { |
| 5589 | seq_printf(seq, " task %d\n", |
| 5590 | task_pid_vnr(task)); |
| 5591 | } |
| 5592 | } |
| 5593 | } |
| 5594 | read_unlock(&css_set_lock); |
| 5595 | return 0; |
| 5596 | } |
| 5597 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5598 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5599 | { |
| 5600 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5601 | } |
| 5602 | |
| 5603 | static struct cftype debug_files[] = { |
| 5604 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5605 | .name = "taskcount", |
| 5606 | .read_u64 = debug_taskcount_read, |
| 5607 | }, |
| 5608 | |
| 5609 | { |
| 5610 | .name = "current_css_set", |
| 5611 | .read_u64 = current_css_set_read, |
| 5612 | }, |
| 5613 | |
| 5614 | { |
| 5615 | .name = "current_css_set_refcount", |
| 5616 | .read_u64 = current_css_set_refcount_read, |
| 5617 | }, |
| 5618 | |
| 5619 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5620 | .name = "current_css_set_cg_links", |
| 5621 | .read_seq_string = current_css_set_cg_links_read, |
| 5622 | }, |
| 5623 | |
| 5624 | { |
| 5625 | .name = "cgroup_css_links", |
| 5626 | .read_seq_string = cgroup_css_links_read, |
| 5627 | }, |
| 5628 | |
| 5629 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5630 | .name = "releasable", |
| 5631 | .read_u64 = releasable_read, |
| 5632 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5633 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5634 | { } /* terminate */ |
| 5635 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5636 | |
| 5637 | struct cgroup_subsys debug_subsys = { |
| 5638 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5639 | .css_alloc = debug_css_alloc, |
| 5640 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5641 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5642 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5643 | }; |
| 5644 | #endif /* CONFIG_CGROUP_DEBUG */ |