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) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 99 | static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 100 | #include <linux/cgroup_subsys.h> |
| 101 | }; |
| 102 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 103 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 104 | * The dummy hierarchy, reserved for the subsystems that are otherwise |
| 105 | * unattached - it never has more than a single cgroup, and all tasks are |
| 106 | * part of that cgroup. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 107 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 108 | static struct cgroupfs_root cgroup_dummy_root; |
| 109 | |
| 110 | /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */ |
| 111 | static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 112 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 113 | /* |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 114 | * cgroupfs file entry, pointed to from leaf dentry->d_fsdata. |
| 115 | */ |
| 116 | struct cfent { |
| 117 | struct list_head node; |
| 118 | struct dentry *dentry; |
| 119 | struct cftype *type; |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 120 | |
| 121 | /* file xattrs */ |
| 122 | struct simple_xattrs xattrs; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 126 | * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when |
| 127 | * cgroup_subsys->use_id != 0. |
| 128 | */ |
| 129 | #define CSS_ID_MAX (65535) |
| 130 | struct css_id { |
| 131 | /* |
| 132 | * The css to which this ID points. This pointer is set to valid value |
| 133 | * after cgroup is populated. If cgroup is removed, this will be NULL. |
| 134 | * This pointer is expected to be RCU-safe because destroy() |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 135 | * is called after synchronize_rcu(). But for safe use, css_tryget() |
| 136 | * should be used for avoiding race. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 137 | */ |
Arnd Bergmann | 2c392b8 | 2010-02-24 19:41:39 +0100 | [diff] [blame] | 138 | struct cgroup_subsys_state __rcu *css; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 139 | /* |
| 140 | * ID of this css. |
| 141 | */ |
| 142 | unsigned short id; |
| 143 | /* |
| 144 | * Depth in hierarchy which this ID belongs to. |
| 145 | */ |
| 146 | unsigned short depth; |
| 147 | /* |
| 148 | * ID is freed by RCU. (and lookup routine is RCU safe.) |
| 149 | */ |
| 150 | struct rcu_head rcu_head; |
| 151 | /* |
| 152 | * Hierarchy of CSS ID belongs to. |
| 153 | */ |
| 154 | unsigned short stack[0]; /* Array of Length (depth+1) */ |
| 155 | }; |
| 156 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 157 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 158 | * cgroup_event represents events which userspace want to receive. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 159 | */ |
| 160 | struct cgroup_event { |
| 161 | /* |
| 162 | * Cgroup which the event belongs to. |
| 163 | */ |
| 164 | struct cgroup *cgrp; |
| 165 | /* |
| 166 | * Control file which the event associated. |
| 167 | */ |
| 168 | struct cftype *cft; |
| 169 | /* |
| 170 | * eventfd to signal userspace about the event. |
| 171 | */ |
| 172 | struct eventfd_ctx *eventfd; |
| 173 | /* |
| 174 | * Each of these stored in a list by the cgroup. |
| 175 | */ |
| 176 | struct list_head list; |
| 177 | /* |
| 178 | * All fields below needed to unregister event when |
| 179 | * userspace closes eventfd. |
| 180 | */ |
| 181 | poll_table pt; |
| 182 | wait_queue_head_t *wqh; |
| 183 | wait_queue_t wait; |
| 184 | struct work_struct remove; |
| 185 | }; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 186 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 187 | /* The list of hierarchy roots */ |
| 188 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 189 | static LIST_HEAD(cgroup_roots); |
| 190 | static int cgroup_root_count; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 191 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Hierarchy ID allocation and mapping. It follows the same exclusion |
| 194 | * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for |
| 195 | * writes, either for reads. |
| 196 | */ |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 197 | static DEFINE_IDR(cgroup_hierarchy_idr); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 198 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 199 | static struct cgroup_name root_cgroup_name = { .name = "/" }; |
| 200 | |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 201 | /* |
| 202 | * Assign a monotonically increasing serial number to cgroups. It |
| 203 | * guarantees cgroups with bigger numbers are newer than those with smaller |
| 204 | * numbers. Also, as cgroups are always appended to the parent's |
| 205 | * ->children list, it guarantees that sibling cgroups are always sorted in |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 206 | * the ascending serial number order on the list. Protected by |
| 207 | * cgroup_mutex. |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 208 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 209 | static u64 cgroup_serial_nr_next = 1; |
Li Zefan | 794611a | 2013-06-18 18:53:53 +0800 | [diff] [blame] | 210 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 211 | /* This flag indicates whether tasks in the fork and exit paths should |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 212 | * check for fork/exit handlers to call. This avoids us having to do |
| 213 | * extra work in the fork/exit path if none of the subsystems need to |
| 214 | * be called. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 215 | */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 216 | static int need_forkexit_callback __read_mostly; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 217 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 218 | static void cgroup_offline_fn(struct work_struct *work); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 219 | static int cgroup_destroy_locked(struct cgroup *cgrp); |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 220 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
| 221 | struct cftype cfts[], bool is_add); |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 222 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 223 | /* convenient tests for these bits */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 224 | static inline bool cgroup_is_dead(const struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 225 | { |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 226 | return test_bit(CGRP_DEAD, &cgrp->flags); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Li Zefan | 78574cf | 2013-04-08 19:00:38 -0700 | [diff] [blame] | 229 | /** |
| 230 | * cgroup_is_descendant - test ancestry |
| 231 | * @cgrp: the cgroup to be tested |
| 232 | * @ancestor: possible ancestor of @cgrp |
| 233 | * |
| 234 | * Test whether @cgrp is a descendant of @ancestor. It also returns %true |
| 235 | * if @cgrp == @ancestor. This function is safe to call as long as @cgrp |
| 236 | * and @ancestor are accessible. |
| 237 | */ |
| 238 | bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor) |
| 239 | { |
| 240 | while (cgrp) { |
| 241 | if (cgrp == ancestor) |
| 242 | return true; |
| 243 | cgrp = cgrp->parent; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(cgroup_is_descendant); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 248 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 249 | static int cgroup_is_releasable(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 250 | { |
| 251 | const int bits = |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 252 | (1 << CGRP_RELEASABLE) | |
| 253 | (1 << CGRP_NOTIFY_ON_RELEASE); |
| 254 | return (cgrp->flags & bits) == bits; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Bunk | e9685a0 | 2008-02-07 00:13:46 -0800 | [diff] [blame] | 257 | static int notify_on_release(const struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 258 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 259 | return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 262 | /* |
| 263 | * for_each_subsys() allows you to iterate on each subsystem attached to |
| 264 | * an active hierarchy |
| 265 | */ |
| 266 | #define for_each_subsys(_root, _ss) \ |
| 267 | list_for_each_entry(_ss, &_root->subsys_list, sibling) |
| 268 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 269 | /* for_each_active_root() allows you to iterate across the active hierarchies */ |
| 270 | #define for_each_active_root(_root) \ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 271 | list_for_each_entry(_root, &cgroup_roots, root_list) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 272 | |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 273 | static inline struct cgroup *__d_cgrp(struct dentry *dentry) |
| 274 | { |
| 275 | return dentry->d_fsdata; |
| 276 | } |
| 277 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 278 | static inline struct cfent *__d_cfe(struct dentry *dentry) |
Tejun Heo | f6ea937 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 279 | { |
| 280 | return dentry->d_fsdata; |
| 281 | } |
| 282 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 283 | static inline struct cftype *__d_cft(struct dentry *dentry) |
| 284 | { |
| 285 | return __d_cfe(dentry)->type; |
| 286 | } |
| 287 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 288 | /** |
| 289 | * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive. |
| 290 | * @cgrp: the cgroup to be checked for liveness |
| 291 | * |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 292 | * On success, returns true; the mutex should be later unlocked. On |
| 293 | * failure returns false with no lock held. |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 294 | */ |
Tejun Heo | b9777cf | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 295 | static bool cgroup_lock_live_group(struct cgroup *cgrp) |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 296 | { |
| 297 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 298 | if (cgroup_is_dead(cgrp)) { |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 299 | mutex_unlock(&cgroup_mutex); |
| 300 | return false; |
| 301 | } |
| 302 | return true; |
| 303 | } |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 304 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 305 | /* the list of cgroups eligible for automatic release. Protected by |
| 306 | * release_list_lock */ |
| 307 | static LIST_HEAD(release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 308 | static DEFINE_RAW_SPINLOCK(release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 309 | static void cgroup_release_agent(struct work_struct *work); |
| 310 | static DECLARE_WORK(release_agent_work, cgroup_release_agent); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 311 | static void check_for_release(struct cgroup *cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 312 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 313 | /* |
| 314 | * A cgroup can be associated with multiple css_sets as different tasks may |
| 315 | * belong to different cgroups on different hierarchies. In the other |
| 316 | * direction, a css_set is naturally associated with multiple cgroups. |
| 317 | * This M:N relationship is represented by the following link structure |
| 318 | * which exists for each association and allows traversing the associations |
| 319 | * from both sides. |
| 320 | */ |
| 321 | struct cgrp_cset_link { |
| 322 | /* the cgroup and css_set this link associates */ |
| 323 | struct cgroup *cgrp; |
| 324 | struct css_set *cset; |
| 325 | |
| 326 | /* list of cgrp_cset_links anchored at cgrp->cset_links */ |
| 327 | struct list_head cset_link; |
| 328 | |
| 329 | /* list of cgrp_cset_links anchored at css_set->cgrp_links */ |
| 330 | struct list_head cgrp_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | /* The default css_set - used by init and its children prior to any |
| 334 | * hierarchies being mounted. It contains a pointer to the root state |
| 335 | * for each subsystem. Also used to anchor the list of css_sets. Not |
| 336 | * reference-counted, to improve performance when child cgroups |
| 337 | * haven't been created. |
| 338 | */ |
| 339 | |
| 340 | static struct css_set init_css_set; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 341 | static struct cgrp_cset_link init_cgrp_cset_link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 342 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 343 | static int cgroup_init_idr(struct cgroup_subsys *ss, |
| 344 | struct cgroup_subsys_state *css); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 345 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 346 | /* css_set_lock protects the list of css_set objects, and the |
| 347 | * chain of tasks off each css_set. Nests outside task->alloc_lock |
| 348 | * due to cgroup_iter_start() */ |
| 349 | static DEFINE_RWLOCK(css_set_lock); |
| 350 | static int css_set_count; |
| 351 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 352 | /* |
| 353 | * hash table for cgroup groups. This improves the performance to find |
| 354 | * an existing css_set. This hash doesn't (currently) take into |
| 355 | * account cgroups in empty hierarchies. |
| 356 | */ |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 357 | #define CSS_SET_HASH_BITS 7 |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 358 | static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 359 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 360 | static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 361 | { |
| 362 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 363 | unsigned long key = 0UL; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 364 | |
| 365 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 366 | key += (unsigned long)css[i]; |
| 367 | key = (key >> 16) ^ key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 368 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 369 | return key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 372 | /* We don't maintain the lists running through each css_set to its |
| 373 | * task until after the first call to cgroup_iter_start(). This |
| 374 | * reduces the fork()/exit() overhead for people who have cgroups |
| 375 | * compiled into their kernel but not actually in use */ |
Li Zefan | 8947f9d | 2008-07-25 01:46:56 -0700 | [diff] [blame] | 376 | static int use_task_css_set_links __read_mostly; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 377 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 378 | static void __put_css_set(struct css_set *cset, int taskexit) |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 379 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 380 | struct cgrp_cset_link *link, *tmp_link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 381 | |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 382 | /* |
| 383 | * Ensure that the refcount doesn't hit zero while any readers |
| 384 | * can see it. Similar to atomic_dec_and_lock(), but for an |
| 385 | * rwlock |
| 386 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 387 | if (atomic_add_unless(&cset->refcount, -1, 1)) |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 388 | return; |
| 389 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 390 | if (!atomic_dec_and_test(&cset->refcount)) { |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 391 | write_unlock(&css_set_lock); |
| 392 | return; |
| 393 | } |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 394 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 395 | /* This css_set is dead. unlink it and release cgroup refcounts */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 396 | hash_del(&cset->hlist); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 397 | css_set_count--; |
| 398 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 399 | list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 400 | struct cgroup *cgrp = link->cgrp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 401 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 402 | list_del(&link->cset_link); |
| 403 | list_del(&link->cgrp_link); |
Li Zefan | 71b5707 | 2013-01-24 14:43:28 +0800 | [diff] [blame] | 404 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 405 | /* @cgrp can't go away while we're holding css_set_lock */ |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 406 | if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 407 | if (taskexit) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 408 | set_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 409 | check_for_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 410 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 411 | |
| 412 | kfree(link); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 413 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 414 | |
| 415 | write_unlock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 416 | kfree_rcu(cset, rcu_head); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * refcounted get/put for css_set objects |
| 421 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 422 | static inline void get_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 423 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 424 | atomic_inc(&cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 427 | static inline void put_css_set(struct css_set *cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 428 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 429 | __put_css_set(cset, 0); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 432 | static inline void put_css_set_taskexit(struct css_set *cset) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 433 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 434 | __put_css_set(cset, 1); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 437 | /** |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 438 | * compare_css_sets - helper function for find_existing_css_set(). |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 439 | * @cset: candidate css_set being tested |
| 440 | * @old_cset: existing css_set for a task |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 441 | * @new_cgrp: cgroup that's being entered by the task |
| 442 | * @template: desired set of css pointers in css_set (pre-calculated) |
| 443 | * |
| 444 | * Returns true if "cg" matches "old_cg" except for the hierarchy |
| 445 | * which "new_cgrp" belongs to, for which it should match "new_cgrp". |
| 446 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 447 | static bool compare_css_sets(struct css_set *cset, |
| 448 | struct css_set *old_cset, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 449 | struct cgroup *new_cgrp, |
| 450 | struct cgroup_subsys_state *template[]) |
| 451 | { |
| 452 | struct list_head *l1, *l2; |
| 453 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 454 | if (memcmp(template, cset->subsys, sizeof(cset->subsys))) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 455 | /* Not all subsystems matched */ |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Compare cgroup pointers in order to distinguish between |
| 461 | * different cgroups in heirarchies with no subsystems. We |
| 462 | * could get by with just this check alone (and skip the |
| 463 | * memcmp above) but on most setups the memcmp check will |
| 464 | * avoid the need for this more expensive check on almost all |
| 465 | * candidates. |
| 466 | */ |
| 467 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 468 | l1 = &cset->cgrp_links; |
| 469 | l2 = &old_cset->cgrp_links; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 470 | while (1) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 471 | struct cgrp_cset_link *link1, *link2; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 472 | struct cgroup *cgrp1, *cgrp2; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 473 | |
| 474 | l1 = l1->next; |
| 475 | l2 = l2->next; |
| 476 | /* See if we reached the end - both lists are equal length. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 477 | if (l1 == &cset->cgrp_links) { |
| 478 | BUG_ON(l2 != &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 479 | break; |
| 480 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 481 | BUG_ON(l2 == &old_cset->cgrp_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 482 | } |
| 483 | /* Locate the cgroups associated with these links. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 484 | link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); |
| 485 | link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); |
| 486 | cgrp1 = link1->cgrp; |
| 487 | cgrp2 = link2->cgrp; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 488 | /* Hierarchies should be linked in the same order. */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 489 | BUG_ON(cgrp1->root != cgrp2->root); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * If this hierarchy is the hierarchy of the cgroup |
| 493 | * that's changing, then we need to check that this |
| 494 | * css_set points to the new cgroup; if it's any other |
| 495 | * hierarchy, then this css_set should point to the |
| 496 | * same cgroup as the old css_set. |
| 497 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 498 | if (cgrp1->root == new_cgrp->root) { |
| 499 | if (cgrp1 != new_cgrp) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 500 | return false; |
| 501 | } else { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 502 | if (cgrp1 != cgrp2) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 509 | /** |
| 510 | * find_existing_css_set - init css array and find the matching css_set |
| 511 | * @old_cset: the css_set that we're using before the cgroup transition |
| 512 | * @cgrp: the cgroup that we're moving into |
| 513 | * @template: out param for the new set of csses, should be clear on entry |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 514 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 515 | static struct css_set *find_existing_css_set(struct css_set *old_cset, |
| 516 | struct cgroup *cgrp, |
| 517 | struct cgroup_subsys_state *template[]) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 518 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 519 | struct cgroupfs_root *root = cgrp->root; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 520 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 521 | unsigned long key; |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 522 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 523 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 524 | /* |
| 525 | * Build the set of subsystem state objects that we want to see in the |
| 526 | * new css_set. while subsystems can change globally, the entries here |
| 527 | * won't change, so no need for locking. |
| 528 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 529 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 530 | if (root->subsys_mask & (1UL << i)) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 531 | /* Subsystem is in this hierarchy. So we want |
| 532 | * the subsystem state from the new |
| 533 | * cgroup */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 534 | template[i] = cgrp->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 535 | } else { |
| 536 | /* Subsystem is not in this hierarchy, so we |
| 537 | * don't want to change the subsystem state */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 538 | template[i] = old_cset->subsys[i]; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 542 | key = css_set_hash(template); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 543 | hash_for_each_possible(css_set_table, cset, hlist, key) { |
| 544 | if (!compare_css_sets(cset, old_cset, cgrp, template)) |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 545 | continue; |
| 546 | |
| 547 | /* This css_set matches what we need */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 548 | return cset; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 549 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* No existing cgroup group matched */ |
| 552 | return NULL; |
| 553 | } |
| 554 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 555 | static void free_cgrp_cset_links(struct list_head *links_to_free) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 556 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 557 | struct cgrp_cset_link *link, *tmp_link; |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 558 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 559 | list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { |
| 560 | list_del(&link->cset_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 561 | kfree(link); |
| 562 | } |
| 563 | } |
| 564 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 565 | /** |
| 566 | * allocate_cgrp_cset_links - allocate cgrp_cset_links |
| 567 | * @count: the number of links to allocate |
| 568 | * @tmp_links: list_head the allocated links are put on |
| 569 | * |
| 570 | * Allocate @count cgrp_cset_link structures and chain them on @tmp_links |
| 571 | * through ->cset_link. Returns 0 on success or -errno. |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 572 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 573 | 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] | 574 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 575 | struct cgrp_cset_link *link; |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 576 | int i; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 577 | |
| 578 | INIT_LIST_HEAD(tmp_links); |
| 579 | |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 580 | for (i = 0; i < count; i++) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 581 | link = kzalloc(sizeof(*link), GFP_KERNEL); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 582 | if (!link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 583 | free_cgrp_cset_links(tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 584 | return -ENOMEM; |
| 585 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 586 | list_add(&link->cset_link, tmp_links); |
Li Zefan | 3655343 | 2008-07-29 22:33:19 -0700 | [diff] [blame] | 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 591 | /** |
| 592 | * 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] | 593 | * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 594 | * @cset: the css_set to be linked |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 595 | * @cgrp: the destination cgroup |
| 596 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 597 | static void link_css_set(struct list_head *tmp_links, struct css_set *cset, |
| 598 | struct cgroup *cgrp) |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 599 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 600 | struct cgrp_cset_link *link; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 601 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 602 | BUG_ON(list_empty(tmp_links)); |
| 603 | link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); |
| 604 | link->cset = cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 605 | link->cgrp = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 606 | list_move(&link->cset_link, &cgrp->cset_links); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 607 | /* |
| 608 | * Always add links to the tail of the list so that the list |
| 609 | * is sorted by order of hierarchy creation |
| 610 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 611 | list_add_tail(&link->cgrp_link, &cset->cgrp_links); |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 614 | /** |
| 615 | * find_css_set - return a new css_set with one cgroup updated |
| 616 | * @old_cset: the baseline css_set |
| 617 | * @cgrp: the cgroup to be updated |
| 618 | * |
| 619 | * Return a new css_set that's equivalent to @old_cset, but with @cgrp |
| 620 | * substituted into the appropriate hierarchy. |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 621 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 622 | static struct css_set *find_css_set(struct css_set *old_cset, |
| 623 | struct cgroup *cgrp) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 624 | { |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 625 | struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 626 | struct css_set *cset; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 627 | struct list_head tmp_links; |
| 628 | struct cgrp_cset_link *link; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 629 | unsigned long key; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 630 | |
Tejun Heo | b326f9d | 2013-06-24 15:21:48 -0700 | [diff] [blame^] | 631 | lockdep_assert_held(&cgroup_mutex); |
| 632 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 633 | /* First see if we already have a cgroup group that matches |
| 634 | * the desired set */ |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 635 | read_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 636 | cset = find_existing_css_set(old_cset, cgrp, template); |
| 637 | if (cset) |
| 638 | get_css_set(cset); |
Li Zefan | 7e9abd8 | 2008-07-25 01:46:54 -0700 | [diff] [blame] | 639 | read_unlock(&css_set_lock); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 640 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 641 | if (cset) |
| 642 | return cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 643 | |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 644 | cset = kzalloc(sizeof(*cset), GFP_KERNEL); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 645 | if (!cset) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 646 | return NULL; |
| 647 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 648 | /* Allocate all the cgrp_cset_link objects that we'll need */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 649 | if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 650 | kfree(cset); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 654 | atomic_set(&cset->refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 655 | INIT_LIST_HEAD(&cset->cgrp_links); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 656 | INIT_LIST_HEAD(&cset->tasks); |
| 657 | INIT_HLIST_NODE(&cset->hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 658 | |
| 659 | /* Copy the set of subsystem state objects generated in |
| 660 | * find_existing_css_set() */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 661 | memcpy(cset->subsys, template, sizeof(cset->subsys)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 662 | |
| 663 | write_lock(&css_set_lock); |
| 664 | /* Add reference counts and links from the new css_set. */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 665 | list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 666 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 667 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 668 | if (c->root == cgrp->root) |
| 669 | c = cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 670 | link_css_set(&tmp_links, cset, c); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 671 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 672 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 673 | BUG_ON(!list_empty(&tmp_links)); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 674 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 675 | css_set_count++; |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 676 | |
| 677 | /* Add this cgroup group to the hash table */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 678 | key = css_set_hash(cset->subsys); |
| 679 | hash_add(css_set_table, &cset->hlist, key); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 680 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 681 | write_unlock(&css_set_lock); |
| 682 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 683 | return cset; |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 686 | /* |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 687 | * Return the cgroup for "task" from the given hierarchy. Must be |
| 688 | * called with cgroup_mutex held. |
| 689 | */ |
| 690 | static struct cgroup *task_cgroup_from_root(struct task_struct *task, |
| 691 | struct cgroupfs_root *root) |
| 692 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 693 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 694 | struct cgroup *res = NULL; |
| 695 | |
| 696 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 697 | read_lock(&css_set_lock); |
| 698 | /* |
| 699 | * No need to lock the task - since we hold cgroup_mutex the |
| 700 | * task can't change groups, so the only thing that can happen |
| 701 | * is that it exits and its css is set back to init_css_set. |
| 702 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 703 | cset = task->cgroups; |
| 704 | if (cset == &init_css_set) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 705 | res = &root->top_cgroup; |
| 706 | } else { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 707 | struct cgrp_cset_link *link; |
| 708 | |
| 709 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 710 | struct cgroup *c = link->cgrp; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 711 | |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 712 | if (c->root == root) { |
| 713 | res = c; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | read_unlock(&css_set_lock); |
| 719 | BUG_ON(!res); |
| 720 | return res; |
| 721 | } |
| 722 | |
| 723 | /* |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 724 | * There is one global cgroup mutex. We also require taking |
| 725 | * task_lock() when dereferencing a task's cgroup subsys pointers. |
| 726 | * See "The task_lock() exception", at the end of this comment. |
| 727 | * |
| 728 | * A task must hold cgroup_mutex to modify cgroups. |
| 729 | * |
| 730 | * Any task can increment and decrement the count field without lock. |
| 731 | * So in general, code holding cgroup_mutex can't rely on the count |
| 732 | * field not changing. However, if the count goes to zero, then only |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 733 | * cgroup_attach_task() can increment it again. Because a count of zero |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 734 | * means that no tasks are currently attached, therefore there is no |
| 735 | * way a task attached to that cgroup can fork (the other way to |
| 736 | * increment the count). So code holding cgroup_mutex can safely |
| 737 | * assume that if the count is zero, it will stay zero. Similarly, if |
| 738 | * a task holds cgroup_mutex on a cgroup with zero count, it |
| 739 | * knows that the cgroup won't be removed, as cgroup_rmdir() |
| 740 | * needs that mutex. |
| 741 | * |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 742 | * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't |
| 743 | * (usually) take cgroup_mutex. These are the two most performance |
| 744 | * critical pieces of code here. The exception occurs on cgroup_exit(), |
| 745 | * when a task in a notify_on_release cgroup exits. Then cgroup_mutex |
| 746 | * 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] | 747 | * to the release agent with the name of the cgroup (path relative to |
| 748 | * the root of cgroup file system) as the argument. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 749 | * |
| 750 | * A cgroup can only be deleted if both its 'count' of using tasks |
| 751 | * is zero, and its list of 'children' cgroups is empty. Since all |
| 752 | * tasks in the system use _some_ cgroup, and since there is always at |
| 753 | * least one task in the system (init, pid == 1), therefore, top_cgroup |
| 754 | * always has either children cgroups and/or using tasks. So we don't |
| 755 | * need a special hack to ensure that top_cgroup cannot be deleted. |
| 756 | * |
| 757 | * The task_lock() exception |
| 758 | * |
| 759 | * The need for this exception arises from the action of |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 760 | * cgroup_attach_task(), which overwrites one task's cgroup pointer with |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 761 | * another. It does so using cgroup_mutex, however there are |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 762 | * several performance critical places that need to reference |
| 763 | * task->cgroup without the expense of grabbing a system global |
| 764 | * mutex. Therefore except as noted below, when dereferencing or, as |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 765 | * in cgroup_attach_task(), modifying a task's cgroup pointer we use |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 766 | * task_lock(), which acts on a spinlock (task->alloc_lock) already in |
| 767 | * the task_struct routinely used for such matters. |
| 768 | * |
| 769 | * P.S. One more locking exception. RCU is used to guard the |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 770 | * update of a tasks cgroup pointer by cgroup_attach_task() |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 771 | */ |
| 772 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 773 | /* |
| 774 | * A couple of forward declarations required, due to cyclic reference loop: |
| 775 | * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir -> |
| 776 | * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations |
| 777 | * -> cgroup_mkdir. |
| 778 | */ |
| 779 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 780 | 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] | 781 | static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 782 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 783 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 784 | unsigned long subsys_mask); |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 785 | static const struct inode_operations cgroup_dir_inode_operations; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 786 | static const struct file_operations proc_cgroupstats_operations; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 787 | |
| 788 | static struct backing_dev_info cgroup_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 789 | .name = "cgroup", |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 790 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 791 | }; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 792 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 793 | static int alloc_css_id(struct cgroup_subsys *ss, |
| 794 | struct cgroup *parent, struct cgroup *child); |
| 795 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 796 | 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] | 797 | { |
| 798 | struct inode *inode = new_inode(sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 799 | |
| 800 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 801 | inode->i_ino = get_next_ino(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 802 | inode->i_mode = mode; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 803 | inode->i_uid = current_fsuid(); |
| 804 | inode->i_gid = current_fsgid(); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 805 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 806 | inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info; |
| 807 | } |
| 808 | return inode; |
| 809 | } |
| 810 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 811 | static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry) |
| 812 | { |
| 813 | struct cgroup_name *name; |
| 814 | |
| 815 | name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL); |
| 816 | if (!name) |
| 817 | return NULL; |
| 818 | strcpy(name->name, dentry->d_name.name); |
| 819 | return name; |
| 820 | } |
| 821 | |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 822 | static void cgroup_free_fn(struct work_struct *work) |
| 823 | { |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 824 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 825 | struct cgroup_subsys *ss; |
| 826 | |
| 827 | mutex_lock(&cgroup_mutex); |
| 828 | /* |
| 829 | * Release the subsystem state objects. |
| 830 | */ |
| 831 | for_each_subsys(cgrp->root, ss) |
| 832 | ss->css_free(cgrp); |
| 833 | |
| 834 | cgrp->root->number_of_cgroups--; |
| 835 | mutex_unlock(&cgroup_mutex); |
| 836 | |
| 837 | /* |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 838 | * We get a ref to the parent's dentry, and put the ref when |
| 839 | * this cgroup is being freed, so it's guaranteed that the |
| 840 | * parent won't be destroyed before its children. |
| 841 | */ |
| 842 | dput(cgrp->parent->dentry); |
| 843 | |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 844 | ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id); |
| 845 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 846 | /* |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 847 | * Drop the active superblock reference that we took when we |
Li Zefan | cc20e01 | 2013-04-26 11:58:02 -0700 | [diff] [blame] | 848 | * created the cgroup. This will free cgrp->root, if we are |
| 849 | * holding the last reference to @sb. |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 850 | */ |
| 851 | deactivate_super(cgrp->root->sb); |
| 852 | |
| 853 | /* |
| 854 | * if we're getting rid of the cgroup, refcount should ensure |
| 855 | * that there are no pidlists left. |
| 856 | */ |
| 857 | BUG_ON(!list_empty(&cgrp->pidlists)); |
| 858 | |
| 859 | simple_xattrs_free(&cgrp->xattrs); |
| 860 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 861 | kfree(rcu_dereference_raw(cgrp->name)); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 862 | kfree(cgrp); |
| 863 | } |
| 864 | |
| 865 | static void cgroup_free_rcu(struct rcu_head *head) |
| 866 | { |
| 867 | struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head); |
| 868 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 869 | INIT_WORK(&cgrp->destroy_work, cgroup_free_fn); |
| 870 | schedule_work(&cgrp->destroy_work); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 871 | } |
| 872 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 873 | static void cgroup_diput(struct dentry *dentry, struct inode *inode) |
| 874 | { |
| 875 | /* is dentry a directory ? if so, kfree() associated cgroup */ |
| 876 | if (S_ISDIR(inode->i_mode)) { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 877 | struct cgroup *cgrp = dentry->d_fsdata; |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 878 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 879 | BUG_ON(!(cgroup_is_dead(cgrp))); |
Li Zefan | be44562 | 2013-01-24 14:31:42 +0800 | [diff] [blame] | 880 | call_rcu(&cgrp->rcu_head, cgroup_free_rcu); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 881 | } else { |
| 882 | struct cfent *cfe = __d_cfe(dentry); |
| 883 | struct cgroup *cgrp = dentry->d_parent->d_fsdata; |
| 884 | |
| 885 | WARN_ONCE(!list_empty(&cfe->node) && |
| 886 | cgrp != &cgrp->root->top_cgroup, |
| 887 | "cfe still linked for %s\n", cfe->type->name); |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 888 | simple_xattrs_free(&cfe->xattrs); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 889 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 890 | } |
| 891 | iput(inode); |
| 892 | } |
| 893 | |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 894 | static int cgroup_delete(const struct dentry *d) |
| 895 | { |
| 896 | return 1; |
| 897 | } |
| 898 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 899 | static void remove_dir(struct dentry *d) |
| 900 | { |
| 901 | struct dentry *parent = dget(d->d_parent); |
| 902 | |
| 903 | d_delete(d); |
| 904 | simple_rmdir(parent->d_inode, d); |
| 905 | dput(parent); |
| 906 | } |
| 907 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 908 | static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 909 | { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 910 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 911 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 912 | lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex); |
| 913 | lockdep_assert_held(&cgroup_mutex); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 914 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 915 | /* |
| 916 | * If we're doing cleanup due to failure of cgroup_create(), |
| 917 | * the corresponding @cfe may not exist. |
| 918 | */ |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 919 | list_for_each_entry(cfe, &cgrp->files, node) { |
| 920 | struct dentry *d = cfe->dentry; |
| 921 | |
| 922 | if (cft && cfe->type != cft) |
| 923 | continue; |
| 924 | |
| 925 | dget(d); |
| 926 | d_delete(d); |
Tejun Heo | ce27e31 | 2012-07-03 10:38:06 -0700 | [diff] [blame] | 927 | simple_unlink(cgrp->dentry->d_inode, d); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 928 | list_del_init(&cfe->node); |
| 929 | dput(d); |
| 930 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 931 | break; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 932 | } |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 935 | /** |
| 936 | * cgroup_clear_directory - selective removal of base and subsystem files |
| 937 | * @dir: directory containing the files |
| 938 | * @base_files: true if the base files should be removed |
| 939 | * @subsys_mask: mask of the subsystem ids whose files should be removed |
| 940 | */ |
| 941 | static void cgroup_clear_directory(struct dentry *dir, bool base_files, |
| 942 | unsigned long subsys_mask) |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 943 | { |
| 944 | struct cgroup *cgrp = __d_cgrp(dir); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 945 | struct cgroup_subsys *ss; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 946 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 947 | for_each_subsys(cgrp->root, ss) { |
| 948 | struct cftype_set *set; |
| 949 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 950 | continue; |
| 951 | list_for_each_entry(set, &ss->cftsets, node) |
Gao feng | 879a3d9 | 2012-12-01 00:21:28 +0800 | [diff] [blame] | 952 | cgroup_addrm_files(cgrp, NULL, set->cfts, false); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 953 | } |
| 954 | if (base_files) { |
| 955 | while (!list_empty(&cgrp->files)) |
| 956 | cgroup_rm_file(cgrp, NULL); |
| 957 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | * NOTE : the dentry must have been dget()'ed |
| 962 | */ |
| 963 | static void cgroup_d_remove_dir(struct dentry *dentry) |
| 964 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 965 | struct dentry *parent; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 966 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 967 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 968 | cgroup_clear_directory(dentry, true, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 969 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 970 | parent = dentry->d_parent; |
| 971 | spin_lock(&parent->d_lock); |
Li Zefan | 3ec762a | 2011-01-14 11:34:34 +0800 | [diff] [blame] | 972 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 973 | list_del_init(&dentry->d_u.d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 974 | spin_unlock(&dentry->d_lock); |
| 975 | spin_unlock(&parent->d_lock); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 976 | remove_dir(dentry); |
| 977 | } |
| 978 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 979 | /* |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 980 | * Call with cgroup_mutex held. Drops reference counts on modules, including |
| 981 | * any duplicate ones that parse_cgroupfs_options took. If this function |
| 982 | * returns an error, no reference counts are touched. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 983 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 984 | static int rebind_subsystems(struct cgroupfs_root *root, |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 985 | unsigned long added_mask, unsigned removed_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 986 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 987 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 988 | int i; |
| 989 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 990 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 991 | BUG_ON(!mutex_is_locked(&cgroup_root_mutex)); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 992 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 993 | /* Check that any added subsystems are currently free */ |
| 994 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Li Zefan | 8d53d55 | 2008-02-23 15:24:11 -0800 | [diff] [blame] | 995 | unsigned long bit = 1UL << i; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 996 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 997 | if (!(bit & added_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 998 | continue; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 999 | /* |
| 1000 | * Nobody should tell us to do a subsys that doesn't exist: |
| 1001 | * parse_cgroupfs_options should catch that case and refcounts |
| 1002 | * ensure that subsystems won't disappear once selected. |
| 1003 | */ |
| 1004 | BUG_ON(ss == NULL); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1005 | if (ss->root != &cgroup_dummy_root) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1006 | /* Subsystem isn't free */ |
| 1007 | return -EBUSY; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* Currently we don't handle adding/removing subsystems when |
| 1012 | * any child cgroups exist. This is theoretically supportable |
| 1013 | * but involves complex error handling, so it's being left until |
| 1014 | * later */ |
Paul Menage | 307257c | 2008-12-15 13:54:22 -0800 | [diff] [blame] | 1015 | if (root->number_of_cgroups > 1) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1016 | return -EBUSY; |
| 1017 | |
| 1018 | /* Process each subsystem */ |
| 1019 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1020 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1021 | unsigned long bit = 1UL << i; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1022 | if (bit & added_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1023 | /* We're binding this subsystem to this hierarchy */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1024 | BUG_ON(ss == NULL); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1025 | BUG_ON(cgrp->subsys[i]); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1026 | BUG_ON(!cgroup_dummy_top->subsys[i]); |
| 1027 | BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1028 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1029 | cgrp->subsys[i] = cgroup_dummy_top->subsys[i]; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1030 | cgrp->subsys[i]->cgroup = cgrp; |
Li Zefan | 33a68ac | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1031 | list_move(&ss->sibling, &root->subsys_list); |
Lai Jiangshan | b2aa30f | 2009-01-07 18:07:37 -0800 | [diff] [blame] | 1032 | ss->root = root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1033 | if (ss->bind) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 1034 | ss->bind(cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1035 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1036 | /* refcount was already taken, and we're keeping it */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1037 | root->subsys_mask |= bit; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1038 | } else if (bit & removed_mask) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1039 | /* We're removing this subsystem */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1040 | BUG_ON(ss == NULL); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1041 | BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1042 | BUG_ON(cgrp->subsys[i]->cgroup != cgrp); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1043 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1044 | if (ss->bind) |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1045 | ss->bind(cgroup_dummy_top); |
| 1046 | cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1047 | cgrp->subsys[i] = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1048 | cgroup_subsys[i]->root = &cgroup_dummy_root; |
| 1049 | list_move(&ss->sibling, &cgroup_dummy_root.subsys_list); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1050 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1051 | /* subsystem is now free - drop reference on module */ |
| 1052 | module_put(ss->module); |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1053 | root->subsys_mask &= ~bit; |
| 1054 | } else if (bit & root->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 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1075 | static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1076 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1077 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1078 | struct cgroup_subsys *ss; |
| 1079 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1080 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1081 | for_each_subsys(root, ss) |
| 1082 | seq_printf(seq, ",%s", ss->name); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1083 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) |
| 1084 | seq_puts(seq, ",sane_behavior"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1085 | if (root->flags & CGRP_ROOT_NOPREFIX) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1086 | seq_puts(seq, ",noprefix"); |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1087 | if (root->flags & CGRP_ROOT_XATTR) |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1088 | seq_puts(seq, ",xattr"); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1089 | if (strlen(root->release_agent_path)) |
| 1090 | seq_printf(seq, ",release_agent=%s", root->release_agent_path); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1091 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags)) |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 1092 | seq_puts(seq, ",clone_children"); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1093 | if (strlen(root->name)) |
| 1094 | seq_printf(seq, ",name=%s", root->name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1095 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | struct cgroup_sb_opts { |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1100 | unsigned long subsys_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1101 | unsigned long flags; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1102 | char *release_agent; |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1103 | bool cpuset_clone_children; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1104 | char *name; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1105 | /* User explicitly requested empty subsystem */ |
| 1106 | bool none; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1107 | |
| 1108 | struct cgroupfs_root *new_root; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1109 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1110 | }; |
| 1111 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1112 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1113 | * Convert a hierarchy specifier into a bitmask of subsystems and |
| 1114 | * flags. Call with cgroup_mutex held to protect the cgroup_subsys[] |
| 1115 | * array. This function takes refcounts on subsystems to be used, unless it |
| 1116 | * returns error, in which case no refcounts are taken. |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1117 | */ |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1118 | static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1119 | { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1120 | char *token, *o = data; |
| 1121 | bool all_ss = false, one_ss = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1122 | unsigned long mask = (unsigned long)-1; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1123 | int i; |
| 1124 | bool module_pin_failed = false; |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1125 | |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1126 | BUG_ON(!mutex_is_locked(&cgroup_mutex)); |
| 1127 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1128 | #ifdef CONFIG_CPUSETS |
| 1129 | mask = ~(1UL << cpuset_subsys_id); |
| 1130 | #endif |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1131 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1132 | memset(opts, 0, sizeof(*opts)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1133 | |
| 1134 | while ((token = strsep(&o, ",")) != NULL) { |
| 1135 | if (!*token) |
| 1136 | return -EINVAL; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1137 | if (!strcmp(token, "none")) { |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1138 | /* Explicitly have no subsystems */ |
| 1139 | opts->none = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1140 | continue; |
| 1141 | } |
| 1142 | if (!strcmp(token, "all")) { |
| 1143 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1144 | if (one_ss) |
| 1145 | return -EINVAL; |
| 1146 | all_ss = true; |
| 1147 | continue; |
| 1148 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1149 | if (!strcmp(token, "__DEVEL__sane_behavior")) { |
| 1150 | opts->flags |= CGRP_ROOT_SANE_BEHAVIOR; |
| 1151 | continue; |
| 1152 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1153 | if (!strcmp(token, "noprefix")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1154 | opts->flags |= CGRP_ROOT_NOPREFIX; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1155 | continue; |
| 1156 | } |
| 1157 | if (!strcmp(token, "clone_children")) { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1158 | opts->cpuset_clone_children = true; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1159 | continue; |
| 1160 | } |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1161 | if (!strcmp(token, "xattr")) { |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1162 | opts->flags |= CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1165 | if (!strncmp(token, "release_agent=", 14)) { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1166 | /* Specifying two release agents is forbidden */ |
| 1167 | if (opts->release_agent) |
| 1168 | return -EINVAL; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1169 | opts->release_agent = |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1170 | kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1171 | if (!opts->release_agent) |
| 1172 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1173 | continue; |
| 1174 | } |
| 1175 | if (!strncmp(token, "name=", 5)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1176 | const char *name = token + 5; |
| 1177 | /* Can't specify an empty name */ |
| 1178 | if (!strlen(name)) |
| 1179 | return -EINVAL; |
| 1180 | /* Must match [\w.-]+ */ |
| 1181 | for (i = 0; i < strlen(name); i++) { |
| 1182 | char c = name[i]; |
| 1183 | if (isalnum(c)) |
| 1184 | continue; |
| 1185 | if ((c == '.') || (c == '-') || (c == '_')) |
| 1186 | continue; |
| 1187 | return -EINVAL; |
| 1188 | } |
| 1189 | /* Specifying two names is forbidden */ |
| 1190 | if (opts->name) |
| 1191 | return -EINVAL; |
| 1192 | opts->name = kstrndup(name, |
Dan Carpenter | e400c28 | 2010-08-10 18:02:54 -0700 | [diff] [blame] | 1193 | MAX_CGROUP_ROOT_NAMELEN - 1, |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1194 | GFP_KERNEL); |
| 1195 | if (!opts->name) |
| 1196 | return -ENOMEM; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1197 | |
| 1198 | continue; |
| 1199 | } |
| 1200 | |
| 1201 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1202 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1203 | if (ss == NULL) |
| 1204 | continue; |
| 1205 | if (strcmp(token, ss->name)) |
| 1206 | continue; |
| 1207 | if (ss->disabled) |
| 1208 | continue; |
| 1209 | |
| 1210 | /* Mutually exclusive option 'all' + subsystem name */ |
| 1211 | if (all_ss) |
| 1212 | return -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1213 | set_bit(i, &opts->subsys_mask); |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1214 | one_ss = true; |
| 1215 | |
| 1216 | break; |
| 1217 | } |
| 1218 | if (i == CGROUP_SUBSYS_COUNT) |
| 1219 | return -ENOENT; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * If the 'all' option was specified select all the subsystems, |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1224 | * otherwise if 'none', 'name=' and a subsystem name options |
| 1225 | * were not specified, let's default to 'all' |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1226 | */ |
Li Zefan | 0d19ea8 | 2011-12-27 14:25:55 +0800 | [diff] [blame] | 1227 | if (all_ss || (!one_ss && !opts->none && !opts->name)) { |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1228 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1229 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Daniel Lezcano | 32a8cf23 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 1230 | if (ss == NULL) |
| 1231 | continue; |
| 1232 | if (ss->disabled) |
| 1233 | continue; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1234 | set_bit(i, &opts->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1238 | /* Consistency checks */ |
| 1239 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1240 | if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1241 | pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n"); |
| 1242 | |
| 1243 | if (opts->flags & CGRP_ROOT_NOPREFIX) { |
| 1244 | pr_err("cgroup: sane_behavior: noprefix is not allowed\n"); |
| 1245 | return -EINVAL; |
| 1246 | } |
| 1247 | |
| 1248 | if (opts->cpuset_clone_children) { |
| 1249 | pr_err("cgroup: sane_behavior: clone_children is not allowed\n"); |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Option noprefix was introduced just for backward compatibility |
| 1256 | * with the old cpuset, so we allow noprefix only if mounting just |
| 1257 | * the cpuset subsystem. |
| 1258 | */ |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 1259 | if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask)) |
Li Zefan | f9ab5b5 | 2009-06-17 16:26:33 -0700 | [diff] [blame] | 1260 | return -EINVAL; |
| 1261 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1262 | |
| 1263 | /* Can't specify "none" and some subsystems */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1264 | if (opts->subsys_mask && opts->none) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1265 | return -EINVAL; |
| 1266 | |
| 1267 | /* |
| 1268 | * We either have to specify by name or by subsystems. (So all |
| 1269 | * empty hierarchies must have a name). |
| 1270 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1271 | if (!opts->subsys_mask && !opts->name) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1272 | return -EINVAL; |
| 1273 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1274 | /* |
| 1275 | * Grab references on all the modules we'll need, so the subsystems |
| 1276 | * don't dance around before rebind_subsystems attaches them. This may |
| 1277 | * take duplicate reference counts on a subsystem that's already used, |
| 1278 | * but rebind_subsystems handles this case. |
| 1279 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1280 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1281 | unsigned long bit = 1UL << i; |
| 1282 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1283 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1284 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1285 | if (!try_module_get(cgroup_subsys[i]->module)) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1286 | module_pin_failed = true; |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | if (module_pin_failed) { |
| 1291 | /* |
| 1292 | * oops, one of the modules was going away. this means that we |
| 1293 | * raced with a module_delete call, and to the user this is |
| 1294 | * essentially a "subsystem doesn't exist" case. |
| 1295 | */ |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1296 | for (i--; i >= 0; i--) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1297 | /* drop refcounts only on the ones we took */ |
| 1298 | unsigned long bit = 1UL << i; |
| 1299 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1300 | if (!(bit & opts->subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1301 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1302 | module_put(cgroup_subsys[i]->module); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1303 | } |
| 1304 | return -ENOENT; |
| 1305 | } |
| 1306 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1307 | return 0; |
| 1308 | } |
| 1309 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1310 | static void drop_parsed_module_refcounts(unsigned long subsys_mask) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1311 | { |
| 1312 | int i; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 1313 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1314 | unsigned long bit = 1UL << i; |
| 1315 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1316 | if (!(bit & subsys_mask)) |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1317 | continue; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1318 | module_put(cgroup_subsys[i]->module); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1322 | static int cgroup_remount(struct super_block *sb, int *flags, char *data) |
| 1323 | { |
| 1324 | int ret = 0; |
| 1325 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1326 | struct cgroup *cgrp = &root->top_cgroup; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1327 | struct cgroup_sb_opts opts; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1328 | unsigned long added_mask, removed_mask; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1329 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1330 | if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1331 | pr_err("cgroup: sane_behavior: remount is not allowed\n"); |
| 1332 | return -EINVAL; |
| 1333 | } |
| 1334 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1335 | mutex_lock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1336 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1337 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1338 | |
| 1339 | /* See what subsystems are wanted */ |
| 1340 | ret = parse_cgroupfs_options(data, &opts); |
| 1341 | if (ret) |
| 1342 | goto out_unlock; |
| 1343 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1344 | if (opts.subsys_mask != root->subsys_mask || opts.release_agent) |
Tejun Heo | 8b5a5a9 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1345 | pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n", |
| 1346 | task_tgid_nr(current), current->comm); |
| 1347 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1348 | added_mask = opts.subsys_mask & ~root->subsys_mask; |
| 1349 | removed_mask = root->subsys_mask & ~opts.subsys_mask; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1350 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1351 | /* Don't allow flags or name to change at remount */ |
| 1352 | if (opts.flags != root->flags || |
| 1353 | (opts.name && strcmp(opts.name, root->name))) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1354 | ret = -EINVAL; |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1355 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1356 | goto out_unlock; |
| 1357 | } |
| 1358 | |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1359 | /* |
| 1360 | * Clear out the files of subsystems that should be removed, do |
| 1361 | * this before rebind_subsystems, since rebind_subsystems may |
| 1362 | * change this hierarchy's subsys_list. |
| 1363 | */ |
| 1364 | cgroup_clear_directory(cgrp->dentry, false, removed_mask); |
| 1365 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1366 | ret = rebind_subsystems(root, added_mask, removed_mask); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1367 | if (ret) { |
Gao feng | 7083d03 | 2012-12-03 09:28:18 +0800 | [diff] [blame] | 1368 | /* rebind_subsystems failed, re-populate the removed files */ |
| 1369 | cgroup_populate_dir(cgrp, false, removed_mask); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1370 | drop_parsed_module_refcounts(opts.subsys_mask); |
Li Zefan | 0670e08 | 2009-04-02 16:57:30 -0700 | [diff] [blame] | 1371 | goto out_unlock; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1372 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1373 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 1374 | /* re-populate subsystem files */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1375 | cgroup_populate_dir(cgrp, false, added_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1376 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1377 | if (opts.release_agent) |
| 1378 | strcpy(root->release_agent_path, opts.release_agent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1379 | out_unlock: |
Jesper Juhl | 66bdc9c | 2009-04-02 16:57:27 -0700 | [diff] [blame] | 1380 | kfree(opts.release_agent); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1381 | kfree(opts.name); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1382 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1383 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1384 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1385 | return ret; |
| 1386 | } |
| 1387 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 1388 | static const struct super_operations cgroup_ops = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1389 | .statfs = simple_statfs, |
| 1390 | .drop_inode = generic_delete_inode, |
| 1391 | .show_options = cgroup_show_options, |
| 1392 | .remount_fs = cgroup_remount, |
| 1393 | }; |
| 1394 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1395 | static void init_cgroup_housekeeping(struct cgroup *cgrp) |
| 1396 | { |
| 1397 | INIT_LIST_HEAD(&cgrp->sibling); |
| 1398 | INIT_LIST_HEAD(&cgrp->children); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 1399 | INIT_LIST_HEAD(&cgrp->files); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1400 | INIT_LIST_HEAD(&cgrp->cset_links); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1401 | INIT_LIST_HEAD(&cgrp->release_list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 1402 | INIT_LIST_HEAD(&cgrp->pidlists); |
| 1403 | mutex_init(&cgrp->pidlist_mutex); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 1404 | INIT_LIST_HEAD(&cgrp->event_list); |
| 1405 | spin_lock_init(&cgrp->event_list_lock); |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1406 | simple_xattrs_init(&cgrp->xattrs); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1407 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1408 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1409 | static void init_cgroup_root(struct cgroupfs_root *root) |
| 1410 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1411 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 1412 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1413 | INIT_LIST_HEAD(&root->subsys_list); |
| 1414 | INIT_LIST_HEAD(&root->root_list); |
| 1415 | root->number_of_cgroups = 1; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1416 | cgrp->root = root; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1417 | cgrp->name = &root_cgroup_name; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 1418 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1421 | static int cgroup_init_root_id(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1422 | { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1423 | int id; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1424 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1425 | lockdep_assert_held(&cgroup_mutex); |
| 1426 | lockdep_assert_held(&cgroup_root_mutex); |
| 1427 | |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1428 | id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL); |
| 1429 | if (id < 0) |
| 1430 | return id; |
| 1431 | |
| 1432 | root->hierarchy_id = id; |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | static void cgroup_exit_root_id(struct cgroupfs_root *root) |
| 1437 | { |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1438 | lockdep_assert_held(&cgroup_mutex); |
| 1439 | lockdep_assert_held(&cgroup_root_mutex); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1440 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 1441 | if (root->hierarchy_id) { |
Tejun Heo | 1a57423 | 2013-04-14 11:36:58 -0700 | [diff] [blame] | 1442 | idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1443 | root->hierarchy_id = 0; |
| 1444 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1447 | static int cgroup_test_super(struct super_block *sb, void *data) |
| 1448 | { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1449 | struct cgroup_sb_opts *opts = data; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1450 | struct cgroupfs_root *root = sb->s_fs_info; |
| 1451 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1452 | /* If we asked for a name then it must match */ |
| 1453 | if (opts->name && strcmp(opts->name, root->name)) |
| 1454 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1455 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1456 | /* |
| 1457 | * If we asked for subsystems (or explicitly for no |
| 1458 | * subsystems) then they must match |
| 1459 | */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1460 | if ((opts->subsys_mask || opts->none) |
| 1461 | && (opts->subsys_mask != root->subsys_mask)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1462 | return 0; |
| 1463 | |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1467 | static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts) |
| 1468 | { |
| 1469 | struct cgroupfs_root *root; |
| 1470 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1471 | if (!opts->subsys_mask && !opts->none) |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1472 | return NULL; |
| 1473 | |
| 1474 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 1475 | if (!root) |
| 1476 | return ERR_PTR(-ENOMEM); |
| 1477 | |
| 1478 | init_cgroup_root(root); |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1479 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1480 | root->subsys_mask = opts->subsys_mask; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1481 | root->flags = opts->flags; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 1482 | ida_init(&root->cgroup_ida); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1483 | if (opts->release_agent) |
| 1484 | strcpy(root->release_agent_path, opts->release_agent); |
| 1485 | if (opts->name) |
| 1486 | strcpy(root->name, opts->name); |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 1487 | if (opts->cpuset_clone_children) |
| 1488 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1489 | return root; |
| 1490 | } |
| 1491 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1492 | static void cgroup_free_root(struct cgroupfs_root *root) |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1493 | { |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1494 | if (root) { |
| 1495 | /* hierarhcy ID shoulid already have been released */ |
| 1496 | WARN_ON_ONCE(root->hierarchy_id); |
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 | ida_destroy(&root->cgroup_ida); |
| 1499 | kfree(root); |
| 1500 | } |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1503 | static int cgroup_set_super(struct super_block *sb, void *data) |
| 1504 | { |
| 1505 | int ret; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1506 | struct cgroup_sb_opts *opts = data; |
| 1507 | |
| 1508 | /* If we don't have a new root, we can't set up a new sb */ |
| 1509 | if (!opts->new_root) |
| 1510 | return -EINVAL; |
| 1511 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1512 | BUG_ON(!opts->subsys_mask && !opts->none); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1513 | |
| 1514 | ret = set_anon_super(sb, NULL); |
| 1515 | if (ret) |
| 1516 | return ret; |
| 1517 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1518 | sb->s_fs_info = opts->new_root; |
| 1519 | opts->new_root->sb = sb; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1520 | |
| 1521 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 1522 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 1523 | sb->s_magic = CGROUP_SUPER_MAGIC; |
| 1524 | sb->s_op = &cgroup_ops; |
| 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | static int cgroup_get_rootdir(struct super_block *sb) |
| 1530 | { |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1531 | static const struct dentry_operations cgroup_dops = { |
| 1532 | .d_iput = cgroup_diput, |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 1533 | .d_delete = cgroup_delete, |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1534 | }; |
| 1535 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1536 | struct inode *inode = |
| 1537 | cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1538 | |
| 1539 | if (!inode) |
| 1540 | return -ENOMEM; |
| 1541 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1542 | inode->i_fop = &simple_dir_operations; |
| 1543 | inode->i_op = &cgroup_dir_inode_operations; |
| 1544 | /* directories start off with i_nlink == 2 (for "." entry) */ |
| 1545 | inc_nlink(inode); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 1546 | sb->s_root = d_make_root(inode); |
| 1547 | if (!sb->s_root) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1548 | return -ENOMEM; |
Al Viro | 0df6a63 | 2010-12-21 13:29:29 -0500 | [diff] [blame] | 1549 | /* for everything else we want ->d_op set */ |
| 1550 | sb->s_d_op = &cgroup_dops; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1554 | static struct dentry *cgroup_mount(struct file_system_type *fs_type, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1555 | int flags, const char *unused_dev_name, |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1556 | void *data) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1557 | { |
| 1558 | struct cgroup_sb_opts opts; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1559 | struct cgroupfs_root *root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1560 | int ret = 0; |
| 1561 | struct super_block *sb; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1562 | struct cgroupfs_root *new_root; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1563 | struct inode *inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1564 | |
| 1565 | /* First find the desired set of subsystems */ |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1566 | mutex_lock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1567 | ret = parse_cgroupfs_options(data, &opts); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 1568 | mutex_unlock(&cgroup_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1569 | if (ret) |
| 1570 | goto out_err; |
| 1571 | |
| 1572 | /* |
| 1573 | * Allocate a new cgroup root. We may not need it if we're |
| 1574 | * reusing an existing hierarchy. |
| 1575 | */ |
| 1576 | new_root = cgroup_root_from_opts(&opts); |
| 1577 | if (IS_ERR(new_root)) { |
| 1578 | ret = PTR_ERR(new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1579 | goto drop_modules; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 1580 | } |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1581 | opts.new_root = new_root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1582 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1583 | /* Locate an existing or new sb for this hierarchy */ |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1584 | sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1585 | if (IS_ERR(sb)) { |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1586 | ret = PTR_ERR(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1587 | cgroup_free_root(opts.new_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1588 | goto drop_modules; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1591 | root = sb->s_fs_info; |
| 1592 | BUG_ON(!root); |
| 1593 | if (root == opts.new_root) { |
| 1594 | /* We used the new root structure, so this is a new hierarchy */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1595 | struct list_head tmp_links; |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1596 | struct cgroup *root_cgrp = &root->top_cgroup; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1597 | struct cgroupfs_root *existing_root; |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1598 | const struct cred *cred; |
Li Zefan | 28fd5df | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 1599 | int i; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1600 | struct css_set *cset; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1601 | |
| 1602 | BUG_ON(sb->s_root != NULL); |
| 1603 | |
| 1604 | ret = cgroup_get_rootdir(sb); |
| 1605 | if (ret) |
| 1606 | goto drop_new_super; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1607 | inode = sb->s_root->d_inode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1608 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1609 | mutex_lock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1610 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1611 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1612 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1613 | /* Check for name clashes with existing mounts */ |
| 1614 | ret = -EBUSY; |
| 1615 | if (strlen(root->name)) |
| 1616 | for_each_active_root(existing_root) |
| 1617 | if (!strcmp(existing_root->name, root->name)) |
| 1618 | goto unlock_drop; |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1619 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1620 | /* |
| 1621 | * We're accessing css_set_count without locking |
| 1622 | * css_set_lock here, but that's OK - it can only be |
| 1623 | * increased by someone holding cgroup_lock, and |
| 1624 | * that's us. The worst that can happen is that we |
| 1625 | * have some link structures left over |
| 1626 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1627 | ret = allocate_cgrp_cset_links(css_set_count, &tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1628 | if (ret) |
| 1629 | goto unlock_drop; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1630 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1631 | ret = cgroup_init_root_id(root); |
| 1632 | if (ret) |
| 1633 | goto unlock_drop; |
| 1634 | |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1635 | ret = rebind_subsystems(root, root->subsys_mask, 0); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1636 | if (ret == -EBUSY) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1637 | free_cgrp_cset_links(&tmp_links); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1638 | goto unlock_drop; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1639 | } |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1640 | /* |
| 1641 | * There must be no failure case after here, since rebinding |
| 1642 | * takes care of subsystems' refcounts, which are explicitly |
| 1643 | * dropped in the failure exit path. |
| 1644 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1645 | |
| 1646 | /* EBUSY should be the only error here */ |
| 1647 | BUG_ON(ret); |
| 1648 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1649 | list_add(&root->root_list, &cgroup_roots); |
| 1650 | cgroup_root_count++; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1651 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1652 | sb->s_root->d_fsdata = root_cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1653 | root->top_cgroup.dentry = sb->s_root; |
| 1654 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1655 | /* Link the top cgroup in this hierarchy into all |
| 1656 | * the css_set objects */ |
| 1657 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1658 | hash_for_each(css_set_table, i, cset, hlist) |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1659 | link_css_set(&tmp_links, cset, root_cgrp); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1660 | write_unlock(&css_set_lock); |
| 1661 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1662 | free_cgrp_cset_links(&tmp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1663 | |
Li Zefan | c12f65d | 2009-01-07 18:07:42 -0800 | [diff] [blame] | 1664 | BUG_ON(!list_empty(&root_cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1665 | BUG_ON(root->number_of_cgroups != 1); |
| 1666 | |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1667 | cred = override_creds(&init_cred); |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1668 | cgroup_populate_dir(root_cgrp, true, root->subsys_mask); |
eparis@redhat | 2ce9738 | 2011-06-02 21:20:51 +1000 | [diff] [blame] | 1669 | revert_creds(cred); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1670 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1671 | mutex_unlock(&cgroup_mutex); |
Xiaotian Feng | 34f77a9 | 2009-09-23 15:56:18 -0700 | [diff] [blame] | 1672 | mutex_unlock(&inode->i_mutex); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1673 | } else { |
| 1674 | /* |
| 1675 | * We re-used an existing hierarchy - the new root (if |
| 1676 | * any) is not needed |
| 1677 | */ |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1678 | cgroup_free_root(opts.new_root); |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1679 | |
Jeff Liu | 2a0ff3f | 2013-05-26 21:33:09 +0800 | [diff] [blame] | 1680 | if (root->flags != opts.flags) { |
| 1681 | if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) { |
| 1682 | pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n"); |
| 1683 | ret = -EINVAL; |
| 1684 | goto drop_new_super; |
| 1685 | } else { |
| 1686 | pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n"); |
| 1687 | } |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1690 | /* no subsys rebinding, so refcounts don't change */ |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1691 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1694 | kfree(opts.release_agent); |
| 1695 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1696 | return dget(sb->s_root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1697 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1698 | unlock_drop: |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1699 | cgroup_exit_root_id(root); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1700 | mutex_unlock(&cgroup_root_mutex); |
| 1701 | mutex_unlock(&cgroup_mutex); |
| 1702 | mutex_unlock(&inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1703 | drop_new_super: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 1704 | deactivate_locked_super(sb); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 1705 | drop_modules: |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 1706 | drop_parsed_module_refcounts(opts.subsys_mask); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 1707 | out_err: |
| 1708 | kfree(opts.release_agent); |
| 1709 | kfree(opts.name); |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1710 | return ERR_PTR(ret); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static void cgroup_kill_sb(struct super_block *sb) { |
| 1714 | struct cgroupfs_root *root = sb->s_fs_info; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1715 | struct cgroup *cgrp = &root->top_cgroup; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1716 | struct cgrp_cset_link *link, *tmp_link; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1717 | int ret; |
| 1718 | |
| 1719 | BUG_ON(!root); |
| 1720 | |
| 1721 | BUG_ON(root->number_of_cgroups != 1); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1722 | BUG_ON(!list_empty(&cgrp->children)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1723 | |
| 1724 | mutex_lock(&cgroup_mutex); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1725 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1726 | |
| 1727 | /* Rebind all subsystems back to the default hierarchy */ |
Tejun Heo | a8a648c | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1728 | ret = rebind_subsystems(root, 0, root->subsys_mask); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1729 | /* Shouldn't be able to fail ... */ |
| 1730 | BUG_ON(ret); |
| 1731 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1732 | /* |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1733 | * Release all the links from cset_links to this hierarchy's |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1734 | * root cgroup |
| 1735 | */ |
| 1736 | write_lock(&css_set_lock); |
KOSAKI Motohiro | 71cbb94 | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 1737 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 1738 | list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { |
| 1739 | list_del(&link->cset_link); |
| 1740 | list_del(&link->cgrp_link); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 1741 | kfree(link); |
| 1742 | } |
| 1743 | write_unlock(&css_set_lock); |
| 1744 | |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1745 | if (!list_empty(&root->root_list)) { |
| 1746 | list_del(&root->root_list); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 1747 | cgroup_root_count--; |
Paul Menage | 839ec54 | 2009-01-29 14:25:22 -0800 | [diff] [blame] | 1748 | } |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 1749 | |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1750 | cgroup_exit_root_id(root); |
| 1751 | |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1752 | mutex_unlock(&cgroup_root_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1753 | mutex_unlock(&cgroup_mutex); |
| 1754 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 1755 | simple_xattrs_free(&cgrp->xattrs); |
| 1756 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1757 | kill_litter_super(sb); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 1758 | cgroup_free_root(root); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | static struct file_system_type cgroup_fs_type = { |
| 1762 | .name = "cgroup", |
Al Viro | f7e8357 | 2010-07-26 13:23:11 +0400 | [diff] [blame] | 1763 | .mount = cgroup_mount, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1764 | .kill_sb = cgroup_kill_sb, |
| 1765 | }; |
| 1766 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 1767 | static struct kobject *cgroup_kobj; |
| 1768 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1769 | /** |
| 1770 | * cgroup_path - generate the path of a cgroup |
| 1771 | * @cgrp: the cgroup in question |
| 1772 | * @buf: the buffer to write the path into |
| 1773 | * @buflen: the length of the buffer |
| 1774 | * |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1775 | * Writes path of cgroup into buf. Returns 0 on success, -errno on error. |
| 1776 | * |
| 1777 | * We can't generate cgroup path using dentry->d_name, as accessing |
| 1778 | * dentry->name must be protected by irq-unsafe dentry->d_lock or parent |
| 1779 | * inode's i_mutex, while on the other hand cgroup_path() can be called |
| 1780 | * with some irq-safe spinlocks held. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1781 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 1782 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1783 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1784 | int ret = -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1785 | char *start; |
Tejun Heo | febfcef | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 1786 | |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1787 | if (!cgrp->parent) { |
| 1788 | if (strlcpy(buf, "/", buflen) >= buflen) |
| 1789 | return -ENAMETOOLONG; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1790 | return 0; |
| 1791 | } |
| 1792 | |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1793 | start = buf + buflen - 1; |
Tao Ma | 316eb66 | 2012-11-08 21:36:38 +0800 | [diff] [blame] | 1794 | *start = '\0'; |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1795 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1796 | rcu_read_lock(); |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1797 | do { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1798 | const char *name = cgroup_name(cgrp); |
| 1799 | int len; |
| 1800 | |
| 1801 | len = strlen(name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1802 | if ((start -= len) < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1803 | goto out; |
| 1804 | memcpy(start, name, len); |
Li Zefan | 9a9686b | 2010-04-22 17:29:24 +0800 | [diff] [blame] | 1805 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1806 | if (--start < buf) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1807 | goto out; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1808 | *start = '/'; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1809 | |
| 1810 | cgrp = cgrp->parent; |
Tejun Heo | da1f296 | 2013-04-14 10:32:19 -0700 | [diff] [blame] | 1811 | } while (cgrp->parent); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1812 | ret = 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1813 | memmove(buf, start, buf + buflen - start); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 1814 | out: |
| 1815 | rcu_read_unlock(); |
| 1816 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1817 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 1818 | EXPORT_SYMBOL_GPL(cgroup_path); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 1819 | |
Tejun Heo | 857a2be | 2013-04-14 20:50:08 -0700 | [diff] [blame] | 1820 | /** |
| 1821 | * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy |
| 1822 | * @task: target task |
| 1823 | * @hierarchy_id: the hierarchy to look up @task's cgroup from |
| 1824 | * @buf: the buffer to write the path into |
| 1825 | * @buflen: the length of the buffer |
| 1826 | * |
| 1827 | * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and |
| 1828 | * copy its path into @buf. This function grabs cgroup_mutex and shouldn't |
| 1829 | * be used inside locks used by cgroup controller callbacks. |
| 1830 | */ |
| 1831 | int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id, |
| 1832 | char *buf, size_t buflen) |
| 1833 | { |
| 1834 | struct cgroupfs_root *root; |
| 1835 | struct cgroup *cgrp = NULL; |
| 1836 | int ret = -ENOENT; |
| 1837 | |
| 1838 | mutex_lock(&cgroup_mutex); |
| 1839 | |
| 1840 | root = idr_find(&cgroup_hierarchy_idr, hierarchy_id); |
| 1841 | if (root) { |
| 1842 | cgrp = task_cgroup_from_root(task, root); |
| 1843 | ret = cgroup_path(cgrp, buf, buflen); |
| 1844 | } |
| 1845 | |
| 1846 | mutex_unlock(&cgroup_mutex); |
| 1847 | |
| 1848 | return ret; |
| 1849 | } |
| 1850 | EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy); |
| 1851 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1852 | /* |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1853 | * Control Group taskset |
| 1854 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1855 | struct task_and_cgroup { |
| 1856 | struct task_struct *task; |
| 1857 | struct cgroup *cgrp; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 1858 | struct css_set *cg; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1859 | }; |
| 1860 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1861 | struct cgroup_taskset { |
| 1862 | struct task_and_cgroup single; |
| 1863 | struct flex_array *tc_array; |
| 1864 | int tc_array_len; |
| 1865 | int idx; |
| 1866 | struct cgroup *cur_cgrp; |
| 1867 | }; |
| 1868 | |
| 1869 | /** |
| 1870 | * cgroup_taskset_first - reset taskset and return the first task |
| 1871 | * @tset: taskset of interest |
| 1872 | * |
| 1873 | * @tset iteration is initialized and the first task is returned. |
| 1874 | */ |
| 1875 | struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset) |
| 1876 | { |
| 1877 | if (tset->tc_array) { |
| 1878 | tset->idx = 0; |
| 1879 | return cgroup_taskset_next(tset); |
| 1880 | } else { |
| 1881 | tset->cur_cgrp = tset->single.cgrp; |
| 1882 | return tset->single.task; |
| 1883 | } |
| 1884 | } |
| 1885 | EXPORT_SYMBOL_GPL(cgroup_taskset_first); |
| 1886 | |
| 1887 | /** |
| 1888 | * cgroup_taskset_next - iterate to the next task in taskset |
| 1889 | * @tset: taskset of interest |
| 1890 | * |
| 1891 | * Return the next task in @tset. Iteration must have been initialized |
| 1892 | * with cgroup_taskset_first(). |
| 1893 | */ |
| 1894 | struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset) |
| 1895 | { |
| 1896 | struct task_and_cgroup *tc; |
| 1897 | |
| 1898 | if (!tset->tc_array || tset->idx >= tset->tc_array_len) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | tc = flex_array_get(tset->tc_array, tset->idx++); |
| 1902 | tset->cur_cgrp = tc->cgrp; |
| 1903 | return tc->task; |
| 1904 | } |
| 1905 | EXPORT_SYMBOL_GPL(cgroup_taskset_next); |
| 1906 | |
| 1907 | /** |
| 1908 | * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task |
| 1909 | * @tset: taskset of interest |
| 1910 | * |
| 1911 | * Return the cgroup for the current (last returned) task of @tset. This |
| 1912 | * function must be preceded by either cgroup_taskset_first() or |
| 1913 | * cgroup_taskset_next(). |
| 1914 | */ |
| 1915 | struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset) |
| 1916 | { |
| 1917 | return tset->cur_cgrp; |
| 1918 | } |
| 1919 | EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup); |
| 1920 | |
| 1921 | /** |
| 1922 | * cgroup_taskset_size - return the number of tasks in taskset |
| 1923 | * @tset: taskset of interest |
| 1924 | */ |
| 1925 | int cgroup_taskset_size(struct cgroup_taskset *tset) |
| 1926 | { |
| 1927 | return tset->tc_array ? tset->tc_array_len : 1; |
| 1928 | } |
| 1929 | EXPORT_SYMBOL_GPL(cgroup_taskset_size); |
| 1930 | |
| 1931 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1932 | /* |
| 1933 | * cgroup_task_migrate - move a task from one cgroup to another. |
| 1934 | * |
Tao Ma | d0b2fdd | 2012-11-20 22:06:18 +0800 | [diff] [blame] | 1935 | * Must be called with cgroup_mutex and threadgroup locked. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1936 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1937 | static void cgroup_task_migrate(struct cgroup *old_cgrp, |
| 1938 | struct task_struct *tsk, |
| 1939 | struct css_set *new_cset) |
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 | struct css_set *old_cset; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1942 | |
| 1943 | /* |
Mandeep Singh Baines | 026085e | 2011-12-21 20:18:35 -0800 | [diff] [blame] | 1944 | * We are synchronized through threadgroup_lock() against PF_EXITING |
| 1945 | * setting such that we can't race against cgroup_exit() changing the |
| 1946 | * css_set to init_css_set and dropping the old one. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1947 | */ |
Frederic Weisbecker | c84cdf7 | 2011-12-21 20:03:18 +0100 | [diff] [blame] | 1948 | WARN_ON_ONCE(tsk->flags & PF_EXITING); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1949 | old_cset = tsk->cgroups; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1950 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1951 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1952 | rcu_assign_pointer(tsk->cgroups, new_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1953 | task_unlock(tsk); |
| 1954 | |
| 1955 | /* Update the css_set linked lists if we're using them */ |
| 1956 | write_lock(&css_set_lock); |
| 1957 | if (!list_empty(&tsk->cg_list)) |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1958 | list_move(&tsk->cg_list, &new_cset->tasks); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1959 | write_unlock(&css_set_lock); |
| 1960 | |
| 1961 | /* |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1962 | * We just gained a reference on old_cset by taking it from the |
| 1963 | * task. As trading it for new_cset is protected by cgroup_mutex, |
| 1964 | * 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] | 1965 | */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 1966 | set_bit(CGRP_RELEASABLE, &old_cgrp->flags); |
| 1967 | put_css_set(old_cset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 1970 | /** |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1971 | * 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] | 1972 | * @cgrp: the cgroup to attach to |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1973 | * @tsk: the task or the leader of the threadgroup to be attached |
| 1974 | * @threadgroup: attach the whole threadgroup? |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1975 | * |
Tejun Heo | 257058a | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1976 | * 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] | 1977 | * 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] | 1978 | */ |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 1979 | static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk, |
| 1980 | bool threadgroup) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1981 | { |
| 1982 | int retval, i, group_size; |
| 1983 | struct cgroup_subsys *ss, *failed_ss = NULL; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1984 | struct cgroupfs_root *root = cgrp->root; |
| 1985 | /* threadgroup list cursor and array */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1986 | struct task_struct *leader = tsk; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1987 | struct task_and_cgroup *tc; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 1988 | struct flex_array *group; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 1989 | struct cgroup_taskset tset = { }; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1990 | |
| 1991 | /* |
| 1992 | * step 0: in order to do expensive, possibly blocking operations for |
| 1993 | * every thread, we cannot iterate the thread group list, since it needs |
| 1994 | * 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] | 1995 | * group - group_rwsem prevents new threads from appearing, and if |
| 1996 | * threads exit, this will just be an over-estimate. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 1997 | */ |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 1998 | if (threadgroup) |
| 1999 | group_size = get_nr_threads(tsk); |
| 2000 | else |
| 2001 | group_size = 1; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2002 | /* flex_array supports very large thread-groups better than kmalloc. */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2003 | group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2004 | if (!group) |
| 2005 | return -ENOMEM; |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2006 | /* pre-allocate to guarantee space while iterating in rcu read-side. */ |
Li Zefan | 3ac1707 | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 2007 | retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2008 | if (retval) |
| 2009 | goto out_free_group_list; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2010 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2011 | i = 0; |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2012 | /* |
| 2013 | * Prevent freeing of tasks while we take a snapshot. Tasks that are |
| 2014 | * already PF_EXITING could be freed from underneath us unless we |
| 2015 | * take an rcu_read_lock. |
| 2016 | */ |
| 2017 | rcu_read_lock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2018 | do { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2019 | struct task_and_cgroup ent; |
| 2020 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2021 | /* @tsk either already exited or can't exit until the end */ |
| 2022 | if (tsk->flags & PF_EXITING) |
| 2023 | continue; |
| 2024 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2025 | /* as per above, nr_threads may decrease, but not increase. */ |
| 2026 | BUG_ON(i >= group_size); |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2027 | ent.task = tsk; |
| 2028 | ent.cgrp = task_cgroup_from_root(tsk, root); |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2029 | /* nothing to do if this task is already in the cgroup */ |
| 2030 | if (ent.cgrp == cgrp) |
| 2031 | continue; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2032 | /* |
| 2033 | * saying GFP_ATOMIC has no effect here because we did prealloc |
| 2034 | * earlier, but it's good form to communicate our expectations. |
| 2035 | */ |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2036 | retval = flex_array_put(group, i, &ent, GFP_ATOMIC); |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2037 | BUG_ON(retval != 0); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2038 | i++; |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2039 | |
| 2040 | if (!threadgroup) |
| 2041 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2042 | } while_each_thread(leader, tsk); |
Mandeep Singh Baines | fb5d2b4 | 2012-01-03 21:18:31 -0800 | [diff] [blame] | 2043 | rcu_read_unlock(); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2044 | /* remember the number of threads in the array for later. */ |
| 2045 | group_size = i; |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2046 | tset.tc_array = group; |
| 2047 | tset.tc_array_len = group_size; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2048 | |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2049 | /* methods shouldn't be called if no task is actually migrating */ |
| 2050 | retval = 0; |
Mandeep Singh Baines | 892a2b9 | 2011-12-21 20:18:37 -0800 | [diff] [blame] | 2051 | if (!group_size) |
Mandeep Singh Baines | b07ef77 | 2011-12-21 20:18:36 -0800 | [diff] [blame] | 2052 | goto out_free_group_list; |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2053 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2054 | /* |
| 2055 | * step 1: check that we can legitimately attach to the cgroup. |
| 2056 | */ |
| 2057 | for_each_subsys(root, ss) { |
| 2058 | if (ss->can_attach) { |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2059 | retval = ss->can_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2060 | if (retval) { |
| 2061 | failed_ss = ss; |
| 2062 | goto out_cancel_attach; |
| 2063 | } |
| 2064 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * step 2: make sure css_sets exist for all threads to be migrated. |
| 2069 | * we use find_css_set, which allocates a new one if necessary. |
| 2070 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2071 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2072 | tc = flex_array_get(group, i); |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2073 | tc->cg = find_css_set(tc->task->cgroups, cgrp); |
| 2074 | if (!tc->cg) { |
| 2075 | retval = -ENOMEM; |
| 2076 | goto out_put_css_set_refs; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2081 | * step 3: now that we're guaranteed success wrt the css_sets, |
| 2082 | * proceed to move all tasks to the new cgroup. There are no |
| 2083 | * failure cases after here, so this is the commit point. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2084 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2085 | for (i = 0; i < group_size; i++) { |
Tejun Heo | 134d337 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2086 | tc = flex_array_get(group, i); |
Kevin Wilson | 1e2ccd1 | 2013-04-01 10:51:37 +0300 | [diff] [blame] | 2087 | cgroup_task_migrate(tc->cgrp, tc->task, tc->cg); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2088 | } |
| 2089 | /* nothing is sensitive to fork() after this point. */ |
| 2090 | |
| 2091 | /* |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2092 | * step 4: do subsystem attach callbacks. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2093 | */ |
| 2094 | for_each_subsys(root, ss) { |
| 2095 | if (ss->attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2096 | ss->attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * step 5: success! and cleanup |
| 2101 | */ |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2102 | retval = 0; |
Mandeep Singh Baines | 61d1d21 | 2012-01-30 12:51:56 -0800 | [diff] [blame] | 2103 | out_put_css_set_refs: |
| 2104 | if (retval) { |
| 2105 | for (i = 0; i < group_size; i++) { |
| 2106 | tc = flex_array_get(group, i); |
| 2107 | if (!tc->cg) |
| 2108 | break; |
| 2109 | put_css_set(tc->cg); |
| 2110 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2111 | } |
| 2112 | out_cancel_attach: |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2113 | if (retval) { |
| 2114 | for_each_subsys(root, ss) { |
Tejun Heo | 494c167 | 2011-12-12 18:12:22 -0800 | [diff] [blame] | 2115 | if (ss == failed_ss) |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2116 | break; |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2117 | if (ss->cancel_attach) |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 2118 | ss->cancel_attach(cgrp, &tset); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2119 | } |
| 2120 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2121 | out_free_group_list: |
Ben Blum | d846687 | 2011-05-26 16:25:21 -0700 | [diff] [blame] | 2122 | flex_array_free(group); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2123 | return retval; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * 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] | 2128 | * function to attach either it or all tasks in its threadgroup. Will lock |
| 2129 | * cgroup_mutex and threadgroup; may take task_lock of task. |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2130 | */ |
| 2131 | 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] | 2132 | { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2133 | struct task_struct *tsk; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2134 | const struct cred *cred = current_cred(), *tcred; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2135 | int ret; |
| 2136 | |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2137 | if (!cgroup_lock_live_group(cgrp)) |
| 2138 | return -ENODEV; |
| 2139 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2140 | retry_find_task: |
| 2141 | rcu_read_lock(); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2142 | if (pid) { |
Pavel Emelyanov | 73507f3 | 2008-02-07 00:14:47 -0800 | [diff] [blame] | 2143 | tsk = find_task_by_vpid(pid); |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2144 | if (!tsk) { |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2145 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2146 | ret= -ESRCH; |
| 2147 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2148 | } |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2149 | /* |
| 2150 | * even if we're attaching all tasks in the thread group, we |
| 2151 | * only need to check permissions on one of them. |
| 2152 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2153 | tcred = __task_cred(tsk); |
Eric W. Biederman | 14a590c | 2012-03-12 15:44:39 -0700 | [diff] [blame] | 2154 | if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && |
| 2155 | !uid_eq(cred->euid, tcred->uid) && |
| 2156 | !uid_eq(cred->euid, tcred->suid)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2157 | rcu_read_unlock(); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2158 | ret = -EACCES; |
| 2159 | goto out_unlock_cgroup; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2160 | } |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2161 | } else |
| 2162 | tsk = current; |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2163 | |
| 2164 | if (threadgroup) |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2165 | tsk = tsk->group_leader; |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2166 | |
| 2167 | /* |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2168 | * Workqueue threads may acquire PF_NO_SETAFFINITY and become |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2169 | * trapped in a cpuset, or RT worker may be born in a cgroup |
| 2170 | * with no rt_runtime allocated. Just say no. |
| 2171 | */ |
Tejun Heo | 14a40ff | 2013-03-19 13:45:20 -0700 | [diff] [blame] | 2172 | if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) { |
Mike Galbraith | c4c27fb | 2012-04-21 09:13:46 +0200 | [diff] [blame] | 2173 | ret = -EINVAL; |
| 2174 | rcu_read_unlock(); |
| 2175 | goto out_unlock_cgroup; |
| 2176 | } |
| 2177 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2178 | get_task_struct(tsk); |
| 2179 | rcu_read_unlock(); |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2180 | |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2181 | threadgroup_lock(tsk); |
| 2182 | if (threadgroup) { |
| 2183 | if (!thread_group_leader(tsk)) { |
| 2184 | /* |
| 2185 | * a race with de_thread from another thread's exec() |
| 2186 | * may strip us of our leadership, if this happens, |
| 2187 | * there is no choice but to throw this task away and |
| 2188 | * try again; this is |
| 2189 | * "double-double-toil-and-trouble-check locking". |
| 2190 | */ |
| 2191 | threadgroup_unlock(tsk); |
| 2192 | put_task_struct(tsk); |
| 2193 | goto retry_find_task; |
| 2194 | } |
Li Zefan | 081aa45 | 2013-03-13 09:17:09 +0800 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | ret = cgroup_attach_task(cgrp, tsk, threadgroup); |
| 2198 | |
Tejun Heo | cd3d095 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2199 | threadgroup_unlock(tsk); |
| 2200 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2201 | put_task_struct(tsk); |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2202 | out_unlock_cgroup: |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2203 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2204 | return ret; |
| 2205 | } |
| 2206 | |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2207 | /** |
| 2208 | * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from' |
| 2209 | * @from: attach to all cgroups of a given task |
| 2210 | * @tsk: the task to be attached |
| 2211 | */ |
| 2212 | int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk) |
| 2213 | { |
| 2214 | struct cgroupfs_root *root; |
| 2215 | int retval = 0; |
| 2216 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2217 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2218 | for_each_active_root(root) { |
| 2219 | struct cgroup *from_cg = task_cgroup_from_root(from, root); |
| 2220 | |
| 2221 | retval = cgroup_attach_task(from_cg, tsk, false); |
| 2222 | if (retval) |
| 2223 | break; |
| 2224 | } |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2225 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 7ae1bad | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2226 | |
| 2227 | return retval; |
| 2228 | } |
| 2229 | EXPORT_SYMBOL_GPL(cgroup_attach_task_all); |
| 2230 | |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2231 | static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid) |
| 2232 | { |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 2233 | return attach_task_by_pid(cgrp, pid, false); |
| 2234 | } |
| 2235 | |
| 2236 | static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid) |
| 2237 | { |
Mandeep Singh Baines | b78949e | 2012-01-03 21:18:30 -0800 | [diff] [blame] | 2238 | return attach_task_by_pid(cgrp, tgid, true); |
Paul Menage | af35102 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2241 | static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft, |
| 2242 | const char *buffer) |
| 2243 | { |
| 2244 | BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX); |
Evgeny Kuznetsov | f4a2589 | 2010-10-27 15:33:37 -0700 | [diff] [blame] | 2245 | if (strlen(buffer) >= PATH_MAX) |
| 2246 | return -EINVAL; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2247 | if (!cgroup_lock_live_group(cgrp)) |
| 2248 | return -ENODEV; |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2249 | mutex_lock(&cgroup_root_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2250 | strcpy(cgrp->root->release_agent_path, buffer); |
Tejun Heo | e25e2cb | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 2251 | mutex_unlock(&cgroup_root_mutex); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2252 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft, |
| 2257 | struct seq_file *seq) |
| 2258 | { |
| 2259 | if (!cgroup_lock_live_group(cgrp)) |
| 2260 | return -ENODEV; |
| 2261 | seq_puts(seq, cgrp->root->release_agent_path); |
| 2262 | seq_putc(seq, '\n'); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 2263 | mutex_unlock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2267 | static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft, |
| 2268 | struct seq_file *seq) |
| 2269 | { |
| 2270 | seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp)); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 2271 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2274 | /* A buffer size big enough for numbers or short strings */ |
| 2275 | #define CGROUP_LOCAL_BUFFER_SIZE 64 |
| 2276 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2277 | static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft, |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2278 | struct file *file, |
| 2279 | const char __user *userbuf, |
| 2280 | size_t nbytes, loff_t *unused_ppos) |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2281 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2282 | char buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2283 | int retval = 0; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2284 | char *end; |
| 2285 | |
| 2286 | if (!nbytes) |
| 2287 | return -EINVAL; |
| 2288 | if (nbytes >= sizeof(buffer)) |
| 2289 | return -E2BIG; |
| 2290 | if (copy_from_user(buffer, userbuf, nbytes)) |
| 2291 | return -EFAULT; |
| 2292 | |
| 2293 | buffer[nbytes] = 0; /* nul-terminate */ |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2294 | if (cft->write_u64) { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2295 | u64 val = simple_strtoull(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2296 | if (*end) |
| 2297 | return -EINVAL; |
| 2298 | retval = cft->write_u64(cgrp, cft, val); |
| 2299 | } else { |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2300 | s64 val = simple_strtoll(strstrip(buffer), &end, 0); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2301 | if (*end) |
| 2302 | return -EINVAL; |
| 2303 | retval = cft->write_s64(cgrp, cft, val); |
| 2304 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2305 | if (!retval) |
| 2306 | retval = nbytes; |
| 2307 | return retval; |
| 2308 | } |
| 2309 | |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2310 | static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft, |
| 2311 | struct file *file, |
| 2312 | const char __user *userbuf, |
| 2313 | size_t nbytes, loff_t *unused_ppos) |
| 2314 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2315 | char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2316 | int retval = 0; |
| 2317 | size_t max_bytes = cft->max_write_len; |
| 2318 | char *buffer = local_buffer; |
| 2319 | |
| 2320 | if (!max_bytes) |
| 2321 | max_bytes = sizeof(local_buffer) - 1; |
| 2322 | if (nbytes >= max_bytes) |
| 2323 | return -E2BIG; |
| 2324 | /* Allocate a dynamic buffer if we need one */ |
| 2325 | if (nbytes >= sizeof(local_buffer)) { |
| 2326 | buffer = kmalloc(nbytes + 1, GFP_KERNEL); |
| 2327 | if (buffer == NULL) |
| 2328 | return -ENOMEM; |
| 2329 | } |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2330 | if (nbytes && copy_from_user(buffer, userbuf, nbytes)) { |
| 2331 | retval = -EFAULT; |
| 2332 | goto out; |
| 2333 | } |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2334 | |
| 2335 | buffer[nbytes] = 0; /* nul-terminate */ |
KOSAKI Motohiro | 478988d | 2009-10-26 16:49:36 -0700 | [diff] [blame] | 2336 | retval = cft->write_string(cgrp, cft, strstrip(buffer)); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2337 | if (!retval) |
| 2338 | retval = nbytes; |
Li Zefan | 5a3eb9f | 2008-07-29 22:33:18 -0700 | [diff] [blame] | 2339 | out: |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2340 | if (buffer != local_buffer) |
| 2341 | kfree(buffer); |
| 2342 | return retval; |
| 2343 | } |
| 2344 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2345 | static ssize_t cgroup_file_write(struct file *file, const char __user *buf, |
| 2346 | size_t nbytes, loff_t *ppos) |
| 2347 | { |
| 2348 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2349 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2350 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2351 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2352 | return -ENODEV; |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2353 | if (cft->write) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2354 | return cft->write(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2355 | if (cft->write_u64 || cft->write_s64) |
| 2356 | return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | db3b149 | 2008-07-25 01:46:58 -0700 | [diff] [blame] | 2357 | if (cft->write_string) |
| 2358 | return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos); |
Pavel Emelyanov | d447ea2 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 2359 | if (cft->trigger) { |
| 2360 | int ret = cft->trigger(cgrp, (unsigned int)cft->private); |
| 2361 | return ret ? ret : nbytes; |
| 2362 | } |
Paul Menage | 355e0c4 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 2363 | return -EINVAL; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2366 | static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft, |
| 2367 | struct file *file, |
| 2368 | char __user *buf, size_t nbytes, |
| 2369 | loff_t *ppos) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2370 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2371 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2372 | u64 val = cft->read_u64(cgrp, cft); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2373 | int len = sprintf(tmp, "%llu\n", (unsigned long long) val); |
| 2374 | |
| 2375 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2376 | } |
| 2377 | |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2378 | static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft, |
| 2379 | struct file *file, |
| 2380 | char __user *buf, size_t nbytes, |
| 2381 | loff_t *ppos) |
| 2382 | { |
Paul Menage | 84eea84 | 2008-07-25 01:47:00 -0700 | [diff] [blame] | 2383 | char tmp[CGROUP_LOCAL_BUFFER_SIZE]; |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2384 | s64 val = cft->read_s64(cgrp, cft); |
| 2385 | int len = sprintf(tmp, "%lld\n", (long long) val); |
| 2386 | |
| 2387 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 2388 | } |
| 2389 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2390 | static ssize_t cgroup_file_read(struct file *file, char __user *buf, |
| 2391 | size_t nbytes, loff_t *ppos) |
| 2392 | { |
| 2393 | struct cftype *cft = __d_cft(file->f_dentry); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2394 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2395 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 2396 | if (cgroup_is_dead(cgrp)) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2397 | return -ENODEV; |
| 2398 | |
| 2399 | if (cft->read) |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2400 | return cft->read(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | f4c753b | 2008-04-29 00:59:56 -0700 | [diff] [blame] | 2401 | if (cft->read_u64) |
| 2402 | return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | e73d2c6 | 2008-04-29 01:00:06 -0700 | [diff] [blame] | 2403 | if (cft->read_s64) |
| 2404 | return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2408 | /* |
| 2409 | * seqfile ops/methods for returning structured data. Currently just |
| 2410 | * supports string->u64 maps, but can be extended in future. |
| 2411 | */ |
| 2412 | |
| 2413 | struct cgroup_seqfile_state { |
| 2414 | struct cftype *cft; |
| 2415 | struct cgroup *cgroup; |
| 2416 | }; |
| 2417 | |
| 2418 | static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value) |
| 2419 | { |
| 2420 | struct seq_file *sf = cb->state; |
| 2421 | return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value); |
| 2422 | } |
| 2423 | |
| 2424 | static int cgroup_seqfile_show(struct seq_file *m, void *arg) |
| 2425 | { |
| 2426 | struct cgroup_seqfile_state *state = m->private; |
| 2427 | struct cftype *cft = state->cft; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2428 | if (cft->read_map) { |
| 2429 | struct cgroup_map_cb cb = { |
| 2430 | .fill = cgroup_map_add, |
| 2431 | .state = m, |
| 2432 | }; |
| 2433 | return cft->read_map(state->cgroup, cft, &cb); |
| 2434 | } |
| 2435 | return cft->read_seq_string(state->cgroup, cft, m); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
Adrian Bunk | 96930a6 | 2008-07-25 19:46:21 -0700 | [diff] [blame] | 2438 | static int cgroup_seqfile_release(struct inode *inode, struct file *file) |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2439 | { |
| 2440 | struct seq_file *seq = file->private_data; |
| 2441 | kfree(seq->private); |
| 2442 | return single_release(inode, file); |
| 2443 | } |
| 2444 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2445 | static const struct file_operations cgroup_seqfile_operations = { |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2446 | .read = seq_read, |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 2447 | .write = cgroup_file_write, |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2448 | .llseek = seq_lseek, |
| 2449 | .release = cgroup_seqfile_release, |
| 2450 | }; |
| 2451 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2452 | static int cgroup_file_open(struct inode *inode, struct file *file) |
| 2453 | { |
| 2454 | int err; |
| 2455 | struct cftype *cft; |
| 2456 | |
| 2457 | err = generic_file_open(inode, file); |
| 2458 | if (err) |
| 2459 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2460 | cft = __d_cft(file->f_dentry); |
Li Zefan | 75139b8 | 2009-01-07 18:07:33 -0800 | [diff] [blame] | 2461 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 2462 | if (cft->read_map || cft->read_seq_string) { |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2463 | struct cgroup_seqfile_state *state; |
| 2464 | |
| 2465 | state = kzalloc(sizeof(*state), GFP_USER); |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2466 | if (!state) |
| 2467 | return -ENOMEM; |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 2468 | |
Paul Menage | 9179656 | 2008-04-29 01:00:01 -0700 | [diff] [blame] | 2469 | state->cft = cft; |
| 2470 | state->cgroup = __d_cgrp(file->f_dentry->d_parent); |
| 2471 | file->f_op = &cgroup_seqfile_operations; |
| 2472 | err = single_open(file, cgroup_seqfile_show, state); |
| 2473 | if (err < 0) |
| 2474 | kfree(state); |
| 2475 | } else if (cft->open) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2476 | err = cft->open(inode, file); |
| 2477 | else |
| 2478 | err = 0; |
| 2479 | |
| 2480 | return err; |
| 2481 | } |
| 2482 | |
| 2483 | static int cgroup_file_release(struct inode *inode, struct file *file) |
| 2484 | { |
| 2485 | struct cftype *cft = __d_cft(file->f_dentry); |
| 2486 | if (cft->release) |
| 2487 | return cft->release(inode, file); |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
| 2491 | /* |
| 2492 | * cgroup_rename - Only allow simple rename of directories in place. |
| 2493 | */ |
| 2494 | static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2495 | struct inode *new_dir, struct dentry *new_dentry) |
| 2496 | { |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2497 | int ret; |
| 2498 | struct cgroup_name *name, *old_name; |
| 2499 | struct cgroup *cgrp; |
| 2500 | |
| 2501 | /* |
| 2502 | * It's convinient to use parent dir's i_mutex to protected |
| 2503 | * cgrp->name. |
| 2504 | */ |
| 2505 | lockdep_assert_held(&old_dir->i_mutex); |
| 2506 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2507 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2508 | return -ENOTDIR; |
| 2509 | if (new_dentry->d_inode) |
| 2510 | return -EEXIST; |
| 2511 | if (old_dir != new_dir) |
| 2512 | return -EIO; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2513 | |
| 2514 | cgrp = __d_cgrp(old_dentry); |
| 2515 | |
Tejun Heo | 6db8e85 | 2013-06-14 11:18:22 -0700 | [diff] [blame] | 2516 | /* |
| 2517 | * This isn't a proper migration and its usefulness is very |
| 2518 | * limited. Disallow if sane_behavior. |
| 2519 | */ |
| 2520 | if (cgroup_sane_behavior(cgrp)) |
| 2521 | return -EPERM; |
| 2522 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 2523 | name = cgroup_alloc_name(new_dentry); |
| 2524 | if (!name) |
| 2525 | return -ENOMEM; |
| 2526 | |
| 2527 | ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2528 | if (ret) { |
| 2529 | kfree(name); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | old_name = cgrp->name; |
| 2534 | rcu_assign_pointer(cgrp->name, name); |
| 2535 | |
| 2536 | kfree_rcu(old_name, rcu_head); |
| 2537 | return 0; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2540 | static struct simple_xattrs *__d_xattrs(struct dentry *dentry) |
| 2541 | { |
| 2542 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 2543 | return &__d_cgrp(dentry)->xattrs; |
| 2544 | else |
Li Zefan | 712317a | 2013-04-18 23:09:52 -0700 | [diff] [blame] | 2545 | return &__d_cfe(dentry)->xattrs; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | static inline int xattr_enabled(struct dentry *dentry) |
| 2549 | { |
| 2550 | struct cgroupfs_root *root = dentry->d_sb->s_fs_info; |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2551 | return root->flags & CGRP_ROOT_XATTR; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | static bool is_valid_xattr(const char *name) |
| 2555 | { |
| 2556 | if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || |
| 2557 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) |
| 2558 | return true; |
| 2559 | return false; |
| 2560 | } |
| 2561 | |
| 2562 | static int cgroup_setxattr(struct dentry *dentry, const char *name, |
| 2563 | const void *val, size_t size, int flags) |
| 2564 | { |
| 2565 | if (!xattr_enabled(dentry)) |
| 2566 | return -EOPNOTSUPP; |
| 2567 | if (!is_valid_xattr(name)) |
| 2568 | return -EINVAL; |
| 2569 | return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags); |
| 2570 | } |
| 2571 | |
| 2572 | static int cgroup_removexattr(struct dentry *dentry, const char *name) |
| 2573 | { |
| 2574 | if (!xattr_enabled(dentry)) |
| 2575 | return -EOPNOTSUPP; |
| 2576 | if (!is_valid_xattr(name)) |
| 2577 | return -EINVAL; |
| 2578 | return simple_xattr_remove(__d_xattrs(dentry), name); |
| 2579 | } |
| 2580 | |
| 2581 | static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name, |
| 2582 | void *buf, size_t size) |
| 2583 | { |
| 2584 | if (!xattr_enabled(dentry)) |
| 2585 | return -EOPNOTSUPP; |
| 2586 | if (!is_valid_xattr(name)) |
| 2587 | return -EINVAL; |
| 2588 | return simple_xattr_get(__d_xattrs(dentry), name, buf, size); |
| 2589 | } |
| 2590 | |
| 2591 | static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 2592 | { |
| 2593 | if (!xattr_enabled(dentry)) |
| 2594 | return -EOPNOTSUPP; |
| 2595 | return simple_xattr_list(__d_xattrs(dentry), buf, size); |
| 2596 | } |
| 2597 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2598 | static const struct file_operations cgroup_file_operations = { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2599 | .read = cgroup_file_read, |
| 2600 | .write = cgroup_file_write, |
| 2601 | .llseek = generic_file_llseek, |
| 2602 | .open = cgroup_file_open, |
| 2603 | .release = cgroup_file_release, |
| 2604 | }; |
| 2605 | |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2606 | static const struct inode_operations cgroup_file_inode_operations = { |
| 2607 | .setxattr = cgroup_setxattr, |
| 2608 | .getxattr = cgroup_getxattr, |
| 2609 | .listxattr = cgroup_listxattr, |
| 2610 | .removexattr = cgroup_removexattr, |
| 2611 | }; |
| 2612 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 2613 | static const struct inode_operations cgroup_dir_inode_operations = { |
Al Viro | c72a04e | 2011-01-14 05:31:45 +0000 | [diff] [blame] | 2614 | .lookup = cgroup_lookup, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2615 | .mkdir = cgroup_mkdir, |
| 2616 | .rmdir = cgroup_rmdir, |
| 2617 | .rename = cgroup_rename, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2618 | .setxattr = cgroup_setxattr, |
| 2619 | .getxattr = cgroup_getxattr, |
| 2620 | .listxattr = cgroup_listxattr, |
| 2621 | .removexattr = cgroup_removexattr, |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2622 | }; |
| 2623 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2624 | 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] | 2625 | { |
| 2626 | if (dentry->d_name.len > NAME_MAX) |
| 2627 | return ERR_PTR(-ENAMETOOLONG); |
| 2628 | d_add(dentry, NULL); |
| 2629 | return NULL; |
| 2630 | } |
| 2631 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2632 | /* |
| 2633 | * Check if a file is a control file |
| 2634 | */ |
| 2635 | static inline struct cftype *__file_cft(struct file *file) |
| 2636 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2637 | if (file_inode(file)->i_fop != &cgroup_file_operations) |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 2638 | return ERR_PTR(-EINVAL); |
| 2639 | return __d_cft(file->f_dentry); |
| 2640 | } |
| 2641 | |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2642 | static int cgroup_create_file(struct dentry *dentry, umode_t mode, |
Nick Piggin | 5adcee1 | 2011-01-07 17:49:20 +1100 | [diff] [blame] | 2643 | struct super_block *sb) |
| 2644 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2645 | struct inode *inode; |
| 2646 | |
| 2647 | if (!dentry) |
| 2648 | return -ENOENT; |
| 2649 | if (dentry->d_inode) |
| 2650 | return -EEXIST; |
| 2651 | |
| 2652 | inode = cgroup_new_inode(mode, sb); |
| 2653 | if (!inode) |
| 2654 | return -ENOMEM; |
| 2655 | |
| 2656 | if (S_ISDIR(mode)) { |
| 2657 | inode->i_op = &cgroup_dir_inode_operations; |
| 2658 | inode->i_fop = &simple_dir_operations; |
| 2659 | |
| 2660 | /* start off with i_nlink == 2 (for "." entry) */ |
| 2661 | inc_nlink(inode); |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 2662 | inc_nlink(dentry->d_parent->d_inode); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2663 | |
Tejun Heo | b8a2df6a | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 2664 | /* |
| 2665 | * Control reaches here with cgroup_mutex held. |
| 2666 | * @inode->i_mutex should nest outside cgroup_mutex but we |
| 2667 | * want to populate it immediately without releasing |
| 2668 | * cgroup_mutex. As @inode isn't visible to anyone else |
| 2669 | * yet, trylock will always succeed without affecting |
| 2670 | * lockdep checks. |
| 2671 | */ |
| 2672 | WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex)); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2673 | } else if (S_ISREG(mode)) { |
| 2674 | inode->i_size = 0; |
| 2675 | inode->i_fop = &cgroup_file_operations; |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2676 | inode->i_op = &cgroup_file_inode_operations; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2677 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2678 | d_instantiate(dentry, inode); |
| 2679 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2683 | /** |
| 2684 | * cgroup_file_mode - deduce file mode of a control file |
| 2685 | * @cft: the control file in question |
| 2686 | * |
| 2687 | * returns cft->mode if ->mode is not 0 |
| 2688 | * returns S_IRUGO|S_IWUSR if it has both a read and a write handler |
| 2689 | * returns S_IRUGO if it has only a read handler |
| 2690 | * returns S_IWUSR if it has only a write hander |
| 2691 | */ |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2692 | static umode_t cgroup_file_mode(const struct cftype *cft) |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2693 | { |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2694 | umode_t mode = 0; |
Li Zefan | 099fca3 | 2009-04-02 16:57:29 -0700 | [diff] [blame] | 2695 | |
| 2696 | if (cft->mode) |
| 2697 | return cft->mode; |
| 2698 | |
| 2699 | if (cft->read || cft->read_u64 || cft->read_s64 || |
| 2700 | cft->read_map || cft->read_seq_string) |
| 2701 | mode |= S_IRUGO; |
| 2702 | |
| 2703 | if (cft->write || cft->write_u64 || cft->write_s64 || |
| 2704 | cft->write_string || cft->trigger) |
| 2705 | mode |= S_IWUSR; |
| 2706 | |
| 2707 | return mode; |
| 2708 | } |
| 2709 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2710 | static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2711 | struct cftype *cft) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2712 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2713 | struct dentry *dir = cgrp->dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2714 | struct cgroup *parent = __d_cgrp(dir); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2715 | struct dentry *dentry; |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2716 | struct cfent *cfe; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2717 | int error; |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 2718 | umode_t mode; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2719 | char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 }; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2720 | |
Tejun Heo | 9343862 | 2013-04-14 20:15:25 -0700 | [diff] [blame] | 2721 | if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2722 | strcpy(name, subsys->name); |
| 2723 | strcat(name, "."); |
| 2724 | } |
| 2725 | strcat(name, cft->name); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2726 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2727 | BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2728 | |
| 2729 | cfe = kzalloc(sizeof(*cfe), GFP_KERNEL); |
| 2730 | if (!cfe) |
| 2731 | return -ENOMEM; |
| 2732 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2733 | dentry = lookup_one_len(name, dir, strlen(name)); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2734 | if (IS_ERR(dentry)) { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2735 | error = PTR_ERR(dentry); |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2736 | goto out; |
| 2737 | } |
| 2738 | |
Li Zefan | d6cbf35 | 2013-05-14 19:44:20 +0800 | [diff] [blame] | 2739 | cfe->type = (void *)cft; |
| 2740 | cfe->dentry = dentry; |
| 2741 | dentry->d_fsdata = cfe; |
| 2742 | simple_xattrs_init(&cfe->xattrs); |
| 2743 | |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2744 | mode = cgroup_file_mode(cft); |
| 2745 | error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb); |
| 2746 | if (!error) { |
Tejun Heo | 05ef1d7 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2747 | list_add_tail(&cfe->node, &parent->files); |
| 2748 | cfe = NULL; |
| 2749 | } |
| 2750 | dput(dentry); |
| 2751 | out: |
| 2752 | kfree(cfe); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2753 | return error; |
| 2754 | } |
| 2755 | |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2756 | static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2757 | struct cftype cfts[], bool is_add) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2758 | { |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2759 | struct cftype *cft; |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2760 | int err, ret = 0; |
| 2761 | |
| 2762 | for (cft = cfts; cft->name[0] != '\0'; cft++) { |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2763 | /* does cft->flags tell us to skip this file on @cgrp? */ |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 2764 | if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp)) |
| 2765 | continue; |
Gao feng | f33fddc | 2012-12-06 14:38:57 +0800 | [diff] [blame] | 2766 | if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent) |
| 2767 | continue; |
| 2768 | if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent) |
| 2769 | continue; |
| 2770 | |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2771 | if (is_add) { |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2772 | err = cgroup_add_file(cgrp, subsys, cft); |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2773 | if (err) |
| 2774 | pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n", |
| 2775 | cft->name, err); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2776 | ret = err; |
Li Zefan | 2739d3c | 2013-01-21 18:18:33 +0800 | [diff] [blame] | 2777 | } else { |
| 2778 | cgroup_rm_file(cgrp, cft); |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2779 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2780 | } |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2781 | return ret; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2784 | static void cgroup_cfts_prepare(void) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2785 | __acquires(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2786 | { |
| 2787 | /* |
| 2788 | * Thanks to the entanglement with vfs inode locking, we can't walk |
| 2789 | * the existing cgroups under cgroup_mutex and create files. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2790 | * Instead, we use cgroup_for_each_descendant_pre() and drop RCU |
| 2791 | * read lock before calling cgroup_addrm_files(). |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2792 | */ |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2793 | mutex_lock(&cgroup_mutex); |
| 2794 | } |
| 2795 | |
| 2796 | static void cgroup_cfts_commit(struct cgroup_subsys *ss, |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2797 | struct cftype *cfts, bool is_add) |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2798 | __releases(&cgroup_mutex) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2799 | { |
| 2800 | LIST_HEAD(pending); |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2801 | struct cgroup *cgrp, *root = &ss->root->top_cgroup; |
Li Zefan | 084457f | 2013-06-18 18:40:19 +0800 | [diff] [blame] | 2802 | struct super_block *sb = ss->root->sb; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2803 | struct dentry *prev = NULL; |
| 2804 | struct inode *inode; |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2805 | u64 update_before; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2806 | |
| 2807 | /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 2808 | if (!cfts || ss->root == &cgroup_dummy_root || |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2809 | !atomic_inc_not_zero(&sb->s_active)) { |
| 2810 | mutex_unlock(&cgroup_mutex); |
| 2811 | return; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2814 | /* |
| 2815 | * All cgroups which are created after we drop cgroup_mutex will |
| 2816 | * have the updated set of files, so we only need to update the |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2817 | * cgroups created before the current @cgroup_serial_nr_next. |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2818 | */ |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2819 | update_before = cgroup_serial_nr_next; |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2820 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2821 | mutex_unlock(&cgroup_mutex); |
| 2822 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2823 | /* @root always needs to be updated */ |
| 2824 | inode = root->dentry->d_inode; |
| 2825 | mutex_lock(&inode->i_mutex); |
| 2826 | mutex_lock(&cgroup_mutex); |
| 2827 | cgroup_addrm_files(root, ss, cfts, is_add); |
| 2828 | mutex_unlock(&cgroup_mutex); |
| 2829 | mutex_unlock(&inode->i_mutex); |
| 2830 | |
| 2831 | /* add/rm files for all cgroups created before */ |
| 2832 | rcu_read_lock(); |
| 2833 | cgroup_for_each_descendant_pre(cgrp, root) { |
| 2834 | if (cgroup_is_dead(cgrp)) |
| 2835 | continue; |
| 2836 | |
| 2837 | inode = cgrp->dentry->d_inode; |
| 2838 | dget(cgrp->dentry); |
| 2839 | rcu_read_unlock(); |
| 2840 | |
| 2841 | dput(prev); |
| 2842 | prev = cgrp->dentry; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2843 | |
| 2844 | mutex_lock(&inode->i_mutex); |
| 2845 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 2846 | if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp)) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2847 | cgroup_addrm_files(cgrp, ss, cfts, is_add); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2848 | mutex_unlock(&cgroup_mutex); |
| 2849 | mutex_unlock(&inode->i_mutex); |
| 2850 | |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2851 | rcu_read_lock(); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2852 | } |
Li Zefan | e8c82d2 | 2013-06-18 18:48:37 +0800 | [diff] [blame] | 2853 | rcu_read_unlock(); |
| 2854 | dput(prev); |
| 2855 | deactivate_super(sb); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /** |
| 2859 | * cgroup_add_cftypes - add an array of cftypes to a subsystem |
| 2860 | * @ss: target cgroup subsystem |
| 2861 | * @cfts: zero-length name terminated array of cftypes |
| 2862 | * |
| 2863 | * Register @cfts to @ss. Files described by @cfts are created for all |
| 2864 | * existing cgroups to which @ss is attached and all future cgroups will |
| 2865 | * have them too. This function can be called anytime whether @ss is |
| 2866 | * attached or not. |
| 2867 | * |
| 2868 | * Returns 0 on successful registration, -errno on failure. Note that this |
| 2869 | * function currently returns 0 as long as @cfts registration is successful |
| 2870 | * even if some file creation attempts on existing cgroups fail. |
| 2871 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2872 | int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2873 | { |
| 2874 | struct cftype_set *set; |
| 2875 | |
| 2876 | set = kzalloc(sizeof(*set), GFP_KERNEL); |
| 2877 | if (!set) |
| 2878 | return -ENOMEM; |
| 2879 | |
| 2880 | cgroup_cfts_prepare(); |
| 2881 | set->cfts = cfts; |
| 2882 | list_add_tail(&set->node, &ss->cftsets); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2883 | cgroup_cfts_commit(ss, cfts, true); |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 2884 | |
| 2885 | return 0; |
| 2886 | } |
| 2887 | EXPORT_SYMBOL_GPL(cgroup_add_cftypes); |
| 2888 | |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2889 | /** |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2890 | * cgroup_rm_cftypes - remove an array of cftypes from a subsystem |
| 2891 | * @ss: target cgroup subsystem |
| 2892 | * @cfts: zero-length name terminated array of cftypes |
| 2893 | * |
| 2894 | * Unregister @cfts from @ss. Files described by @cfts are removed from |
| 2895 | * all existing cgroups to which @ss is attached and all future cgroups |
| 2896 | * won't have them either. This function can be called anytime whether @ss |
| 2897 | * is attached or not. |
| 2898 | * |
| 2899 | * Returns 0 on successful unregistration, -ENOENT if @cfts is not |
| 2900 | * registered with @ss. |
| 2901 | */ |
Aristeu Rozanski | 03b1cde | 2012-08-23 16:53:30 -0400 | [diff] [blame] | 2902 | int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2903 | { |
| 2904 | struct cftype_set *set; |
| 2905 | |
| 2906 | cgroup_cfts_prepare(); |
| 2907 | |
| 2908 | list_for_each_entry(set, &ss->cftsets, node) { |
| 2909 | if (set->cfts == cfts) { |
Li Zefan | f57947d | 2013-06-18 18:41:53 +0800 | [diff] [blame] | 2910 | list_del(&set->node); |
| 2911 | kfree(set); |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 2912 | cgroup_cfts_commit(ss, cfts, false); |
| 2913 | return 0; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | cgroup_cfts_commit(ss, NULL, false); |
| 2918 | return -ENOENT; |
| 2919 | } |
| 2920 | |
| 2921 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 2922 | * cgroup_task_count - count the number of tasks in a cgroup. |
| 2923 | * @cgrp: the cgroup in question |
| 2924 | * |
| 2925 | * Return the number of tasks in the cgroup. |
| 2926 | */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 2927 | int cgroup_task_count(const struct cgroup *cgrp) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2928 | { |
| 2929 | int count = 0; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2930 | struct cgrp_cset_link *link; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2931 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2932 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2933 | list_for_each_entry(link, &cgrp->cset_links, cset_link) |
| 2934 | count += atomic_read(&link->cset->refcount); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2935 | read_unlock(&css_set_lock); |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 2936 | return count; |
| 2937 | } |
| 2938 | |
| 2939 | /* |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2940 | * Advance a list_head iterator. The iterator should be positioned at |
| 2941 | * the start of a css_set |
| 2942 | */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2943 | static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2944 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2945 | struct list_head *l = it->cset_link; |
| 2946 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2947 | struct css_set *cset; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2948 | |
| 2949 | /* Advance to the next non-empty css_set */ |
| 2950 | do { |
| 2951 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2952 | if (l == &cgrp->cset_links) { |
| 2953 | it->cset_link = NULL; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2954 | return; |
| 2955 | } |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2956 | link = list_entry(l, struct cgrp_cset_link, cset_link); |
| 2957 | cset = link->cset; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2958 | } while (list_empty(&cset->tasks)); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 2959 | it->cset_link = l; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 2960 | it->task = cset->tasks.next; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2963 | /* |
| 2964 | * To reduce the fork() overhead for systems that are not actually |
| 2965 | * using their cgroups capability, we don't maintain the lists running |
| 2966 | * through each css_set to its tasks until we see the list actually |
| 2967 | * used - in other words after the first call to cgroup_iter_start(). |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2968 | */ |
Adrian Bunk | 3df91fe | 2008-04-29 00:59:54 -0700 | [diff] [blame] | 2969 | static void cgroup_enable_task_cg_lists(void) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2970 | { |
| 2971 | struct task_struct *p, *g; |
| 2972 | write_lock(&css_set_lock); |
| 2973 | use_task_css_set_links = 1; |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2974 | /* |
| 2975 | * We need tasklist_lock because RCU is not safe against |
| 2976 | * while_each_thread(). Besides, a forking task that has passed |
| 2977 | * cgroup_post_fork() without seeing use_task_css_set_links = 1 |
| 2978 | * is not guaranteed to have its child immediately visible in the |
| 2979 | * tasklist if we walk through it with RCU. |
| 2980 | */ |
| 2981 | read_lock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2982 | do_each_thread(g, p) { |
| 2983 | task_lock(p); |
Li Zefan | 0e04388 | 2008-04-17 11:37:15 +0800 | [diff] [blame] | 2984 | /* |
| 2985 | * We should check if the process is exiting, otherwise |
| 2986 | * it will race with cgroup_exit() in that the list |
| 2987 | * entry won't be deleted though the process has exited. |
| 2988 | */ |
| 2989 | if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list)) |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2990 | list_add(&p->cg_list, &p->cgroups->tasks); |
| 2991 | task_unlock(p); |
| 2992 | } while_each_thread(g, p); |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 2993 | read_unlock(&tasklist_lock); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 2994 | write_unlock(&css_set_lock); |
| 2995 | } |
| 2996 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 2997 | /** |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 2998 | * cgroup_next_sibling - find the next sibling of a given cgroup |
| 2999 | * @pos: the current cgroup |
| 3000 | * |
| 3001 | * This function returns the next sibling of @pos and should be called |
| 3002 | * under RCU read lock. The only requirement is that @pos is accessible. |
| 3003 | * The next sibling is guaranteed to be returned regardless of @pos's |
| 3004 | * state. |
| 3005 | */ |
| 3006 | struct cgroup *cgroup_next_sibling(struct cgroup *pos) |
| 3007 | { |
| 3008 | struct cgroup *next; |
| 3009 | |
| 3010 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3011 | |
| 3012 | /* |
| 3013 | * @pos could already have been removed. Once a cgroup is removed, |
| 3014 | * its ->sibling.next is no longer updated when its next sibling |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 3015 | * changes. As CGRP_DEAD assertion is serialized and happens |
| 3016 | * before the cgroup is taken off the ->sibling list, if we see it |
| 3017 | * unasserted, it's guaranteed that the next sibling hasn't |
| 3018 | * finished its grace period even if it's already removed, and thus |
| 3019 | * safe to dereference from this RCU critical section. If |
| 3020 | * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed |
| 3021 | * to be visible as %true here. |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3022 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 3023 | if (likely(!cgroup_is_dead(pos))) { |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3024 | next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling); |
| 3025 | if (&next->sibling != &pos->parent->children) |
| 3026 | return next; |
| 3027 | return NULL; |
| 3028 | } |
| 3029 | |
| 3030 | /* |
| 3031 | * Can't dereference the next pointer. Each cgroup is given a |
| 3032 | * monotonically increasing unique serial number and always |
| 3033 | * appended to the sibling list, so the next one can be found by |
| 3034 | * walking the parent's children until we see a cgroup with higher |
| 3035 | * serial number than @pos's. |
| 3036 | * |
| 3037 | * While this path can be slow, it's taken only when either the |
| 3038 | * current cgroup is removed or iteration and removal race. |
| 3039 | */ |
| 3040 | list_for_each_entry_rcu(next, &pos->parent->children, sibling) |
| 3041 | if (next->serial_nr > pos->serial_nr) |
| 3042 | return next; |
| 3043 | return NULL; |
| 3044 | } |
| 3045 | EXPORT_SYMBOL_GPL(cgroup_next_sibling); |
| 3046 | |
| 3047 | /** |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3048 | * cgroup_next_descendant_pre - find the next descendant for pre-order walk |
| 3049 | * @pos: the current position (%NULL to initiate traversal) |
| 3050 | * @cgroup: cgroup whose descendants to walk |
| 3051 | * |
| 3052 | * To be used by cgroup_for_each_descendant_pre(). Find the next |
| 3053 | * descendant to visit for pre-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3054 | * |
| 3055 | * While this function requires RCU read locking, it doesn't require the |
| 3056 | * whole traversal to be contained in a single RCU critical section. This |
| 3057 | * function will return the correct next descendant as long as both @pos |
| 3058 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3059 | */ |
| 3060 | struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos, |
| 3061 | struct cgroup *cgroup) |
| 3062 | { |
| 3063 | struct cgroup *next; |
| 3064 | |
| 3065 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3066 | |
| 3067 | /* if first iteration, pretend we just visited @cgroup */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3068 | if (!pos) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3069 | pos = cgroup; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3070 | |
| 3071 | /* visit the first child if exists */ |
| 3072 | next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling); |
| 3073 | if (next) |
| 3074 | return next; |
| 3075 | |
| 3076 | /* no child, visit my or the closest ancestor's next sibling */ |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3077 | while (pos != cgroup) { |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3078 | next = cgroup_next_sibling(pos); |
| 3079 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3080 | return next; |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3081 | pos = pos->parent; |
Tejun Heo | 7805d00 | 2013-05-24 10:50:24 +0900 | [diff] [blame] | 3082 | } |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3083 | |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre); |
| 3087 | |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3088 | /** |
| 3089 | * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup |
| 3090 | * @pos: cgroup of interest |
| 3091 | * |
| 3092 | * Return the rightmost descendant of @pos. If there's no descendant, |
| 3093 | * @pos is returned. This can be used during pre-order traversal to skip |
| 3094 | * subtree of @pos. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3095 | * |
| 3096 | * While this function requires RCU read locking, it doesn't require the |
| 3097 | * whole traversal to be contained in a single RCU critical section. This |
| 3098 | * function will return the correct rightmost descendant as long as @pos is |
| 3099 | * accessible. |
Tejun Heo | 12a9d2f | 2013-01-07 08:49:33 -0800 | [diff] [blame] | 3100 | */ |
| 3101 | struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos) |
| 3102 | { |
| 3103 | struct cgroup *last, *tmp; |
| 3104 | |
| 3105 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3106 | |
| 3107 | do { |
| 3108 | last = pos; |
| 3109 | /* ->prev isn't RCU safe, walk ->next till the end */ |
| 3110 | pos = NULL; |
| 3111 | list_for_each_entry_rcu(tmp, &last->children, sibling) |
| 3112 | pos = tmp; |
| 3113 | } while (pos); |
| 3114 | |
| 3115 | return last; |
| 3116 | } |
| 3117 | EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant); |
| 3118 | |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3119 | static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos) |
| 3120 | { |
| 3121 | struct cgroup *last; |
| 3122 | |
| 3123 | do { |
| 3124 | last = pos; |
| 3125 | pos = list_first_or_null_rcu(&pos->children, struct cgroup, |
| 3126 | sibling); |
| 3127 | } while (pos); |
| 3128 | |
| 3129 | return last; |
| 3130 | } |
| 3131 | |
| 3132 | /** |
| 3133 | * cgroup_next_descendant_post - find the next descendant for post-order walk |
| 3134 | * @pos: the current position (%NULL to initiate traversal) |
| 3135 | * @cgroup: cgroup whose descendants to walk |
| 3136 | * |
| 3137 | * To be used by cgroup_for_each_descendant_post(). Find the next |
| 3138 | * descendant to visit for post-order traversal of @cgroup's descendants. |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3139 | * |
| 3140 | * While this function requires RCU read locking, it doesn't require the |
| 3141 | * whole traversal to be contained in a single RCU critical section. This |
| 3142 | * function will return the correct next descendant as long as both @pos |
| 3143 | * and @cgroup are accessible and @pos is a descendant of @cgroup. |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3144 | */ |
| 3145 | struct cgroup *cgroup_next_descendant_post(struct cgroup *pos, |
| 3146 | struct cgroup *cgroup) |
| 3147 | { |
| 3148 | struct cgroup *next; |
| 3149 | |
| 3150 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 3151 | |
| 3152 | /* if first iteration, visit the leftmost descendant */ |
| 3153 | if (!pos) { |
| 3154 | next = cgroup_leftmost_descendant(cgroup); |
| 3155 | return next != cgroup ? next : NULL; |
| 3156 | } |
| 3157 | |
| 3158 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | 75501a6 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 3159 | next = cgroup_next_sibling(pos); |
| 3160 | if (next) |
Tejun Heo | 574bd9f | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 3161 | return cgroup_leftmost_descendant(next); |
| 3162 | |
| 3163 | /* no sibling left, visit parent */ |
| 3164 | next = pos->parent; |
| 3165 | return next != cgroup ? next : NULL; |
| 3166 | } |
| 3167 | EXPORT_SYMBOL_GPL(cgroup_next_descendant_post); |
| 3168 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3169 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3170 | __acquires(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3171 | { |
| 3172 | /* |
| 3173 | * The first time anyone tries to iterate across a cgroup, |
| 3174 | * we need to enable the list linking each css_set to its |
| 3175 | * tasks, and fix up all existing tasks. |
| 3176 | */ |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3177 | if (!use_task_css_set_links) |
| 3178 | cgroup_enable_task_cg_lists(); |
| 3179 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3180 | read_lock(&css_set_lock); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3181 | it->cset_link = &cgrp->cset_links; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3182 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3185 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3186 | struct cgroup_iter *it) |
| 3187 | { |
| 3188 | struct task_struct *res; |
| 3189 | struct list_head *l = it->task; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3190 | struct cgrp_cset_link *link; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3191 | |
| 3192 | /* If the iterator cg is NULL, we have no tasks */ |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3193 | if (!it->cset_link) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3194 | return NULL; |
| 3195 | res = list_entry(l, struct task_struct, cg_list); |
| 3196 | /* Advance iterator to find next entry */ |
| 3197 | l = l->next; |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 3198 | link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link); |
| 3199 | if (l == &link->cset->tasks) { |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3200 | /* We reached the end of this task list - move on to |
| 3201 | * the next cg_cgroup_link */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3202 | cgroup_advance_iter(cgrp, it); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3203 | } else { |
| 3204 | it->task = l; |
| 3205 | } |
| 3206 | return res; |
| 3207 | } |
| 3208 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3209 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it) |
Kirill A. Shutemov | c6ca575 | 2011-12-27 07:46:26 +0200 | [diff] [blame] | 3210 | __releases(css_set_lock) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3211 | { |
| 3212 | read_unlock(&css_set_lock); |
| 3213 | } |
| 3214 | |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3215 | static inline int started_after_time(struct task_struct *t1, |
| 3216 | struct timespec *time, |
| 3217 | struct task_struct *t2) |
| 3218 | { |
| 3219 | int start_diff = timespec_compare(&t1->start_time, time); |
| 3220 | if (start_diff > 0) { |
| 3221 | return 1; |
| 3222 | } else if (start_diff < 0) { |
| 3223 | return 0; |
| 3224 | } else { |
| 3225 | /* |
| 3226 | * Arbitrarily, if two processes started at the same |
| 3227 | * time, we'll say that the lower pointer value |
| 3228 | * started first. Note that t2 may have exited by now |
| 3229 | * so this may not be a valid pointer any longer, but |
| 3230 | * that's fine - it still serves to distinguish |
| 3231 | * between two tasks started (effectively) simultaneously. |
| 3232 | */ |
| 3233 | return t1 > t2; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | /* |
| 3238 | * This function is a callback from heap_insert() and is used to order |
| 3239 | * the heap. |
| 3240 | * In this case we order the heap in descending task start time. |
| 3241 | */ |
| 3242 | static inline int started_after(void *p1, void *p2) |
| 3243 | { |
| 3244 | struct task_struct *t1 = p1; |
| 3245 | struct task_struct *t2 = p2; |
| 3246 | return started_after_time(t1, &t2->start_time, t2); |
| 3247 | } |
| 3248 | |
| 3249 | /** |
| 3250 | * cgroup_scan_tasks - iterate though all the tasks in a cgroup |
| 3251 | * @scan: struct cgroup_scanner containing arguments for the scan |
| 3252 | * |
| 3253 | * Arguments include pointers to callback functions test_task() and |
| 3254 | * process_task(). |
| 3255 | * Iterate through all the tasks in a cgroup, calling test_task() for each, |
| 3256 | * and if it returns true, call process_task() for it also. |
| 3257 | * The test_task pointer may be NULL, meaning always true (select all tasks). |
| 3258 | * Effectively duplicates cgroup_iter_{start,next,end}() |
| 3259 | * but does not lock css_set_lock for the call to process_task(). |
| 3260 | * The struct cgroup_scanner may be embedded in any structure of the caller's |
| 3261 | * creation. |
| 3262 | * It is guaranteed that process_task() will act on every task that |
| 3263 | * is a member of the cgroup for the duration of this call. This |
| 3264 | * function may or may not call process_task() for tasks that exit |
| 3265 | * or move to a different cgroup during the call, or are forked or |
| 3266 | * move into the cgroup during the call. |
| 3267 | * |
| 3268 | * Note that test_task() may be called with locks held, and may in some |
| 3269 | * situations be called multiple times for the same task, so it should |
| 3270 | * be cheap. |
| 3271 | * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been |
| 3272 | * pre-allocated and will be used for heap operations (and its "gt" member will |
| 3273 | * be overwritten), else a temporary heap will be used (allocation of which |
| 3274 | * may cause this function to fail). |
| 3275 | */ |
| 3276 | int cgroup_scan_tasks(struct cgroup_scanner *scan) |
| 3277 | { |
| 3278 | int retval, i; |
| 3279 | struct cgroup_iter it; |
| 3280 | struct task_struct *p, *dropped; |
| 3281 | /* Never dereference latest_task, since it's not refcounted */ |
| 3282 | struct task_struct *latest_task = NULL; |
| 3283 | struct ptr_heap tmp_heap; |
| 3284 | struct ptr_heap *heap; |
| 3285 | struct timespec latest_time = { 0, 0 }; |
| 3286 | |
| 3287 | if (scan->heap) { |
| 3288 | /* The caller supplied our heap and pre-allocated its memory */ |
| 3289 | heap = scan->heap; |
| 3290 | heap->gt = &started_after; |
| 3291 | } else { |
| 3292 | /* We need to allocate our own heap memory */ |
| 3293 | heap = &tmp_heap; |
| 3294 | retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after); |
| 3295 | if (retval) |
| 3296 | /* cannot allocate the heap */ |
| 3297 | return retval; |
| 3298 | } |
| 3299 | |
| 3300 | again: |
| 3301 | /* |
| 3302 | * Scan tasks in the cgroup, using the scanner's "test_task" callback |
| 3303 | * to determine which are of interest, and using the scanner's |
| 3304 | * "process_task" callback to process any of them that need an update. |
| 3305 | * Since we don't want to hold any locks during the task updates, |
| 3306 | * gather tasks to be processed in a heap structure. |
| 3307 | * The heap is sorted by descending task start time. |
| 3308 | * If the statically-sized heap fills up, we overflow tasks that |
| 3309 | * started later, and in future iterations only consider tasks that |
| 3310 | * started after the latest task in the previous pass. This |
| 3311 | * guarantees forward progress and that we don't miss any tasks. |
| 3312 | */ |
| 3313 | heap->size = 0; |
| 3314 | cgroup_iter_start(scan->cg, &it); |
| 3315 | while ((p = cgroup_iter_next(scan->cg, &it))) { |
| 3316 | /* |
| 3317 | * Only affect tasks that qualify per the caller's callback, |
| 3318 | * if he provided one |
| 3319 | */ |
| 3320 | if (scan->test_task && !scan->test_task(p, scan)) |
| 3321 | continue; |
| 3322 | /* |
| 3323 | * Only process tasks that started after the last task |
| 3324 | * we processed |
| 3325 | */ |
| 3326 | if (!started_after_time(p, &latest_time, latest_task)) |
| 3327 | continue; |
| 3328 | dropped = heap_insert(heap, p); |
| 3329 | if (dropped == NULL) { |
| 3330 | /* |
| 3331 | * The new task was inserted; the heap wasn't |
| 3332 | * previously full |
| 3333 | */ |
| 3334 | get_task_struct(p); |
| 3335 | } else if (dropped != p) { |
| 3336 | /* |
| 3337 | * The new task was inserted, and pushed out a |
| 3338 | * different task |
| 3339 | */ |
| 3340 | get_task_struct(p); |
| 3341 | put_task_struct(dropped); |
| 3342 | } |
| 3343 | /* |
| 3344 | * Else the new task was newer than anything already in |
| 3345 | * the heap and wasn't inserted |
| 3346 | */ |
| 3347 | } |
| 3348 | cgroup_iter_end(scan->cg, &it); |
| 3349 | |
| 3350 | if (heap->size) { |
| 3351 | for (i = 0; i < heap->size; i++) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3352 | struct task_struct *q = heap->ptrs[i]; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3353 | if (i == 0) { |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3354 | latest_time = q->start_time; |
| 3355 | latest_task = q; |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3356 | } |
| 3357 | /* Process the task per the caller's callback */ |
Paul Jackson | 4fe91d5 | 2008-04-29 00:59:55 -0700 | [diff] [blame] | 3358 | scan->process_task(q, scan); |
| 3359 | put_task_struct(q); |
Cliff Wickman | 31a7df0 | 2008-02-07 00:14:42 -0800 | [diff] [blame] | 3360 | } |
| 3361 | /* |
| 3362 | * If we had to process any tasks at all, scan again |
| 3363 | * in case some of them were in the middle of forking |
| 3364 | * children that didn't get processed. |
| 3365 | * Not the most efficient way to do it, but it avoids |
| 3366 | * having to take callback_mutex in the fork path |
| 3367 | */ |
| 3368 | goto again; |
| 3369 | } |
| 3370 | if (heap == &tmp_heap) |
| 3371 | heap_free(&tmp_heap); |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3375 | static void cgroup_transfer_one_task(struct task_struct *task, |
| 3376 | struct cgroup_scanner *scan) |
| 3377 | { |
| 3378 | struct cgroup *new_cgroup = scan->data; |
| 3379 | |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3380 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3381 | cgroup_attach_task(new_cgroup, task, false); |
Tejun Heo | 47cfcd0 | 2013-04-07 09:29:51 -0700 | [diff] [blame] | 3382 | mutex_unlock(&cgroup_mutex); |
Tejun Heo | 8cc9934 | 2013-04-07 09:29:50 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | /** |
| 3386 | * cgroup_trasnsfer_tasks - move tasks from one cgroup to another |
| 3387 | * @to: cgroup to which the tasks will be moved |
| 3388 | * @from: cgroup in which the tasks currently reside |
| 3389 | */ |
| 3390 | int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from) |
| 3391 | { |
| 3392 | struct cgroup_scanner scan; |
| 3393 | |
| 3394 | scan.cg = from; |
| 3395 | scan.test_task = NULL; /* select all tasks in cgroup */ |
| 3396 | scan.process_task = cgroup_transfer_one_task; |
| 3397 | scan.heap = NULL; |
| 3398 | scan.data = to; |
| 3399 | |
| 3400 | return cgroup_scan_tasks(&scan); |
| 3401 | } |
| 3402 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3403 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3404 | * Stuff for reading the 'tasks'/'procs' files. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3405 | * |
| 3406 | * Reading this file can return large amounts of data if a cgroup has |
| 3407 | * *lots* of attached tasks. So it may need several calls to read(), |
| 3408 | * but we cannot guarantee that the information we produce is correct |
| 3409 | * unless we produce it entirely atomically. |
| 3410 | * |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3411 | */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3412 | |
Li Zefan | 2452825 | 2012-01-20 11:58:43 +0800 | [diff] [blame] | 3413 | /* which pidlist file are we talking about? */ |
| 3414 | enum cgroup_filetype { |
| 3415 | CGROUP_FILE_PROCS, |
| 3416 | CGROUP_FILE_TASKS, |
| 3417 | }; |
| 3418 | |
| 3419 | /* |
| 3420 | * A pidlist is a list of pids that virtually represents the contents of one |
| 3421 | * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists, |
| 3422 | * a pair (one each for procs, tasks) for each pid namespace that's relevant |
| 3423 | * to the cgroup. |
| 3424 | */ |
| 3425 | struct cgroup_pidlist { |
| 3426 | /* |
| 3427 | * used to find which pidlist is wanted. doesn't change as long as |
| 3428 | * this particular list stays in the list. |
| 3429 | */ |
| 3430 | struct { enum cgroup_filetype type; struct pid_namespace *ns; } key; |
| 3431 | /* array of xids */ |
| 3432 | pid_t *list; |
| 3433 | /* how many elements the above list has */ |
| 3434 | int length; |
| 3435 | /* how many files are using the current array */ |
| 3436 | int use_count; |
| 3437 | /* each of these stored in a list by its cgroup */ |
| 3438 | struct list_head links; |
| 3439 | /* pointer to the cgroup we belong to, for list removal purposes */ |
| 3440 | struct cgroup *owner; |
| 3441 | /* protects the other fields */ |
| 3442 | struct rw_semaphore mutex; |
| 3443 | }; |
| 3444 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3445 | /* |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3446 | * The following two functions "fix" the issue where there are more pids |
| 3447 | * than kmalloc will give memory for; in such cases, we use vmalloc/vfree. |
| 3448 | * TODO: replace with a kernel-wide solution to this problem |
| 3449 | */ |
| 3450 | #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2)) |
| 3451 | static void *pidlist_allocate(int count) |
| 3452 | { |
| 3453 | if (PIDLIST_TOO_LARGE(count)) |
| 3454 | return vmalloc(count * sizeof(pid_t)); |
| 3455 | else |
| 3456 | return kmalloc(count * sizeof(pid_t), GFP_KERNEL); |
| 3457 | } |
| 3458 | static void pidlist_free(void *p) |
| 3459 | { |
| 3460 | if (is_vmalloc_addr(p)) |
| 3461 | vfree(p); |
| 3462 | else |
| 3463 | kfree(p); |
| 3464 | } |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3465 | |
| 3466 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3467 | * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3468 | * Returns the number of unique elements. |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3469 | */ |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3470 | static int pidlist_uniq(pid_t *list, int length) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3471 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3472 | int src, dest = 1; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3473 | |
| 3474 | /* |
| 3475 | * we presume the 0th element is unique, so i starts at 1. trivial |
| 3476 | * edge cases first; no work needs to be done for either |
| 3477 | */ |
| 3478 | if (length == 0 || length == 1) |
| 3479 | return length; |
| 3480 | /* src and dest walk down the list; dest counts unique elements */ |
| 3481 | for (src = 1; src < length; src++) { |
| 3482 | /* find next unique element */ |
| 3483 | while (list[src] == list[src-1]) { |
| 3484 | src++; |
| 3485 | if (src == length) |
| 3486 | goto after; |
| 3487 | } |
| 3488 | /* dest always points to where the next unique element goes */ |
| 3489 | list[dest] = list[src]; |
| 3490 | dest++; |
| 3491 | } |
| 3492 | after: |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3493 | return dest; |
| 3494 | } |
| 3495 | |
| 3496 | static int cmppid(const void *a, const void *b) |
| 3497 | { |
| 3498 | return *(pid_t *)a - *(pid_t *)b; |
| 3499 | } |
| 3500 | |
| 3501 | /* |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3502 | * find the appropriate pidlist for our purpose (given procs vs tasks) |
| 3503 | * returns with the lock on that pidlist already held, and takes care |
| 3504 | * of the use count, or returns NULL with no locks held if we're out of |
| 3505 | * memory. |
| 3506 | */ |
| 3507 | static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp, |
| 3508 | enum cgroup_filetype type) |
| 3509 | { |
| 3510 | struct cgroup_pidlist *l; |
| 3511 | /* don't need task_nsproxy() if we're looking at ourself */ |
Eric W. Biederman | 17cf22c | 2010-03-02 14:51:53 -0800 | [diff] [blame] | 3512 | struct pid_namespace *ns = task_active_pid_ns(current); |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3513 | |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3514 | /* |
| 3515 | * We can't drop the pidlist_mutex before taking the l->mutex in case |
| 3516 | * the last ref-holder is trying to remove l from the list at the same |
| 3517 | * time. Holding the pidlist_mutex precludes somebody taking whichever |
| 3518 | * list we find out from under us - compare release_pid_array(). |
| 3519 | */ |
| 3520 | mutex_lock(&cgrp->pidlist_mutex); |
| 3521 | list_for_each_entry(l, &cgrp->pidlists, links) { |
| 3522 | if (l->key.type == type && l->key.ns == ns) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3523 | /* make sure l doesn't vanish out from under us */ |
| 3524 | down_write(&l->mutex); |
| 3525 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3526 | return l; |
| 3527 | } |
| 3528 | } |
| 3529 | /* entry not found; create a new one */ |
Tejun Heo | f4f4be2 | 2013-06-12 21:04:51 -0700 | [diff] [blame] | 3530 | l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3531 | if (!l) { |
| 3532 | mutex_unlock(&cgrp->pidlist_mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3533 | return l; |
| 3534 | } |
| 3535 | init_rwsem(&l->mutex); |
| 3536 | down_write(&l->mutex); |
| 3537 | l->key.type = type; |
Li Zefan | b70cc5f | 2010-03-10 15:22:12 -0800 | [diff] [blame] | 3538 | l->key.ns = get_pid_ns(ns); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3539 | l->owner = cgrp; |
| 3540 | list_add(&l->links, &cgrp->pidlists); |
| 3541 | mutex_unlock(&cgrp->pidlist_mutex); |
| 3542 | return l; |
| 3543 | } |
| 3544 | |
| 3545 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3546 | * Load a cgroup's pidarray with either procs' tgids or tasks' pids |
| 3547 | */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3548 | static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type, |
| 3549 | struct cgroup_pidlist **lp) |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3550 | { |
| 3551 | pid_t *array; |
| 3552 | int length; |
| 3553 | int pid, n = 0; /* used for populating the array */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3554 | struct cgroup_iter it; |
| 3555 | struct task_struct *tsk; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3556 | struct cgroup_pidlist *l; |
| 3557 | |
| 3558 | /* |
| 3559 | * If cgroup gets more users after we read count, we won't have |
| 3560 | * enough space - tough. This race is indistinguishable to the |
| 3561 | * caller from the case that the additional cgroup users didn't |
| 3562 | * show up until sometime later on. |
| 3563 | */ |
| 3564 | length = cgroup_task_count(cgrp); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3565 | array = pidlist_allocate(length); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3566 | if (!array) |
| 3567 | return -ENOMEM; |
| 3568 | /* now, populate the array */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3569 | cgroup_iter_start(cgrp, &it); |
| 3570 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3571 | if (unlikely(n == length)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3572 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3573 | /* get tgid or pid for procs or tasks file respectively */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3574 | if (type == CGROUP_FILE_PROCS) |
| 3575 | pid = task_tgid_vnr(tsk); |
| 3576 | else |
| 3577 | pid = task_pid_vnr(tsk); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3578 | if (pid > 0) /* make sure to only use valid results */ |
| 3579 | array[n++] = pid; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 3580 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3581 | cgroup_iter_end(cgrp, &it); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3582 | length = n; |
| 3583 | /* now sort & (if procs) strip out duplicates */ |
| 3584 | sort(array, length, sizeof(pid_t), cmppid, NULL); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3585 | if (type == CGROUP_FILE_PROCS) |
Li Zefan | 6ee211a | 2013-03-12 15:36:00 -0700 | [diff] [blame] | 3586 | length = pidlist_uniq(array, length); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3587 | l = cgroup_pidlist_find(cgrp, type); |
| 3588 | if (!l) { |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3589 | pidlist_free(array); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3590 | return -ENOMEM; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3591 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3592 | /* store array, freeing old if necessary - lock already held */ |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3593 | pidlist_free(l->list); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3594 | l->list = array; |
| 3595 | l->length = length; |
| 3596 | l->use_count++; |
| 3597 | up_write(&l->mutex); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3598 | *lp = l; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3599 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3600 | } |
| 3601 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3602 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3603 | * cgroupstats_build - build and fill cgroupstats |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3604 | * @stats: cgroupstats to fill information into |
| 3605 | * @dentry: A dentry entry belonging to the cgroup for which stats have |
| 3606 | * been requested. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 3607 | * |
| 3608 | * Build and fill cgroupstats so that taskstats can export it to user |
| 3609 | * space. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3610 | */ |
| 3611 | int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry) |
| 3612 | { |
| 3613 | int ret = -EINVAL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3614 | struct cgroup *cgrp; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3615 | struct cgroup_iter it; |
| 3616 | struct task_struct *tsk; |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3617 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3618 | /* |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3619 | * Validate dentry by checking the superblock operations, |
| 3620 | * and make sure it's a directory. |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3621 | */ |
Li Zefan | 33d283b | 2008-11-19 15:36:48 -0800 | [diff] [blame] | 3622 | if (dentry->d_sb->s_op != &cgroup_ops || |
| 3623 | !S_ISDIR(dentry->d_inode->i_mode)) |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3624 | goto err; |
| 3625 | |
| 3626 | ret = 0; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3627 | cgrp = dentry->d_fsdata; |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3628 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3629 | cgroup_iter_start(cgrp, &it); |
| 3630 | while ((tsk = cgroup_iter_next(cgrp, &it))) { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3631 | switch (tsk->state) { |
| 3632 | case TASK_RUNNING: |
| 3633 | stats->nr_running++; |
| 3634 | break; |
| 3635 | case TASK_INTERRUPTIBLE: |
| 3636 | stats->nr_sleeping++; |
| 3637 | break; |
| 3638 | case TASK_UNINTERRUPTIBLE: |
| 3639 | stats->nr_uninterruptible++; |
| 3640 | break; |
| 3641 | case TASK_STOPPED: |
| 3642 | stats->nr_stopped++; |
| 3643 | break; |
| 3644 | default: |
| 3645 | if (delayacct_is_task_waiting_on_io(tsk)) |
| 3646 | stats->nr_io_wait++; |
| 3647 | break; |
| 3648 | } |
| 3649 | } |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3650 | cgroup_iter_end(cgrp, &it); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3651 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 3652 | err: |
| 3653 | return ret; |
| 3654 | } |
| 3655 | |
Paul Menage | 8f3ff20 | 2009-09-23 15:56:25 -0700 | [diff] [blame] | 3656 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3657 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3658 | * 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] | 3659 | * 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] | 3660 | * in the cgroup->l->list array. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3661 | */ |
| 3662 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3663 | static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3664 | { |
| 3665 | /* |
| 3666 | * Initially we receive a position value that corresponds to |
| 3667 | * one more than the last pid shown (or 0 on the first call or |
| 3668 | * after a seek to the start). Use a binary-search to find the |
| 3669 | * next pid to display, if any |
| 3670 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3671 | struct cgroup_pidlist *l = s->private; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3672 | int index = 0, pid = *pos; |
| 3673 | int *iter; |
| 3674 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3675 | down_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3676 | if (pid) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3677 | int end = l->length; |
Stephen Rothwell | 2077776 | 2008-10-21 16:11:20 +1100 | [diff] [blame] | 3678 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3679 | while (index < end) { |
| 3680 | int mid = (index + end) / 2; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3681 | if (l->list[mid] == pid) { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3682 | index = mid; |
| 3683 | break; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3684 | } else if (l->list[mid] <= pid) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3685 | index = mid + 1; |
| 3686 | else |
| 3687 | end = mid; |
| 3688 | } |
| 3689 | } |
| 3690 | /* If we're off the end of the array, we're done */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3691 | if (index >= l->length) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3692 | return NULL; |
| 3693 | /* Update the abstract position to be the actual pid that we found */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3694 | iter = l->list + index; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3695 | *pos = *iter; |
| 3696 | return iter; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3699 | static void cgroup_pidlist_stop(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3700 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3701 | struct cgroup_pidlist *l = s->private; |
| 3702 | up_read(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3705 | 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] | 3706 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3707 | struct cgroup_pidlist *l = s->private; |
| 3708 | pid_t *p = v; |
| 3709 | pid_t *end = l->list + l->length; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3710 | /* |
| 3711 | * Advance to the next pid in the array. If this goes off the |
| 3712 | * end, we're done |
| 3713 | */ |
| 3714 | p++; |
| 3715 | if (p >= end) { |
| 3716 | return NULL; |
| 3717 | } else { |
| 3718 | *pos = *p; |
| 3719 | return p; |
| 3720 | } |
| 3721 | } |
| 3722 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3723 | static int cgroup_pidlist_show(struct seq_file *s, void *v) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3724 | { |
| 3725 | return seq_printf(s, "%d\n", *(int *)v); |
| 3726 | } |
| 3727 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3728 | /* |
| 3729 | * seq_operations functions for iterating on pidlists through seq_file - |
| 3730 | * independent of whether it's tasks or procs |
| 3731 | */ |
| 3732 | static const struct seq_operations cgroup_pidlist_seq_operations = { |
| 3733 | .start = cgroup_pidlist_start, |
| 3734 | .stop = cgroup_pidlist_stop, |
| 3735 | .next = cgroup_pidlist_next, |
| 3736 | .show = cgroup_pidlist_show, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3737 | }; |
| 3738 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3739 | static void cgroup_release_pid_array(struct cgroup_pidlist *l) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3740 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3741 | /* |
| 3742 | * the case where we're the last user of this particular pidlist will |
| 3743 | * have us remove it from the cgroup's list, which entails taking the |
| 3744 | * mutex. since in pidlist_find the pidlist->lock depends on cgroup-> |
| 3745 | * pidlist_mutex, we have to take pidlist_mutex first. |
| 3746 | */ |
| 3747 | mutex_lock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3748 | down_write(&l->mutex); |
| 3749 | BUG_ON(!l->use_count); |
| 3750 | if (!--l->use_count) { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3751 | /* we're the last user if refcount is 0; remove and free */ |
| 3752 | list_del(&l->links); |
| 3753 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | d1d9fd3 | 2009-09-23 15:56:28 -0700 | [diff] [blame] | 3754 | pidlist_free(l->list); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3755 | put_pid_ns(l->key.ns); |
| 3756 | up_write(&l->mutex); |
| 3757 | kfree(l); |
| 3758 | return; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3759 | } |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3760 | mutex_unlock(&l->owner->pidlist_mutex); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3761 | up_write(&l->mutex); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3764 | static int cgroup_pidlist_release(struct inode *inode, struct file *file) |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3765 | { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3766 | struct cgroup_pidlist *l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3767 | if (!(file->f_mode & FMODE_READ)) |
| 3768 | return 0; |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3769 | /* |
| 3770 | * the seq_file will only be initialized if the file was opened for |
| 3771 | * reading; hence we check if it's not null only in that case. |
| 3772 | */ |
| 3773 | l = ((struct seq_file *)file->private_data)->private; |
| 3774 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3775 | return seq_release(inode, file); |
| 3776 | } |
| 3777 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3778 | static const struct file_operations cgroup_pidlist_operations = { |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3779 | .read = seq_read, |
| 3780 | .llseek = seq_lseek, |
| 3781 | .write = cgroup_file_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3782 | .release = cgroup_pidlist_release, |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3783 | }; |
| 3784 | |
| 3785 | /* |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3786 | * The following functions handle opens on a file that displays a pidlist |
| 3787 | * (tasks or procs). Prepare an array of the process/thread IDs of whoever's |
| 3788 | * in the cgroup. |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3789 | */ |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3790 | /* helper function for the two below it */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3791 | static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type) |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3792 | { |
| 3793 | struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent); |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3794 | struct cgroup_pidlist *l; |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3795 | int retval; |
| 3796 | |
| 3797 | /* Nothing to do for write-only files */ |
| 3798 | if (!(file->f_mode & FMODE_READ)) |
| 3799 | return 0; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3800 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3801 | /* have the array populated */ |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3802 | retval = pidlist_array_load(cgrp, type, &l); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3803 | if (retval) |
| 3804 | return retval; |
| 3805 | /* configure file information */ |
| 3806 | file->f_op = &cgroup_pidlist_operations; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3807 | |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3808 | retval = seq_open(file, &cgroup_pidlist_seq_operations); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3809 | if (retval) { |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3810 | cgroup_release_pid_array(l); |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 3811 | return retval; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3812 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3813 | ((struct seq_file *)file->private_data)->private = l; |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3814 | return 0; |
| 3815 | } |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3816 | static int cgroup_tasks_open(struct inode *unused, struct file *file) |
| 3817 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3818 | return cgroup_pidlist_open(file, CGROUP_FILE_TASKS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3819 | } |
| 3820 | static int cgroup_procs_open(struct inode *unused, struct file *file) |
| 3821 | { |
Ben Blum | 72a8cb3 | 2009-09-23 15:56:27 -0700 | [diff] [blame] | 3822 | return cgroup_pidlist_open(file, CGROUP_FILE_PROCS); |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 3823 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3824 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3825 | static u64 cgroup_read_notify_on_release(struct cgroup *cgrp, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3826 | struct cftype *cft) |
| 3827 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 3828 | return notify_on_release(cgrp); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
Paul Menage | 6379c10 | 2008-07-25 01:47:01 -0700 | [diff] [blame] | 3831 | static int cgroup_write_notify_on_release(struct cgroup *cgrp, |
| 3832 | struct cftype *cft, |
| 3833 | u64 val) |
| 3834 | { |
| 3835 | clear_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 3836 | if (val) |
| 3837 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3838 | else |
| 3839 | clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 3840 | return 0; |
| 3841 | } |
| 3842 | |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 3843 | /* |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3844 | * When dput() is called asynchronously, if umount has been done and |
| 3845 | * then deactivate_super() in cgroup_free_fn() kills the superblock, |
| 3846 | * there's a small window that vfs will see the root dentry with non-zero |
| 3847 | * refcnt and trigger BUG(). |
| 3848 | * |
| 3849 | * That's why we hold a reference before dput() and drop it right after. |
| 3850 | */ |
| 3851 | static void cgroup_dput(struct cgroup *cgrp) |
| 3852 | { |
| 3853 | struct super_block *sb = cgrp->root->sb; |
| 3854 | |
| 3855 | atomic_inc(&sb->s_active); |
| 3856 | dput(cgrp->dentry); |
| 3857 | deactivate_super(sb); |
| 3858 | } |
| 3859 | |
| 3860 | /* |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3861 | * Unregister event and free resources. |
| 3862 | * |
| 3863 | * Gets called from workqueue. |
| 3864 | */ |
| 3865 | static void cgroup_event_remove(struct work_struct *work) |
| 3866 | { |
| 3867 | struct cgroup_event *event = container_of(work, struct cgroup_event, |
| 3868 | remove); |
| 3869 | struct cgroup *cgrp = event->cgrp; |
| 3870 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3871 | remove_wait_queue(event->wqh, &event->wait); |
| 3872 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3873 | event->cft->unregister_event(cgrp, event->cft, event->eventfd); |
| 3874 | |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3875 | /* Notify userspace the event is going away. */ |
| 3876 | eventfd_signal(event->eventfd, 1); |
| 3877 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3878 | eventfd_ctx_put(event->eventfd); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3879 | kfree(event); |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 3880 | cgroup_dput(cgrp); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | /* |
| 3884 | * Gets called on POLLHUP on eventfd when user closes it. |
| 3885 | * |
| 3886 | * Called with wqh->lock held and interrupts disabled. |
| 3887 | */ |
| 3888 | static int cgroup_event_wake(wait_queue_t *wait, unsigned mode, |
| 3889 | int sync, void *key) |
| 3890 | { |
| 3891 | struct cgroup_event *event = container_of(wait, |
| 3892 | struct cgroup_event, wait); |
| 3893 | struct cgroup *cgrp = event->cgrp; |
| 3894 | unsigned long flags = (unsigned long)key; |
| 3895 | |
| 3896 | if (flags & POLLHUP) { |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3897 | /* |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3898 | * If the event has been detached at cgroup removal, we |
| 3899 | * can simply return knowing the other side will cleanup |
| 3900 | * for us. |
| 3901 | * |
| 3902 | * We can't race against event freeing since the other |
| 3903 | * side will require wqh->lock via remove_wait_queue(), |
| 3904 | * which we hold. |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3905 | */ |
Li Zefan | 810cbee | 2013-02-18 18:56:14 +0800 | [diff] [blame] | 3906 | spin_lock(&cgrp->event_list_lock); |
| 3907 | if (!list_empty(&event->list)) { |
| 3908 | list_del_init(&event->list); |
| 3909 | /* |
| 3910 | * We are in atomic context, but cgroup_event_remove() |
| 3911 | * may sleep, so we have to call it in workqueue. |
| 3912 | */ |
| 3913 | schedule_work(&event->remove); |
| 3914 | } |
| 3915 | spin_unlock(&cgrp->event_list_lock); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | return 0; |
| 3919 | } |
| 3920 | |
| 3921 | static void cgroup_event_ptable_queue_proc(struct file *file, |
| 3922 | wait_queue_head_t *wqh, poll_table *pt) |
| 3923 | { |
| 3924 | struct cgroup_event *event = container_of(pt, |
| 3925 | struct cgroup_event, pt); |
| 3926 | |
| 3927 | event->wqh = wqh; |
| 3928 | add_wait_queue(wqh, &event->wait); |
| 3929 | } |
| 3930 | |
| 3931 | /* |
| 3932 | * Parse input and register new cgroup event handler. |
| 3933 | * |
| 3934 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 3935 | * Interpretation of args is defined by control file implementation. |
| 3936 | */ |
| 3937 | static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft, |
| 3938 | const char *buffer) |
| 3939 | { |
| 3940 | struct cgroup_event *event = NULL; |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3941 | struct cgroup *cgrp_cfile; |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3942 | unsigned int efd, cfd; |
| 3943 | struct file *efile = NULL; |
| 3944 | struct file *cfile = NULL; |
| 3945 | char *endp; |
| 3946 | int ret; |
| 3947 | |
| 3948 | efd = simple_strtoul(buffer, &endp, 10); |
| 3949 | if (*endp != ' ') |
| 3950 | return -EINVAL; |
| 3951 | buffer = endp + 1; |
| 3952 | |
| 3953 | cfd = simple_strtoul(buffer, &endp, 10); |
| 3954 | if ((*endp != ' ') && (*endp != '\0')) |
| 3955 | return -EINVAL; |
| 3956 | buffer = endp + 1; |
| 3957 | |
| 3958 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 3959 | if (!event) |
| 3960 | return -ENOMEM; |
| 3961 | event->cgrp = cgrp; |
| 3962 | INIT_LIST_HEAD(&event->list); |
| 3963 | init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc); |
| 3964 | init_waitqueue_func_entry(&event->wait, cgroup_event_wake); |
| 3965 | INIT_WORK(&event->remove, cgroup_event_remove); |
| 3966 | |
| 3967 | efile = eventfd_fget(efd); |
| 3968 | if (IS_ERR(efile)) { |
| 3969 | ret = PTR_ERR(efile); |
| 3970 | goto fail; |
| 3971 | } |
| 3972 | |
| 3973 | event->eventfd = eventfd_ctx_fileget(efile); |
| 3974 | if (IS_ERR(event->eventfd)) { |
| 3975 | ret = PTR_ERR(event->eventfd); |
| 3976 | goto fail; |
| 3977 | } |
| 3978 | |
| 3979 | cfile = fget(cfd); |
| 3980 | if (!cfile) { |
| 3981 | ret = -EBADF; |
| 3982 | goto fail; |
| 3983 | } |
| 3984 | |
| 3985 | /* the process need read permission on control file */ |
Al Viro | 3bfa784 | 2011-06-19 12:55:10 -0400 | [diff] [blame] | 3986 | /* 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] | 3987 | ret = inode_permission(file_inode(cfile), MAY_READ); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 3988 | if (ret < 0) |
| 3989 | goto fail; |
| 3990 | |
| 3991 | event->cft = __file_cft(cfile); |
| 3992 | if (IS_ERR(event->cft)) { |
| 3993 | ret = PTR_ERR(event->cft); |
| 3994 | goto fail; |
| 3995 | } |
| 3996 | |
Li Zefan | f169007 | 2013-02-18 14:13:35 +0800 | [diff] [blame] | 3997 | /* |
| 3998 | * The file to be monitored must be in the same cgroup as |
| 3999 | * cgroup.event_control is. |
| 4000 | */ |
| 4001 | cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent); |
| 4002 | if (cgrp_cfile != cgrp) { |
| 4003 | ret = -EINVAL; |
| 4004 | goto fail; |
| 4005 | } |
| 4006 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4007 | if (!event->cft->register_event || !event->cft->unregister_event) { |
| 4008 | ret = -EINVAL; |
| 4009 | goto fail; |
| 4010 | } |
| 4011 | |
| 4012 | ret = event->cft->register_event(cgrp, event->cft, |
| 4013 | event->eventfd, buffer); |
| 4014 | if (ret) |
| 4015 | goto fail; |
| 4016 | |
Li Zefan | 7ef70e4 | 2013-04-26 11:58:03 -0700 | [diff] [blame] | 4017 | efile->f_op->poll(efile, &event->pt); |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4018 | |
Kirill A. Shutemov | a0a4db5 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4019 | /* |
| 4020 | * Events should be removed after rmdir of cgroup directory, but before |
| 4021 | * destroying subsystem state objects. Let's take reference to cgroup |
| 4022 | * directory dentry to do that. |
| 4023 | */ |
| 4024 | dget(cgrp->dentry); |
| 4025 | |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4026 | spin_lock(&cgrp->event_list_lock); |
| 4027 | list_add(&event->list, &cgrp->event_list); |
| 4028 | spin_unlock(&cgrp->event_list_lock); |
| 4029 | |
| 4030 | fput(cfile); |
| 4031 | fput(efile); |
| 4032 | |
| 4033 | return 0; |
| 4034 | |
| 4035 | fail: |
| 4036 | if (cfile) |
| 4037 | fput(cfile); |
| 4038 | |
| 4039 | if (event && event->eventfd && !IS_ERR(event->eventfd)) |
| 4040 | eventfd_ctx_put(event->eventfd); |
| 4041 | |
| 4042 | if (!IS_ERR_OR_NULL(efile)) |
| 4043 | fput(efile); |
| 4044 | |
| 4045 | kfree(event); |
| 4046 | |
| 4047 | return ret; |
| 4048 | } |
| 4049 | |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4050 | static u64 cgroup_clone_children_read(struct cgroup *cgrp, |
| 4051 | struct cftype *cft) |
| 4052 | { |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4053 | return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | static int cgroup_clone_children_write(struct cgroup *cgrp, |
| 4057 | struct cftype *cft, |
| 4058 | u64 val) |
| 4059 | { |
| 4060 | if (val) |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4061 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4062 | else |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4063 | clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4064 | return 0; |
| 4065 | } |
| 4066 | |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4067 | static struct cftype cgroup_base_files[] = { |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4068 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4069 | .name = "cgroup.procs", |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4070 | .open = cgroup_procs_open, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4071 | .write_u64 = cgroup_procs_write, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4072 | .release = cgroup_pidlist_release, |
Ben Blum | 74a1166 | 2011-05-26 16:25:20 -0700 | [diff] [blame] | 4073 | .mode = S_IRUGO | S_IWUSR, |
Ben Blum | 102a775 | 2009-09-23 15:56:26 -0700 | [diff] [blame] | 4074 | }, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4075 | { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4076 | .name = "cgroup.event_control", |
Kirill A. Shutemov | 0dea116 | 2010-03-10 15:22:20 -0800 | [diff] [blame] | 4077 | .write_string = cgroup_write_event_control, |
| 4078 | .mode = S_IWUGO, |
| 4079 | }, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4080 | { |
| 4081 | .name = "cgroup.clone_children", |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4082 | .flags = CFTYPE_INSANE, |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4083 | .read_u64 = cgroup_clone_children_read, |
| 4084 | .write_u64 = cgroup_clone_children_write, |
| 4085 | }, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4086 | { |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4087 | .name = "cgroup.sane_behavior", |
| 4088 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 4089 | .read_seq_string = cgroup_sane_behavior_show, |
| 4090 | }, |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4091 | |
| 4092 | /* |
| 4093 | * Historical crazy stuff. These don't have "cgroup." prefix and |
| 4094 | * don't exist if sane_behavior. If you're depending on these, be |
| 4095 | * prepared to be burned. |
| 4096 | */ |
| 4097 | { |
| 4098 | .name = "tasks", |
| 4099 | .flags = CFTYPE_INSANE, /* use "procs" instead */ |
| 4100 | .open = cgroup_tasks_open, |
| 4101 | .write_u64 = cgroup_tasks_write, |
| 4102 | .release = cgroup_pidlist_release, |
| 4103 | .mode = S_IRUGO | S_IWUSR, |
| 4104 | }, |
| 4105 | { |
| 4106 | .name = "notify_on_release", |
| 4107 | .flags = CFTYPE_INSANE, |
| 4108 | .read_u64 = cgroup_read_notify_on_release, |
| 4109 | .write_u64 = cgroup_write_notify_on_release, |
| 4110 | }, |
Tejun Heo | 873fe09 | 2013-04-14 20:15:26 -0700 | [diff] [blame] | 4111 | { |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4112 | .name = "release_agent", |
Tejun Heo | cc5943a | 2013-06-03 19:13:55 -0700 | [diff] [blame] | 4113 | .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT, |
Tejun Heo | 6e6ff25 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4114 | .read_seq_string = cgroup_release_agent_show, |
| 4115 | .write_string = cgroup_release_agent_write, |
| 4116 | .max_write_len = PATH_MAX, |
| 4117 | }, |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4118 | { } /* terminate */ |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4119 | }; |
| 4120 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4121 | /** |
| 4122 | * cgroup_populate_dir - selectively creation of files in a directory |
| 4123 | * @cgrp: target cgroup |
| 4124 | * @base_files: true if the base files should be added |
| 4125 | * @subsys_mask: mask of the subsystem ids whose files should be added |
| 4126 | */ |
| 4127 | static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files, |
| 4128 | unsigned long subsys_mask) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4129 | { |
| 4130 | int err; |
| 4131 | struct cgroup_subsys *ss; |
| 4132 | |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4133 | if (base_files) { |
Tejun Heo | d5c56ce | 2013-06-03 19:14:34 -0700 | [diff] [blame] | 4134 | err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true); |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4135 | if (err < 0) |
| 4136 | return err; |
| 4137 | } |
Paul Menage | bbcb81d | 2007-10-18 23:39:32 -0700 | [diff] [blame] | 4138 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4139 | /* process cftsets of each subsystem */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4140 | for_each_subsys(cgrp->root, ss) { |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4141 | struct cftype_set *set; |
Aristeu Rozanski | 13af07d | 2012-08-23 16:53:29 -0400 | [diff] [blame] | 4142 | if (!test_bit(ss->subsys_id, &subsys_mask)) |
| 4143 | continue; |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4144 | |
Tejun Heo | db0416b | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4145 | list_for_each_entry(set, &ss->cftsets, node) |
Tejun Heo | 7957862 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4146 | cgroup_addrm_files(cgrp, ss, set->cfts, true); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4147 | } |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4148 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4149 | /* This cgroup is ready now */ |
| 4150 | for_each_subsys(cgrp->root, ss) { |
| 4151 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4152 | /* |
| 4153 | * Update id->css pointer and make this css visible from |
| 4154 | * CSS ID functions. This pointer will be dereferened |
| 4155 | * from RCU-read-side without locks. |
| 4156 | */ |
| 4157 | if (css->id) |
| 4158 | rcu_assign_pointer(css->id->css, css); |
| 4159 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4160 | |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4164 | static void css_dput_fn(struct work_struct *work) |
| 4165 | { |
| 4166 | struct cgroup_subsys_state *css = |
| 4167 | container_of(work, struct cgroup_subsys_state, dput_work); |
| 4168 | |
Li Zefan | 1c8158e | 2013-06-18 18:41:10 +0800 | [diff] [blame] | 4169 | cgroup_dput(css->cgroup); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4172 | static void css_release(struct percpu_ref *ref) |
| 4173 | { |
| 4174 | struct cgroup_subsys_state *css = |
| 4175 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4176 | |
| 4177 | schedule_work(&css->dput_work); |
| 4178 | } |
| 4179 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4180 | static void init_cgroup_css(struct cgroup_subsys_state *css, |
| 4181 | struct cgroup_subsys *ss, |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4182 | struct cgroup *cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4183 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4184 | css->cgroup = cgrp; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4185 | css->flags = 0; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4186 | css->id = NULL; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4187 | if (cgrp == cgroup_dummy_top) |
Tejun Heo | 38b53ab | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4188 | css->flags |= CSS_ROOT; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4189 | BUG_ON(cgrp->subsys[ss->subsys_id]); |
| 4190 | cgrp->subsys[ss->subsys_id] = css; |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4191 | |
| 4192 | /* |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4193 | * css holds an extra ref to @cgrp->dentry which is put on the last |
| 4194 | * css_put(). dput() requires process context, which css_put() may |
| 4195 | * be called without. @css->dput_work will be used to invoke |
| 4196 | * dput() asynchronously from css_put(). |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4197 | */ |
| 4198 | INIT_WORK(&css->dput_work, css_dput_fn); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4199 | } |
| 4200 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4201 | /* invoke ->post_create() on a new CSS and mark it online if successful */ |
| 4202 | static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4203 | { |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4204 | int ret = 0; |
| 4205 | |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4206 | lockdep_assert_held(&cgroup_mutex); |
| 4207 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4208 | if (ss->css_online) |
| 4209 | ret = ss->css_online(cgrp); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4210 | if (!ret) |
| 4211 | cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE; |
| 4212 | return ret; |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */ |
| 4216 | static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp) |
| 4217 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
| 4218 | { |
| 4219 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4220 | |
| 4221 | lockdep_assert_held(&cgroup_mutex); |
| 4222 | |
| 4223 | if (!(css->flags & CSS_ONLINE)) |
| 4224 | return; |
| 4225 | |
Li Zefan | d7eeac1 | 2013-03-12 15:35:59 -0700 | [diff] [blame] | 4226 | if (ss->css_offline) |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4227 | ss->css_offline(cgrp); |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4228 | |
| 4229 | cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE; |
| 4230 | } |
| 4231 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4232 | /* |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4233 | * cgroup_create - create a cgroup |
| 4234 | * @parent: cgroup that will be parent of the new cgroup |
| 4235 | * @dentry: dentry of the new cgroup |
| 4236 | * @mode: mode to set on new inode |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4237 | * |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4238 | * Must be called with the mutex on the parent inode held |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4239 | */ |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4240 | static long cgroup_create(struct cgroup *parent, struct dentry *dentry, |
Al Viro | a5e7ed3 | 2011-07-26 01:55:55 -0400 | [diff] [blame] | 4241 | umode_t mode) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4242 | { |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4243 | struct cgroup *cgrp; |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4244 | struct cgroup_name *name; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4245 | struct cgroupfs_root *root = parent->root; |
| 4246 | int err = 0; |
| 4247 | struct cgroup_subsys *ss; |
| 4248 | struct super_block *sb = root->sb; |
| 4249 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4250 | /* allocate the cgroup and its ID, 0 is reserved for the root */ |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4251 | cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL); |
| 4252 | if (!cgrp) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4253 | return -ENOMEM; |
| 4254 | |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4255 | name = cgroup_alloc_name(dentry); |
| 4256 | if (!name) |
| 4257 | goto err_free_cgrp; |
| 4258 | rcu_assign_pointer(cgrp->name, name); |
| 4259 | |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4260 | cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL); |
| 4261 | if (cgrp->id < 0) |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4262 | goto err_free_name; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4263 | |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4264 | /* |
| 4265 | * Only live parents can have children. Note that the liveliness |
| 4266 | * check isn't strictly necessary because cgroup_mkdir() and |
| 4267 | * cgroup_rmdir() are fully synchronized by i_mutex; however, do it |
| 4268 | * anyway so that locking is contained inside cgroup proper and we |
| 4269 | * don't get nasty surprises if we ever grow another caller. |
| 4270 | */ |
| 4271 | if (!cgroup_lock_live_group(parent)) { |
| 4272 | err = -ENODEV; |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4273 | goto err_free_id; |
Tejun Heo | 976c06b | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4274 | } |
| 4275 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4276 | /* Grab a reference on the superblock so the hierarchy doesn't |
| 4277 | * get deleted on unmount if there are child cgroups. This |
| 4278 | * can be done outside cgroup_mutex, since the sb can't |
| 4279 | * disappear while someone has an open control file on the |
| 4280 | * fs */ |
| 4281 | atomic_inc(&sb->s_active); |
| 4282 | |
Paul Menage | cc31edc | 2008-10-18 20:28:04 -0700 | [diff] [blame] | 4283 | init_cgroup_housekeeping(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4284 | |
Li Zefan | fe1c06c | 2013-01-24 14:30:22 +0800 | [diff] [blame] | 4285 | dentry->d_fsdata = cgrp; |
| 4286 | cgrp->dentry = dentry; |
| 4287 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4288 | cgrp->parent = parent; |
| 4289 | cgrp->root = parent->root; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4290 | |
Li Zefan | b6abdb0 | 2008-03-04 14:28:19 -0800 | [diff] [blame] | 4291 | if (notify_on_release(parent)) |
| 4292 | set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); |
| 4293 | |
Tejun Heo | 2260e7f | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4294 | if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) |
| 4295 | set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); |
Daniel Lezcano | 97978e6 | 2010-10-27 15:33:35 -0700 | [diff] [blame] | 4296 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4297 | for_each_subsys(root, ss) { |
Tejun Heo | 8c7f6ed | 2012-09-13 12:20:58 -0700 | [diff] [blame] | 4298 | struct cgroup_subsys_state *css; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4299 | |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4300 | css = ss->css_alloc(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4301 | if (IS_ERR(css)) { |
| 4302 | err = PTR_ERR(css); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4303 | goto err_free_all; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4304 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4305 | |
| 4306 | err = percpu_ref_init(&css->refcnt, css_release); |
| 4307 | if (err) |
| 4308 | goto err_free_all; |
| 4309 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4310 | init_cgroup_css(css, ss, cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4311 | |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4312 | if (ss->use_id) { |
| 4313 | err = alloc_css_id(ss, parent, cgrp); |
| 4314 | if (err) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4315 | goto err_free_all; |
Li Zefan | 4528fd0 | 2010-02-02 13:44:10 -0800 | [diff] [blame] | 4316 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4317 | } |
| 4318 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4319 | /* |
| 4320 | * Create directory. cgroup_create_file() returns with the new |
| 4321 | * directory locked on success so that it can be populated without |
| 4322 | * dropping cgroup_mutex. |
| 4323 | */ |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4324 | err = cgroup_create_file(dentry, S_IFDIR | mode, sb); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4325 | if (err < 0) |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4326 | goto err_free_all; |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4327 | lockdep_assert_held(&dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4328 | |
Tejun Heo | 00356bd | 2013-06-18 11:14:22 -0700 | [diff] [blame] | 4329 | cgrp->serial_nr = cgroup_serial_nr_next++; |
Tejun Heo | 53fa526 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 4330 | |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4331 | /* allocation complete, commit to creation */ |
Tejun Heo | 4e139af | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4332 | list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children); |
| 4333 | root->number_of_cgroups++; |
Tejun Heo | 28fd6f3 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4334 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4335 | /* each css holds a ref to the cgroup's dentry */ |
| 4336 | for_each_subsys(root, ss) |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4337 | dget(dentry); |
Tejun Heo | 48ddbe1 | 2012-04-01 12:09:56 -0700 | [diff] [blame] | 4338 | |
Li Zefan | 415cf07 | 2013-04-08 14:35:02 +0800 | [diff] [blame] | 4339 | /* hold a ref to the parent's dentry */ |
| 4340 | dget(parent->dentry); |
| 4341 | |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4342 | /* creation succeeded, notify subsystems */ |
| 4343 | for_each_subsys(root, ss) { |
| 4344 | err = online_css(ss, cgrp); |
| 4345 | if (err) |
| 4346 | goto err_destroy; |
Glauber Costa | 1f869e8 | 2012-11-30 17:31:23 +0400 | [diff] [blame] | 4347 | |
| 4348 | if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && |
| 4349 | parent->parent) { |
| 4350 | 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", |
| 4351 | current->comm, current->pid, ss->name); |
| 4352 | if (!strcmp(ss->name, "memory")) |
| 4353 | pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n"); |
| 4354 | ss->warned_broken_hierarchy = true; |
| 4355 | } |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
Aristeu Rozanski | a1a71b4 | 2012-08-23 16:53:31 -0400 | [diff] [blame] | 4358 | err = cgroup_populate_dir(cgrp, true, root->subsys_mask); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4359 | if (err) |
| 4360 | goto err_destroy; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4361 | |
| 4362 | mutex_unlock(&cgroup_mutex); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4363 | mutex_unlock(&cgrp->dentry->d_inode->i_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4364 | |
| 4365 | return 0; |
| 4366 | |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4367 | err_free_all: |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4368 | for_each_subsys(root, ss) { |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4369 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4370 | |
| 4371 | if (css) { |
| 4372 | percpu_ref_cancel_init(&css->refcnt); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4373 | ss->css_free(cgrp); |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4374 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4375 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4376 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4377 | /* Release the reference count that we took on the superblock */ |
| 4378 | deactivate_super(sb); |
Tejun Heo | 0a950f6 | 2012-11-19 09:02:12 -0800 | [diff] [blame] | 4379 | err_free_id: |
| 4380 | ida_simple_remove(&root->cgroup_ida, cgrp->id); |
Li Zefan | 65dff75 | 2013-03-01 15:01:56 +0800 | [diff] [blame] | 4381 | err_free_name: |
| 4382 | kfree(rcu_dereference_raw(cgrp->name)); |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4383 | err_free_cgrp: |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4384 | kfree(cgrp); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4385 | return err; |
Tejun Heo | 4b8b47eb | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4386 | |
| 4387 | err_destroy: |
| 4388 | cgroup_destroy_locked(cgrp); |
| 4389 | mutex_unlock(&cgroup_mutex); |
| 4390 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 4391 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4394 | 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] | 4395 | { |
| 4396 | struct cgroup *c_parent = dentry->d_parent->d_fsdata; |
| 4397 | |
| 4398 | /* the vfs holds inode->i_mutex already */ |
| 4399 | return cgroup_create(c_parent, dentry, mode | S_IFDIR); |
| 4400 | } |
| 4401 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4402 | static void cgroup_css_killed(struct cgroup *cgrp) |
| 4403 | { |
| 4404 | if (!atomic_dec_and_test(&cgrp->css_kill_cnt)) |
| 4405 | return; |
| 4406 | |
| 4407 | /* percpu ref's of all css's are killed, kick off the next step */ |
| 4408 | INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn); |
| 4409 | schedule_work(&cgrp->destroy_work); |
| 4410 | } |
| 4411 | |
| 4412 | static void css_ref_killed_fn(struct percpu_ref *ref) |
| 4413 | { |
| 4414 | struct cgroup_subsys_state *css = |
| 4415 | container_of(ref, struct cgroup_subsys_state, refcnt); |
| 4416 | |
| 4417 | cgroup_css_killed(css->cgroup); |
| 4418 | } |
| 4419 | |
| 4420 | /** |
| 4421 | * cgroup_destroy_locked - the first stage of cgroup destruction |
| 4422 | * @cgrp: cgroup to be destroyed |
| 4423 | * |
| 4424 | * css's make use of percpu refcnts whose killing latency shouldn't be |
| 4425 | * exposed to userland and are RCU protected. Also, cgroup core needs to |
| 4426 | * guarantee that css_tryget() won't succeed by the time ->css_offline() is |
| 4427 | * invoked. To satisfy all the requirements, destruction is implemented in |
| 4428 | * the following two steps. |
| 4429 | * |
| 4430 | * s1. Verify @cgrp can be destroyed and mark it dying. Remove all |
| 4431 | * userland visible parts and start killing the percpu refcnts of |
| 4432 | * css's. Set up so that the next stage will be kicked off once all |
| 4433 | * the percpu refcnts are confirmed to be killed. |
| 4434 | * |
| 4435 | * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the |
| 4436 | * rest of destruction. Once all cgroup references are gone, the |
| 4437 | * cgroup is RCU-freed. |
| 4438 | * |
| 4439 | * This function implements s1. After this step, @cgrp is gone as far as |
| 4440 | * the userland is concerned and a new cgroup with the same name may be |
| 4441 | * created. As cgroup doesn't care about the names internally, this |
| 4442 | * doesn't cause any problem. |
| 4443 | */ |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4444 | static int cgroup_destroy_locked(struct cgroup *cgrp) |
| 4445 | __releases(&cgroup_mutex) __acquires(&cgroup_mutex) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4446 | { |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4447 | struct dentry *d = cgrp->dentry; |
Kirill A. Shutemov | 4ab7868 | 2010-03-10 15:22:34 -0800 | [diff] [blame] | 4448 | struct cgroup_event *event, *tmp; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4449 | struct cgroup_subsys *ss; |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4450 | bool empty; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4451 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4452 | lockdep_assert_held(&d->d_inode->i_mutex); |
| 4453 | lockdep_assert_held(&cgroup_mutex); |
| 4454 | |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4455 | /* |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4456 | * css_set_lock synchronizes access to ->cset_links and prevents |
| 4457 | * @cgrp from being removed while __put_css_set() is in progress. |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4458 | */ |
| 4459 | read_lock(&css_set_lock); |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 4460 | empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children); |
Tejun Heo | ddd6914 | 2013-06-12 21:04:54 -0700 | [diff] [blame] | 4461 | read_unlock(&css_set_lock); |
| 4462 | if (!empty) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4463 | return -EBUSY; |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4464 | |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4465 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4466 | * Block new css_tryget() by killing css refcnts. cgroup core |
| 4467 | * guarantees that, by the time ->css_offline() is invoked, no new |
| 4468 | * css reference will be given out via css_tryget(). We can't |
| 4469 | * simply call percpu_ref_kill() and proceed to offlining css's |
| 4470 | * because percpu_ref_kill() doesn't guarantee that the ref is seen |
| 4471 | * as killed on all CPUs on return. |
| 4472 | * |
| 4473 | * Use percpu_ref_kill_and_confirm() to get notifications as each |
| 4474 | * css is confirmed to be seen as killed on all CPUs. The |
| 4475 | * notification callback keeps track of the number of css's to be |
| 4476 | * killed and schedules cgroup_offline_fn() to perform the rest of |
| 4477 | * destruction once the percpu refs of all css's are confirmed to |
| 4478 | * be killed. |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4479 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4480 | atomic_set(&cgrp->css_kill_cnt, 1); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4481 | for_each_subsys(cgrp->root, ss) { |
| 4482 | struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id]; |
| 4483 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4484 | /* |
| 4485 | * Killing would put the base ref, but we need to keep it |
| 4486 | * alive until after ->css_offline. |
| 4487 | */ |
| 4488 | percpu_ref_get(&css->refcnt); |
| 4489 | |
| 4490 | atomic_inc(&cgrp->css_kill_cnt); |
| 4491 | percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 4492 | } |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4493 | cgroup_css_killed(cgrp); |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4494 | |
| 4495 | /* |
| 4496 | * Mark @cgrp dead. This prevents further task migration and child |
| 4497 | * creation by disabling cgroup_lock_live_group(). Note that |
| 4498 | * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to |
| 4499 | * resume iteration after dropping RCU read lock. See |
| 4500 | * cgroup_next_sibling() for details. |
| 4501 | */ |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 4502 | set_bit(CGRP_DEAD, &cgrp->flags); |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4503 | |
Tejun Heo | 455050d | 2013-06-13 19:27:41 -0700 | [diff] [blame] | 4504 | /* CGRP_DEAD is set, remove from ->release_list for the last time */ |
| 4505 | raw_spin_lock(&release_list_lock); |
| 4506 | if (!list_empty(&cgrp->release_list)) |
| 4507 | list_del_init(&cgrp->release_list); |
| 4508 | raw_spin_unlock(&release_list_lock); |
| 4509 | |
| 4510 | /* |
| 4511 | * Remove @cgrp directory. The removal puts the base ref but we |
| 4512 | * aren't quite done with @cgrp yet, so hold onto it. |
| 4513 | */ |
| 4514 | dget(d); |
| 4515 | cgroup_d_remove_dir(d); |
| 4516 | |
| 4517 | /* |
| 4518 | * Unregister events and notify userspace. |
| 4519 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 4520 | * directory to avoid race between userspace and kernelspace. |
| 4521 | */ |
| 4522 | spin_lock(&cgrp->event_list_lock); |
| 4523 | list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) { |
| 4524 | list_del_init(&event->list); |
| 4525 | schedule_work(&event->remove); |
| 4526 | } |
| 4527 | spin_unlock(&cgrp->event_list_lock); |
| 4528 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4529 | return 0; |
| 4530 | }; |
| 4531 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4532 | /** |
| 4533 | * cgroup_offline_fn - the second step of cgroup destruction |
| 4534 | * @work: cgroup->destroy_free_work |
| 4535 | * |
| 4536 | * This function is invoked from a work item for a cgroup which is being |
| 4537 | * destroyed after the percpu refcnts of all css's are guaranteed to be |
| 4538 | * seen as killed on all CPUs, and performs the rest of destruction. This |
| 4539 | * is the second step of destruction described in the comment above |
| 4540 | * cgroup_destroy_locked(). |
| 4541 | */ |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4542 | static void cgroup_offline_fn(struct work_struct *work) |
| 4543 | { |
| 4544 | struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work); |
| 4545 | struct cgroup *parent = cgrp->parent; |
| 4546 | struct dentry *d = cgrp->dentry; |
| 4547 | struct cgroup_subsys *ss; |
| 4548 | |
| 4549 | mutex_lock(&cgroup_mutex); |
| 4550 | |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4551 | /* |
| 4552 | * css_tryget() is guaranteed to fail now. Tell subsystems to |
| 4553 | * initate destruction. |
| 4554 | */ |
Tejun Heo | 1a90dd5 | 2012-11-05 09:16:59 -0800 | [diff] [blame] | 4555 | for_each_subsys(cgrp->root, ss) |
Tejun Heo | a31f2d3 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4556 | offline_css(ss, cgrp); |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4557 | |
| 4558 | /* |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 4559 | * Put the css refs from cgroup_destroy_locked(). Each css holds |
| 4560 | * an extra reference to the cgroup's dentry and cgroup removal |
| 4561 | * proceeds regardless of css refs. On the last put of each css, |
| 4562 | * whenever that may be, the extra dentry ref is put so that dentry |
| 4563 | * destruction happens only after all css's are released. |
Tejun Heo | ed957793 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4564 | */ |
Tejun Heo | e931608 | 2012-11-05 09:16:58 -0800 | [diff] [blame] | 4565 | for_each_subsys(cgrp->root, ss) |
| 4566 | css_put(cgrp->subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4567 | |
Paul Menage | 999cd8a | 2009-01-07 18:08:36 -0800 | [diff] [blame] | 4568 | /* delete this cgroup from parent->children */ |
Tejun Heo | eb6fd50 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4569 | list_del_rcu(&cgrp->sibling); |
Tejun Heo | b0ca5a8 | 2012-04-01 12:09:54 -0700 | [diff] [blame] | 4570 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4571 | dput(d); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4572 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4573 | set_bit(CGRP_RELEASABLE, &parent->flags); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 4574 | check_for_release(parent); |
| 4575 | |
Tejun Heo | ea15f8c | 2013-06-13 19:27:42 -0700 | [diff] [blame] | 4576 | mutex_unlock(&cgroup_mutex); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4577 | } |
| 4578 | |
Tejun Heo | 42809dd | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4579 | static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry) |
| 4580 | { |
| 4581 | int ret; |
| 4582 | |
| 4583 | mutex_lock(&cgroup_mutex); |
| 4584 | ret = cgroup_destroy_locked(dentry->d_fsdata); |
| 4585 | mutex_unlock(&cgroup_mutex); |
| 4586 | |
| 4587 | return ret; |
| 4588 | } |
| 4589 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4590 | static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss) |
| 4591 | { |
| 4592 | INIT_LIST_HEAD(&ss->cftsets); |
| 4593 | |
| 4594 | /* |
| 4595 | * base_cftset is embedded in subsys itself, no need to worry about |
| 4596 | * deregistration. |
| 4597 | */ |
| 4598 | if (ss->base_cftypes) { |
| 4599 | ss->base_cftset.cfts = ss->base_cftypes; |
| 4600 | list_add_tail(&ss->base_cftset.node, &ss->cftsets); |
| 4601 | } |
| 4602 | } |
| 4603 | |
Li Zefan | 06a1192 | 2008-04-29 01:00:07 -0700 | [diff] [blame] | 4604 | static void __init cgroup_init_subsys(struct cgroup_subsys *ss) |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4605 | { |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4606 | struct cgroup_subsys_state *css; |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4607 | |
| 4608 | printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4609 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4610 | mutex_lock(&cgroup_mutex); |
| 4611 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4612 | /* init base cftset */ |
| 4613 | cgroup_init_cftsets(ss); |
| 4614 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4615 | /* Create the top cgroup state for this subsystem */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4616 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4617 | ss->root = &cgroup_dummy_root; |
| 4618 | css = ss->css_alloc(cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4619 | /* We don't handle early failures gracefully */ |
| 4620 | BUG_ON(IS_ERR(css)); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4621 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4622 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4623 | /* Update the init_css_set to contain a subsys |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4624 | * pointer to this state - since the subsystem is |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4625 | * newly registered, all tasks and hence the |
| 4626 | * init_css_set is in the subsystem's top cgroup. */ |
Tejun Heo | b48c6a8 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4627 | init_css_set.subsys[ss->subsys_id] = css; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4628 | |
| 4629 | need_forkexit_callback |= ss->fork || ss->exit; |
| 4630 | |
Li Zefan | e8d55fd | 2008-04-29 01:00:13 -0700 | [diff] [blame] | 4631 | /* At system boot, before all subsystems have been |
| 4632 | * registered, no tasks have been forked, so we don't |
| 4633 | * need to invoke fork callbacks here. */ |
| 4634 | BUG_ON(!list_empty(&init_task.tasks)); |
| 4635 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4636 | BUG_ON(online_css(ss, cgroup_dummy_top)); |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4637 | |
Tejun Heo | 648bb56 | 2012-11-19 08:13:36 -0800 | [diff] [blame] | 4638 | mutex_unlock(&cgroup_mutex); |
| 4639 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4640 | /* this function shouldn't be used with modular subsystems, since they |
| 4641 | * need to register a subsys_id, among other things */ |
| 4642 | BUG_ON(ss->module); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | /** |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4646 | * cgroup_load_subsys: load and register a modular subsystem at runtime |
| 4647 | * @ss: the subsystem to load |
| 4648 | * |
| 4649 | * 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] | 4650 | * 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] | 4651 | * up for use. If the subsystem is built-in anyway, work is delegated to the |
| 4652 | * simpler cgroup_init_subsys. |
| 4653 | */ |
| 4654 | int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss) |
| 4655 | { |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4656 | struct cgroup_subsys_state *css; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4657 | int i, ret; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4658 | struct hlist_node *tmp; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4659 | struct css_set *cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4660 | unsigned long key; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4661 | |
| 4662 | /* check name and function validity */ |
| 4663 | if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN || |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4664 | ss->css_alloc == NULL || ss->css_free == NULL) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4665 | return -EINVAL; |
| 4666 | |
| 4667 | /* |
| 4668 | * we don't support callbacks in modular subsystems. this check is |
| 4669 | * before the ss->module check for consistency; a subsystem that could |
| 4670 | * be a module should still have no callbacks even if the user isn't |
| 4671 | * compiling it as one. |
| 4672 | */ |
| 4673 | if (ss->fork || ss->exit) |
| 4674 | return -EINVAL; |
| 4675 | |
| 4676 | /* |
| 4677 | * an optionally modular subsystem is built-in: we want to do nothing, |
| 4678 | * since cgroup_init_subsys will have already taken care of it. |
| 4679 | */ |
| 4680 | if (ss->module == NULL) { |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4681 | /* a sanity check */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4682 | BUG_ON(cgroup_subsys[ss->subsys_id] != ss); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4683 | return 0; |
| 4684 | } |
| 4685 | |
Tejun Heo | 8e3f654 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4686 | /* init base cftset */ |
| 4687 | cgroup_init_cftsets(ss); |
| 4688 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4689 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4690 | cgroup_subsys[ss->subsys_id] = ss; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4691 | |
| 4692 | /* |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4693 | * no ss->css_alloc seems to need anything important in the ss |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4694 | * struct, so this can happen first (i.e. before the dummy root |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4695 | * attachment). |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4696 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4697 | css = ss->css_alloc(cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4698 | if (IS_ERR(css)) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4699 | /* failure case - need to deassign the cgroup_subsys[] slot. */ |
| 4700 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4701 | mutex_unlock(&cgroup_mutex); |
| 4702 | return PTR_ERR(css); |
| 4703 | } |
| 4704 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4705 | list_add(&ss->sibling, &cgroup_dummy_root.subsys_list); |
| 4706 | ss->root = &cgroup_dummy_root; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4707 | |
| 4708 | /* our new subsystem will be attached to the dummy hierarchy. */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4709 | init_cgroup_css(css, ss, cgroup_dummy_top); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4710 | /* init_idr must be after init_cgroup_css because it sets css->id. */ |
| 4711 | if (ss->use_id) { |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4712 | ret = cgroup_init_idr(ss, css); |
| 4713 | if (ret) |
| 4714 | goto err_unload; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | /* |
| 4718 | * Now we need to entangle the css into the existing css_sets. unlike |
| 4719 | * in cgroup_init_subsys, there are now multiple css_sets, so each one |
| 4720 | * will need a new pointer to it; done by iterating the css_set_table. |
| 4721 | * furthermore, modifying the existing css_sets will corrupt the hash |
| 4722 | * table state, so each changed css_set will need its hash recomputed. |
| 4723 | * this is all done under the css_set_lock. |
| 4724 | */ |
| 4725 | write_lock(&css_set_lock); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4726 | hash_for_each_safe(css_set_table, i, tmp, cset, hlist) { |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4727 | /* skip entries that we already rehashed */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4728 | if (cset->subsys[ss->subsys_id]) |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4729 | continue; |
| 4730 | /* remove existing entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4731 | hash_del(&cset->hlist); |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4732 | /* set new value */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4733 | cset->subsys[ss->subsys_id] = css; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4734 | /* recompute hash and restore entry */ |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4735 | key = css_set_hash(cset->subsys); |
| 4736 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4737 | } |
| 4738 | write_unlock(&css_set_lock); |
| 4739 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4740 | ret = online_css(ss, cgroup_dummy_top); |
Tejun Heo | b1929db | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4741 | if (ret) |
| 4742 | goto err_unload; |
Tejun Heo | a863803 | 2012-11-09 09:12:29 -0800 | [diff] [blame] | 4743 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4744 | /* success! */ |
| 4745 | mutex_unlock(&cgroup_mutex); |
| 4746 | return 0; |
Tejun Heo | d19e19d | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4747 | |
| 4748 | err_unload: |
| 4749 | mutex_unlock(&cgroup_mutex); |
| 4750 | /* @ss can't be mounted here as try_module_get() would fail */ |
| 4751 | cgroup_unload_subsys(ss); |
| 4752 | return ret; |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4753 | } |
| 4754 | EXPORT_SYMBOL_GPL(cgroup_load_subsys); |
| 4755 | |
| 4756 | /** |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4757 | * cgroup_unload_subsys: unload a modular subsystem |
| 4758 | * @ss: the subsystem to unload |
| 4759 | * |
| 4760 | * This function should be called in a modular subsystem's exitcall. When this |
| 4761 | * function is invoked, the refcount on the subsystem's module will be 0, so |
| 4762 | * the subsystem will not be attached to any hierarchy. |
| 4763 | */ |
| 4764 | void cgroup_unload_subsys(struct cgroup_subsys *ss) |
| 4765 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4766 | struct cgrp_cset_link *link; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4767 | |
| 4768 | BUG_ON(ss->module == NULL); |
| 4769 | |
| 4770 | /* |
| 4771 | * we shouldn't be called if the subsystem is in use, and the use of |
| 4772 | * try_module_get in parse_cgroupfs_options should ensure that it |
| 4773 | * doesn't start being used while we're killing it off. |
| 4774 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4775 | BUG_ON(ss->root != &cgroup_dummy_root); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4776 | |
| 4777 | mutex_lock(&cgroup_mutex); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4778 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4779 | offline_css(ss, cgroup_dummy_top); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4780 | |
Tejun Heo | c897ff6 | 2013-02-27 17:03:49 -0800 | [diff] [blame] | 4781 | if (ss->use_id) |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4782 | idr_destroy(&ss->idr); |
Tejun Heo | 02ae748 | 2012-11-19 08:13:37 -0800 | [diff] [blame] | 4783 | |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4784 | /* deassign the subsys_id */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4785 | cgroup_subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4786 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4787 | /* remove subsystem from the dummy root's list of subsystems */ |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 4788 | list_del_init(&ss->sibling); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4789 | |
| 4790 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4791 | * disentangle the css from all css_sets attached to the dummy |
| 4792 | * top. as in loading, we need to pay our respects to the hashtable |
| 4793 | * gods. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4794 | */ |
| 4795 | write_lock(&css_set_lock); |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4796 | list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4797 | struct css_set *cset = link->cset; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4798 | unsigned long key; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4799 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 4800 | hash_del(&cset->hlist); |
| 4801 | cset->subsys[ss->subsys_id] = NULL; |
| 4802 | key = css_set_hash(cset->subsys); |
| 4803 | hash_add(css_set_table, &cset->hlist, key); |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4804 | } |
| 4805 | write_unlock(&css_set_lock); |
| 4806 | |
| 4807 | /* |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4808 | * remove subsystem's css from the cgroup_dummy_top and free it - |
| 4809 | * need to free before marking as null because ss->css_free needs |
| 4810 | * the cgrp->subsys pointer to find their state. note that this |
| 4811 | * also takes care of freeing the css_id. |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4812 | */ |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4813 | ss->css_free(cgroup_dummy_top); |
| 4814 | cgroup_dummy_top->subsys[ss->subsys_id] = NULL; |
Ben Blum | cf5d594 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4815 | |
| 4816 | mutex_unlock(&cgroup_mutex); |
| 4817 | } |
| 4818 | EXPORT_SYMBOL_GPL(cgroup_unload_subsys); |
| 4819 | |
| 4820 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4821 | * cgroup_init_early - cgroup initialization at system boot |
| 4822 | * |
| 4823 | * Initialize cgroups at system boot, and initialize any |
| 4824 | * subsystems that request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4825 | */ |
| 4826 | int __init cgroup_init_early(void) |
| 4827 | { |
| 4828 | int i; |
Lai Jiangshan | 146aa1b | 2008-10-18 20:28:03 -0700 | [diff] [blame] | 4829 | atomic_set(&init_css_set.refcount, 1); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4830 | INIT_LIST_HEAD(&init_css_set.cgrp_links); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4831 | INIT_LIST_HEAD(&init_css_set.tasks); |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4832 | INIT_HLIST_NODE(&init_css_set.hlist); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4833 | css_set_count = 1; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4834 | init_cgroup_root(&cgroup_dummy_root); |
| 4835 | cgroup_root_count = 1; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 4836 | init_task.cgroups = &init_css_set; |
| 4837 | |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4838 | init_cgrp_cset_link.cset = &init_css_set; |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4839 | init_cgrp_cset_link.cgrp = cgroup_dummy_top; |
| 4840 | list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 4841 | 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] | 4842 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4843 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4844 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4845 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4846 | /* at bootup time, we don't worry about modular subsystems */ |
| 4847 | if (!ss || ss->module) |
| 4848 | continue; |
| 4849 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4850 | BUG_ON(!ss->name); |
| 4851 | BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN); |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 4852 | BUG_ON(!ss->css_alloc); |
| 4853 | BUG_ON(!ss->css_free); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4854 | if (ss->subsys_id != i) { |
Diego Calleja | cfe36bd | 2007-11-14 16:58:54 -0800 | [diff] [blame] | 4855 | printk(KERN_ERR "cgroup: Subsys %s id == %d\n", |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4856 | ss->name, ss->subsys_id); |
| 4857 | BUG(); |
| 4858 | } |
| 4859 | |
| 4860 | if (ss->early_init) |
| 4861 | cgroup_init_subsys(ss); |
| 4862 | } |
| 4863 | return 0; |
| 4864 | } |
| 4865 | |
| 4866 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 4867 | * cgroup_init - cgroup initialization |
| 4868 | * |
| 4869 | * Register cgroup filesystem and /proc file, and initialize |
| 4870 | * any subsystems that didn't request early init. |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4871 | */ |
| 4872 | int __init cgroup_init(void) |
| 4873 | { |
| 4874 | int err; |
| 4875 | int i; |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4876 | unsigned long key; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4877 | |
| 4878 | err = bdi_init(&cgroup_backing_dev_info); |
| 4879 | if (err) |
| 4880 | return err; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4881 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4882 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4883 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 4884 | |
| 4885 | /* at bootup time, we don't worry about modular subsystems */ |
| 4886 | if (!ss || ss->module) |
| 4887 | continue; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4888 | if (!ss->early_init) |
| 4889 | cgroup_init_subsys(ss); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 4890 | if (ss->use_id) |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 4891 | cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]); |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
Li Zefan | 472b105 | 2008-04-29 01:00:11 -0700 | [diff] [blame] | 4894 | /* Add init_css_set to the hash table */ |
Li Zefan | 0ac801f | 2013-01-10 11:49:27 +0800 | [diff] [blame] | 4895 | key = css_set_hash(init_css_set.subsys); |
| 4896 | hash_add(css_set_table, &init_css_set.hlist, key); |
Tejun Heo | fa3ca07 | 2013-04-14 11:36:56 -0700 | [diff] [blame] | 4897 | |
| 4898 | /* allocate id for the dummy hierarchy */ |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4899 | mutex_lock(&cgroup_mutex); |
| 4900 | mutex_lock(&cgroup_root_mutex); |
| 4901 | |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 4902 | BUG_ON(cgroup_init_root_id(&cgroup_dummy_root)); |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4903 | |
Tejun Heo | 54e7b4e | 2013-04-14 11:36:57 -0700 | [diff] [blame] | 4904 | mutex_unlock(&cgroup_root_mutex); |
| 4905 | mutex_unlock(&cgroup_mutex); |
| 4906 | |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4907 | cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj); |
| 4908 | if (!cgroup_kobj) { |
| 4909 | err = -ENOMEM; |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4910 | goto out; |
Greg KH | 676db4a | 2010-08-05 13:53:35 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | err = register_filesystem(&cgroup_fs_type); |
| 4914 | if (err < 0) { |
| 4915 | kobject_put(cgroup_kobj); |
| 4916 | goto out; |
| 4917 | } |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4918 | |
Li Zefan | 46ae220 | 2008-04-29 01:00:08 -0700 | [diff] [blame] | 4919 | proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4920 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4921 | out: |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4922 | if (err) |
| 4923 | bdi_destroy(&cgroup_backing_dev_info); |
| 4924 | |
Paul Menage | ddbcc7e | 2007-10-18 23:39:30 -0700 | [diff] [blame] | 4925 | return err; |
| 4926 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 4927 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4928 | /* |
| 4929 | * proc_cgroup_show() |
| 4930 | * - Print task's cgroup paths into seq_file, one line for each hierarchy |
| 4931 | * - Used for /proc/<pid>/cgroup. |
| 4932 | * - No need to task_lock(tsk) on this tsk->cgroup reference, as it |
| 4933 | * doesn't really matter if tsk->cgroup changes after we read it, |
Cliff Wickman | 956db3c | 2008-02-07 00:14:43 -0800 | [diff] [blame] | 4934 | * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4935 | * anyway. No need to check that tsk->cgroup != NULL, thanks to |
| 4936 | * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks |
| 4937 | * cgroup to top_cgroup. |
| 4938 | */ |
| 4939 | |
| 4940 | /* TODO: Use a proper seq_file iterator */ |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 4941 | int proc_cgroup_show(struct seq_file *m, void *v) |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4942 | { |
| 4943 | struct pid *pid; |
| 4944 | struct task_struct *tsk; |
| 4945 | char *buf; |
| 4946 | int retval; |
| 4947 | struct cgroupfs_root *root; |
| 4948 | |
| 4949 | retval = -ENOMEM; |
| 4950 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4951 | if (!buf) |
| 4952 | goto out; |
| 4953 | |
| 4954 | retval = -ESRCH; |
| 4955 | pid = m->private; |
| 4956 | tsk = get_pid_task(pid, PIDTYPE_PID); |
| 4957 | if (!tsk) |
| 4958 | goto out_free; |
| 4959 | |
| 4960 | retval = 0; |
| 4961 | |
| 4962 | mutex_lock(&cgroup_mutex); |
| 4963 | |
Li Zefan | e5f6a86 | 2009-01-07 18:07:41 -0800 | [diff] [blame] | 4964 | for_each_active_root(root) { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4965 | struct cgroup_subsys *ss; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4966 | struct cgroup *cgrp; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4967 | int count = 0; |
| 4968 | |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 4969 | seq_printf(m, "%d:", root->hierarchy_id); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4970 | for_each_subsys(root, ss) |
| 4971 | seq_printf(m, "%s%s", count++ ? "," : "", ss->name); |
Paul Menage | c6d57f3 | 2009-09-23 15:56:19 -0700 | [diff] [blame] | 4972 | if (strlen(root->name)) |
| 4973 | seq_printf(m, "%sname=%s", count ? "," : "", |
| 4974 | root->name); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4975 | seq_putc(m, ':'); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 4976 | cgrp = task_cgroup_from_root(tsk, root); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 4977 | retval = cgroup_path(cgrp, buf, PAGE_SIZE); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4978 | if (retval < 0) |
| 4979 | goto out_unlock; |
| 4980 | seq_puts(m, buf); |
| 4981 | seq_putc(m, '\n'); |
| 4982 | } |
| 4983 | |
| 4984 | out_unlock: |
| 4985 | mutex_unlock(&cgroup_mutex); |
| 4986 | put_task_struct(tsk); |
| 4987 | out_free: |
| 4988 | kfree(buf); |
| 4989 | out: |
| 4990 | return retval; |
| 4991 | } |
| 4992 | |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4993 | /* Display information about each subsystem and each hierarchy */ |
| 4994 | static int proc_cgroupstats_show(struct seq_file *m, void *v) |
| 4995 | { |
| 4996 | int i; |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 4997 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 4998 | seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n"); |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 4999 | /* |
| 5000 | * ideally we don't want subsystems moving around while we do this. |
| 5001 | * cgroup_mutex is also necessary to guarantee an atomic snapshot of |
| 5002 | * subsys/hierarchy state. |
| 5003 | */ |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5004 | mutex_lock(&cgroup_mutex); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5005 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5006 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Ben Blum | aae8aab | 2010-03-10 15:22:07 -0800 | [diff] [blame] | 5007 | if (ss == NULL) |
| 5008 | continue; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5009 | seq_printf(m, "%s\t%d\t%d\t%d\n", |
| 5010 | ss->name, ss->root->hierarchy_id, |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5011 | ss->root->number_of_cgroups, !ss->disabled); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5012 | } |
| 5013 | mutex_unlock(&cgroup_mutex); |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | static int cgroupstats_open(struct inode *inode, struct file *file) |
| 5018 | { |
Al Viro | 9dce07f | 2008-03-29 03:07:28 +0000 | [diff] [blame] | 5019 | return single_open(file, proc_cgroupstats_show, NULL); |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5020 | } |
| 5021 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 5022 | static const struct file_operations proc_cgroupstats_operations = { |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 5023 | .open = cgroupstats_open, |
| 5024 | .read = seq_read, |
| 5025 | .llseek = seq_lseek, |
| 5026 | .release = single_release, |
| 5027 | }; |
| 5028 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5029 | /** |
| 5030 | * cgroup_fork - attach newly forked task to its parents cgroup. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5031 | * @child: pointer to task_struct of forking parent process. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5032 | * |
| 5033 | * Description: A task inherits its parent's cgroup at fork(). |
| 5034 | * |
| 5035 | * A pointer to the shared css_set was automatically copied in |
| 5036 | * fork.c by dup_task_struct(). However, we ignore that copy, since |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5037 | * it was not made under the protection of RCU or cgroup_mutex, so |
| 5038 | * might no longer be a valid cgroup pointer. cgroup_attach_task() might |
| 5039 | * have already changed current->cgroups, allowing the previously |
| 5040 | * referenced cgroup group to be removed and freed. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5041 | * |
| 5042 | * At the point that cgroup_fork() is called, 'current' is the parent |
| 5043 | * task, and the passed argument 'child' points to the child task. |
| 5044 | */ |
| 5045 | void cgroup_fork(struct task_struct *child) |
| 5046 | { |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5047 | task_lock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5048 | child->cgroups = current->cgroups; |
| 5049 | get_css_set(child->cgroups); |
Tejun Heo | 9bb7130 | 2012-10-18 17:52:07 -0700 | [diff] [blame] | 5050 | task_unlock(current); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5051 | INIT_LIST_HEAD(&child->cg_list); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | /** |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5055 | * cgroup_post_fork - called on a new task after adding it to the task list |
| 5056 | * @child: the task in question |
| 5057 | * |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5058 | * Adds the task to the list running through its css_set if necessary and |
| 5059 | * call the subsystem fork() callbacks. Has to be after the task is |
| 5060 | * visible on the task list in case we race with the first call to |
| 5061 | * cgroup_iter_start() - to guarantee that the new task ends up on its |
| 5062 | * list. |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5063 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5064 | void cgroup_post_fork(struct task_struct *child) |
| 5065 | { |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5066 | int i; |
| 5067 | |
Frederic Weisbecker | 3ce3230 | 2012-02-08 03:37:27 +0100 | [diff] [blame] | 5068 | /* |
| 5069 | * use_task_css_set_links is set to 1 before we walk the tasklist |
| 5070 | * under the tasklist_lock and we read it here after we added the child |
| 5071 | * to the tasklist under the tasklist_lock as well. If the child wasn't |
| 5072 | * yet in the tasklist when we walked through it from |
| 5073 | * cgroup_enable_task_cg_lists(), then use_task_css_set_links value |
| 5074 | * should be visible now due to the paired locking and barriers implied |
| 5075 | * by LOCK/UNLOCK: it is written before the tasklist_lock unlock |
| 5076 | * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock |
| 5077 | * lock on fork. |
| 5078 | */ |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5079 | if (use_task_css_set_links) { |
| 5080 | write_lock(&css_set_lock); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5081 | task_lock(child); |
| 5082 | if (list_empty(&child->cg_list)) |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5083 | list_add(&child->cg_list, &child->cgroups->tasks); |
Tejun Heo | d878383 | 2012-10-18 17:40:30 -0700 | [diff] [blame] | 5084 | task_unlock(child); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5085 | write_unlock(&css_set_lock); |
| 5086 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5087 | |
| 5088 | /* |
| 5089 | * Call ss->fork(). This must happen after @child is linked on |
| 5090 | * css_set; otherwise, @child might change state between ->fork() |
| 5091 | * and addition to css_set. |
| 5092 | */ |
| 5093 | if (need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5094 | /* |
| 5095 | * fork/exit callbacks are supported only for builtin |
| 5096 | * subsystems, and the builtin section of the subsys |
| 5097 | * array is immutable, so we don't need to lock the |
| 5098 | * subsys array here. On the other hand, modular section |
| 5099 | * of the array can be freed at module unload, so we |
| 5100 | * can't touch that. |
| 5101 | */ |
| 5102 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5103 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5104 | |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5105 | if (ss->fork) |
| 5106 | ss->fork(child); |
| 5107 | } |
| 5108 | } |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5109 | } |
Tejun Heo | 5edee61 | 2012-10-16 15:03:14 -0700 | [diff] [blame] | 5110 | |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5111 | /** |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5112 | * cgroup_exit - detach cgroup from exiting task |
| 5113 | * @tsk: pointer to task_struct of exiting process |
Li Zefan | a043e3b | 2008-02-23 15:24:09 -0800 | [diff] [blame] | 5114 | * @run_callback: run exit callbacks? |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5115 | * |
| 5116 | * Description: Detach cgroup from @tsk and release it. |
| 5117 | * |
| 5118 | * Note that cgroups marked notify_on_release force every task in |
| 5119 | * them to take the global cgroup_mutex mutex when exiting. |
| 5120 | * This could impact scaling on very large systems. Be reluctant to |
| 5121 | * use notify_on_release cgroups where very high task exit scaling |
| 5122 | * is required on large systems. |
| 5123 | * |
| 5124 | * the_top_cgroup_hack: |
| 5125 | * |
| 5126 | * Set the exiting tasks cgroup to the root cgroup (top_cgroup). |
| 5127 | * |
| 5128 | * We call cgroup_exit() while the task is still competent to |
| 5129 | * handle notify_on_release(), then leave the task attached to the |
| 5130 | * root cgroup in each hierarchy for the remainder of its exit. |
| 5131 | * |
| 5132 | * To do this properly, we would increment the reference count on |
| 5133 | * top_cgroup, and near the very end of the kernel/exit.c do_exit() |
| 5134 | * code we would add a second cgroup function call, to drop that |
| 5135 | * reference. This would just create an unnecessary hot spot on |
| 5136 | * the top_cgroup reference count, to no avail. |
| 5137 | * |
| 5138 | * Normally, holding a reference to a cgroup without bumping its |
| 5139 | * count is unsafe. The cgroup could go away, or someone could |
| 5140 | * attach us to a different cgroup, decrementing the count on |
| 5141 | * the first cgroup that we never incremented. But in this case, |
| 5142 | * 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] | 5143 | * which wards off any cgroup_attach_task() attempts, or task is a failed |
| 5144 | * fork, never visible to cgroup_attach_task. |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5145 | */ |
| 5146 | void cgroup_exit(struct task_struct *tsk, int run_callbacks) |
| 5147 | { |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5148 | struct css_set *cset; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5149 | int i; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5150 | |
| 5151 | /* |
| 5152 | * Unlink from the css_set task list if necessary. |
| 5153 | * Optimistically check cg_list before taking |
| 5154 | * css_set_lock |
| 5155 | */ |
| 5156 | if (!list_empty(&tsk->cg_list)) { |
| 5157 | write_lock(&css_set_lock); |
| 5158 | if (!list_empty(&tsk->cg_list)) |
Phil Carmody | 8d25879 | 2011-03-22 16:30:13 -0700 | [diff] [blame] | 5159 | list_del_init(&tsk->cg_list); |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5160 | write_unlock(&css_set_lock); |
| 5161 | } |
| 5162 | |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5163 | /* Reassign the task to the init_css_set. */ |
| 5164 | task_lock(tsk); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5165 | cset = tsk->cgroups; |
Paul Menage | 817929e | 2007-10-18 23:39:36 -0700 | [diff] [blame] | 5166 | tsk->cgroups = &init_css_set; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5167 | |
| 5168 | if (run_callbacks && need_forkexit_callback) { |
Li Zefan | 7d8e0bf | 2013-03-05 10:57:03 +0800 | [diff] [blame] | 5169 | /* |
| 5170 | * fork/exit callbacks are supported only for builtin |
| 5171 | * subsystems, see cgroup_post_fork() for details. |
| 5172 | */ |
| 5173 | for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5174 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5175 | |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5176 | if (ss->exit) { |
| 5177 | struct cgroup *old_cgrp = |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5178 | rcu_dereference_raw(cset->subsys[i])->cgroup; |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5179 | struct cgroup *cgrp = task_cgroup(tsk, i); |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 5180 | ss->exit(cgrp, old_cgrp, tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5181 | } |
| 5182 | } |
| 5183 | } |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5184 | task_unlock(tsk); |
Peter Zijlstra | d41d5a0 | 2011-02-07 17:02:20 +0100 | [diff] [blame] | 5185 | |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5186 | put_css_set_taskexit(cset); |
Paul Menage | b4f48b6 | 2007-10-18 23:39:33 -0700 | [diff] [blame] | 5187 | } |
Paul Menage | 697f416 | 2007-10-18 23:39:34 -0700 | [diff] [blame] | 5188 | |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5189 | static void check_for_release(struct cgroup *cgrp) |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5190 | { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5191 | if (cgroup_is_releasable(cgrp) && |
Tejun Heo | 6f3d828f0 | 2013-06-12 21:04:55 -0700 | [diff] [blame] | 5192 | list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) { |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5193 | /* |
| 5194 | * Control Group is currently removeable. If it's not |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5195 | * already queued for a userspace notification, queue |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5196 | * it now |
| 5197 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5198 | int need_schedule_work = 0; |
Li Zefan | f50daa7 | 2013-03-01 15:06:07 +0800 | [diff] [blame] | 5199 | |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5200 | raw_spin_lock(&release_list_lock); |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5201 | if (!cgroup_is_dead(cgrp) && |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5202 | list_empty(&cgrp->release_list)) { |
| 5203 | list_add(&cgrp->release_list, &release_list); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5204 | need_schedule_work = 1; |
| 5205 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5206 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5207 | if (need_schedule_work) |
| 5208 | schedule_work(&release_agent_work); |
| 5209 | } |
| 5210 | } |
| 5211 | |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5212 | /* |
| 5213 | * Notify userspace when a cgroup is released, by running the |
| 5214 | * configured release agent with the name of the cgroup (path |
| 5215 | * relative to the root of cgroup file system) as the argument. |
| 5216 | * |
| 5217 | * Most likely, this user command will try to rmdir this cgroup. |
| 5218 | * |
| 5219 | * This races with the possibility that some other task will be |
| 5220 | * attached to this cgroup before it is removed, or that some other |
| 5221 | * user task will 'mkdir' a child cgroup of this cgroup. That's ok. |
| 5222 | * The presumed 'rmdir' will fail quietly if this cgroup is no longer |
| 5223 | * unused, and this cgroup will be reprieved from its death sentence, |
| 5224 | * to continue to serve a useful existence. Next time it's released, |
| 5225 | * we will get notified again, if it still has 'notify_on_release' set. |
| 5226 | * |
| 5227 | * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which |
| 5228 | * means only wait until the task is successfully execve()'d. The |
| 5229 | * separate release agent task is forked by call_usermodehelper(), |
| 5230 | * then control in this thread returns here, without waiting for the |
| 5231 | * release agent task. We don't bother to wait because the caller of |
| 5232 | * this routine has no use for the exit status of the release agent |
| 5233 | * task, so no sense holding our caller up for that. |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5234 | */ |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5235 | static void cgroup_release_agent(struct work_struct *work) |
| 5236 | { |
| 5237 | BUG_ON(work != &release_agent_work); |
| 5238 | mutex_lock(&cgroup_mutex); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5239 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5240 | while (!list_empty(&release_list)) { |
| 5241 | char *argv[3], *envp[3]; |
| 5242 | int i; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5243 | char *pathbuf = NULL, *agentbuf = NULL; |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5244 | struct cgroup *cgrp = list_entry(release_list.next, |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5245 | struct cgroup, |
| 5246 | release_list); |
Paul Menage | bd89aab | 2007-10-18 23:40:44 -0700 | [diff] [blame] | 5247 | list_del_init(&cgrp->release_list); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5248 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5249 | pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5250 | if (!pathbuf) |
| 5251 | goto continue_free; |
| 5252 | if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) |
| 5253 | goto continue_free; |
| 5254 | agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL); |
| 5255 | if (!agentbuf) |
| 5256 | goto continue_free; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5257 | |
| 5258 | i = 0; |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5259 | argv[i++] = agentbuf; |
| 5260 | argv[i++] = pathbuf; |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5261 | argv[i] = NULL; |
| 5262 | |
| 5263 | i = 0; |
| 5264 | /* minimal command environment */ |
| 5265 | envp[i++] = "HOME=/"; |
| 5266 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 5267 | envp[i] = NULL; |
| 5268 | |
| 5269 | /* Drop the lock while we invoke the usermode helper, |
| 5270 | * since the exec could involve hitting disk and hence |
| 5271 | * be a slow process */ |
| 5272 | mutex_unlock(&cgroup_mutex); |
| 5273 | call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5274 | mutex_lock(&cgroup_mutex); |
Paul Menage | e788e066 | 2008-07-25 01:46:59 -0700 | [diff] [blame] | 5275 | continue_free: |
| 5276 | kfree(pathbuf); |
| 5277 | kfree(agentbuf); |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5278 | raw_spin_lock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5279 | } |
Thomas Gleixner | cdcc136f | 2009-07-25 16:47:45 +0200 | [diff] [blame] | 5280 | raw_spin_unlock(&release_list_lock); |
Paul Menage | 81a6a5c | 2007-10-18 23:39:38 -0700 | [diff] [blame] | 5281 | mutex_unlock(&cgroup_mutex); |
| 5282 | } |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5283 | |
| 5284 | static int __init cgroup_disable(char *str) |
| 5285 | { |
| 5286 | int i; |
| 5287 | char *token; |
| 5288 | |
| 5289 | while ((token = strsep(&str, ",")) != NULL) { |
| 5290 | if (!*token) |
| 5291 | continue; |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5292 | for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) { |
Tejun Heo | 9871bf9 | 2013-06-24 15:21:47 -0700 | [diff] [blame] | 5293 | struct cgroup_subsys *ss = cgroup_subsys[i]; |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5294 | |
Daniel Wagner | be45c90 | 2012-09-13 09:50:55 +0200 | [diff] [blame] | 5295 | /* |
| 5296 | * cgroup_disable, being at boot time, can't |
| 5297 | * know about module subsystems, so we don't |
| 5298 | * worry about them. |
| 5299 | */ |
| 5300 | if (!ss || ss->module) |
| 5301 | continue; |
| 5302 | |
Paul Menage | 8bab8dd | 2008-04-04 14:29:57 -0700 | [diff] [blame] | 5303 | if (!strcmp(token, ss->name)) { |
| 5304 | ss->disabled = 1; |
| 5305 | printk(KERN_INFO "Disabling %s control group" |
| 5306 | " subsystem\n", ss->name); |
| 5307 | break; |
| 5308 | } |
| 5309 | } |
| 5310 | } |
| 5311 | return 1; |
| 5312 | } |
| 5313 | __setup("cgroup_disable=", cgroup_disable); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5314 | |
| 5315 | /* |
| 5316 | * Functons for CSS ID. |
| 5317 | */ |
| 5318 | |
Tejun Heo | 54766d4 | 2013-06-12 21:04:53 -0700 | [diff] [blame] | 5319 | /* 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] | 5320 | unsigned short css_id(struct cgroup_subsys_state *css) |
| 5321 | { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 5322 | struct css_id *cssid; |
| 5323 | |
| 5324 | /* |
| 5325 | * This css_id() can return correct value when somone has refcnt |
| 5326 | * on this or this is under rcu_read_lock(). Once css->id is allocated, |
| 5327 | * it's unchanged until freed. |
| 5328 | */ |
Tejun Heo | d3daf28 | 2013-06-13 19:39:16 -0700 | [diff] [blame] | 5329 | cssid = rcu_dereference_raw(css->id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5330 | |
| 5331 | if (cssid) |
| 5332 | return cssid->id; |
| 5333 | return 0; |
| 5334 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5335 | EXPORT_SYMBOL_GPL(css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5336 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5337 | /** |
| 5338 | * css_is_ancestor - test "root" css is an ancestor of "child" |
| 5339 | * @child: the css to be tested. |
| 5340 | * @root: the css supporsed to be an ancestor of the child. |
| 5341 | * |
| 5342 | * 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] | 5343 | * this function reads css->id, the caller must hold rcu_read_lock(). |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5344 | * But, considering usual usage, the csses should be valid objects after test. |
| 5345 | * Assuming that the caller will do some action to the child if this returns |
| 5346 | * returns true, the caller must take "child";s reference count. |
| 5347 | * If "child" is valid object and this returns true, "root" is valid, too. |
| 5348 | */ |
| 5349 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5350 | bool css_is_ancestor(struct cgroup_subsys_state *child, |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 5351 | const struct cgroup_subsys_state *root) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5352 | { |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5353 | struct css_id *child_id; |
| 5354 | struct css_id *root_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5355 | |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5356 | child_id = rcu_dereference(child->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5357 | if (!child_id) |
| 5358 | return false; |
KAMEZAWA Hiroyuki | 747388d | 2010-05-11 14:06:59 -0700 | [diff] [blame] | 5359 | root_id = rcu_dereference(root->id); |
Johannes Weiner | 91c63734 | 2012-05-29 15:06:24 -0700 | [diff] [blame] | 5360 | if (!root_id) |
| 5361 | return false; |
| 5362 | if (child_id->depth < root_id->depth) |
| 5363 | return false; |
| 5364 | if (child_id->stack[root_id->depth] != root_id->id) |
| 5365 | return false; |
| 5366 | return true; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5367 | } |
| 5368 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5369 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css) |
| 5370 | { |
| 5371 | struct css_id *id = css->id; |
| 5372 | /* When this is called before css_id initialization, id can be NULL */ |
| 5373 | if (!id) |
| 5374 | return; |
| 5375 | |
| 5376 | BUG_ON(!ss->use_id); |
| 5377 | |
| 5378 | rcu_assign_pointer(id->css, NULL); |
| 5379 | rcu_assign_pointer(css->id, NULL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5380 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5381 | idr_remove(&ss->idr, id->id); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5382 | spin_unlock(&ss->id_lock); |
Lai Jiangshan | 025cea9 | 2011-03-15 17:56:10 +0800 | [diff] [blame] | 5383 | kfree_rcu(id, rcu_head); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5384 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5385 | EXPORT_SYMBOL_GPL(free_css_id); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5386 | |
| 5387 | /* |
| 5388 | * This is called by init or create(). Then, calls to this function are |
| 5389 | * always serialized (By cgroup_mutex() at create()). |
| 5390 | */ |
| 5391 | |
| 5392 | static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth) |
| 5393 | { |
| 5394 | struct css_id *newid; |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5395 | int ret, size; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5396 | |
| 5397 | BUG_ON(!ss->use_id); |
| 5398 | |
| 5399 | size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1); |
| 5400 | newid = kzalloc(size, GFP_KERNEL); |
| 5401 | if (!newid) |
| 5402 | return ERR_PTR(-ENOMEM); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5403 | |
| 5404 | idr_preload(GFP_KERNEL); |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5405 | spin_lock(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5406 | /* Don't use 0. allocates an ID of 1-65535 */ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5407 | 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] | 5408 | spin_unlock(&ss->id_lock); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5409 | idr_preload_end(); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5410 | |
| 5411 | /* Returns error when there are no free spaces for new ID.*/ |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5412 | if (ret < 0) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5413 | goto err_out; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5414 | |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5415 | newid->id = ret; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5416 | newid->depth = depth; |
| 5417 | return newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5418 | err_out: |
| 5419 | kfree(newid); |
Tejun Heo | d228d9e | 2013-02-27 17:04:54 -0800 | [diff] [blame] | 5420 | return ERR_PTR(ret); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5421 | |
| 5422 | } |
| 5423 | |
Ben Blum | e6a1105 | 2010-03-10 15:22:09 -0800 | [diff] [blame] | 5424 | static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss, |
| 5425 | struct cgroup_subsys_state *rootcss) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5426 | { |
| 5427 | struct css_id *newid; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5428 | |
Hugh Dickins | 42aee6c | 2012-03-21 16:34:21 -0700 | [diff] [blame] | 5429 | spin_lock_init(&ss->id_lock); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5430 | idr_init(&ss->idr); |
| 5431 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5432 | newid = get_new_cssid(ss, 0); |
| 5433 | if (IS_ERR(newid)) |
| 5434 | return PTR_ERR(newid); |
| 5435 | |
| 5436 | newid->stack[0] = newid->id; |
| 5437 | newid->css = rootcss; |
| 5438 | rootcss->id = newid; |
| 5439 | return 0; |
| 5440 | } |
| 5441 | |
| 5442 | static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent, |
| 5443 | struct cgroup *child) |
| 5444 | { |
| 5445 | int subsys_id, i, depth = 0; |
| 5446 | struct cgroup_subsys_state *parent_css, *child_css; |
Li Zefan | fae9c79 | 2010-04-22 17:30:00 +0800 | [diff] [blame] | 5447 | struct css_id *child_id, *parent_id; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5448 | |
| 5449 | subsys_id = ss->subsys_id; |
| 5450 | parent_css = parent->subsys[subsys_id]; |
| 5451 | child_css = child->subsys[subsys_id]; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5452 | parent_id = parent_css->id; |
Greg Thelen | 94b3dd0 | 2010-06-04 14:15:03 -0700 | [diff] [blame] | 5453 | depth = parent_id->depth + 1; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5454 | |
| 5455 | child_id = get_new_cssid(ss, depth); |
| 5456 | if (IS_ERR(child_id)) |
| 5457 | return PTR_ERR(child_id); |
| 5458 | |
| 5459 | for (i = 0; i < depth; i++) |
| 5460 | child_id->stack[i] = parent_id->stack[i]; |
| 5461 | child_id->stack[depth] = child_id->id; |
| 5462 | /* |
| 5463 | * child_id->css pointer will be set after this cgroup is available |
| 5464 | * see cgroup_populate_dir() |
| 5465 | */ |
| 5466 | rcu_assign_pointer(child_css->id, child_id); |
| 5467 | |
| 5468 | return 0; |
| 5469 | } |
| 5470 | |
| 5471 | /** |
| 5472 | * css_lookup - lookup css by id |
| 5473 | * @ss: cgroup subsys to be looked into. |
| 5474 | * @id: the id |
| 5475 | * |
| 5476 | * Returns pointer to cgroup_subsys_state if there is valid one with id. |
| 5477 | * NULL if not. Should be called under rcu_read_lock() |
| 5478 | */ |
| 5479 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id) |
| 5480 | { |
| 5481 | struct css_id *cssid = NULL; |
| 5482 | |
| 5483 | BUG_ON(!ss->use_id); |
| 5484 | cssid = idr_find(&ss->idr, id); |
| 5485 | |
| 5486 | if (unlikely(!cssid)) |
| 5487 | return NULL; |
| 5488 | |
| 5489 | return rcu_dereference(cssid->css); |
| 5490 | } |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 5491 | EXPORT_SYMBOL_GPL(css_lookup); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 5492 | |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5493 | /* |
| 5494 | * get corresponding css from file open on cgroupfs directory |
| 5495 | */ |
| 5496 | struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id) |
| 5497 | { |
| 5498 | struct cgroup *cgrp; |
| 5499 | struct inode *inode; |
| 5500 | struct cgroup_subsys_state *css; |
| 5501 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5502 | inode = file_inode(f); |
Stephane Eranian | e5d1367 | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 5503 | /* check in cgroup filesystem dir */ |
| 5504 | if (inode->i_op != &cgroup_dir_inode_operations) |
| 5505 | return ERR_PTR(-EBADF); |
| 5506 | |
| 5507 | if (id < 0 || id >= CGROUP_SUBSYS_COUNT) |
| 5508 | return ERR_PTR(-EINVAL); |
| 5509 | |
| 5510 | /* get cgroup */ |
| 5511 | cgrp = __d_cgrp(f->f_dentry); |
| 5512 | css = cgrp->subsys[id]; |
| 5513 | return css ? css : ERR_PTR(-ENOENT); |
| 5514 | } |
| 5515 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5516 | #ifdef CONFIG_CGROUP_DEBUG |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5517 | static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5518 | { |
| 5519 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 5520 | |
| 5521 | if (!css) |
| 5522 | return ERR_PTR(-ENOMEM); |
| 5523 | |
| 5524 | return css; |
| 5525 | } |
| 5526 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5527 | static void debug_css_free(struct cgroup *cgrp) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5528 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5529 | kfree(cgrp->subsys[debug_subsys_id]); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5530 | } |
| 5531 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5532 | static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5533 | { |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5534 | return cgroup_task_count(cgrp); |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5535 | } |
| 5536 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5537 | static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5538 | { |
| 5539 | return (u64)(unsigned long)current->cgroups; |
| 5540 | } |
| 5541 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5542 | static u64 current_css_set_refcount_read(struct cgroup *cgrp, |
| 5543 | struct cftype *cft) |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5544 | { |
| 5545 | u64 count; |
| 5546 | |
| 5547 | rcu_read_lock(); |
| 5548 | count = atomic_read(¤t->cgroups->refcount); |
| 5549 | rcu_read_unlock(); |
| 5550 | return count; |
| 5551 | } |
| 5552 | |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5553 | static int current_css_set_cg_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5554 | struct cftype *cft, |
| 5555 | struct seq_file *seq) |
| 5556 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5557 | struct cgrp_cset_link *link; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5558 | struct css_set *cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5559 | |
| 5560 | read_lock(&css_set_lock); |
| 5561 | rcu_read_lock(); |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5562 | cset = rcu_dereference(current->cgroups); |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5563 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5564 | struct cgroup *c = link->cgrp; |
| 5565 | const char *name; |
| 5566 | |
| 5567 | if (c->dentry) |
| 5568 | name = c->dentry->d_name.name; |
| 5569 | else |
| 5570 | name = "?"; |
Paul Menage | 2c6ab6d | 2009-09-23 15:56:23 -0700 | [diff] [blame] | 5571 | seq_printf(seq, "Root %d group %s\n", |
| 5572 | c->root->hierarchy_id, name); |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5573 | } |
| 5574 | rcu_read_unlock(); |
| 5575 | read_unlock(&css_set_lock); |
| 5576 | return 0; |
| 5577 | } |
| 5578 | |
| 5579 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5580 | static int cgroup_css_links_read(struct cgroup *cgrp, |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5581 | struct cftype *cft, |
| 5582 | struct seq_file *seq) |
| 5583 | { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5584 | struct cgrp_cset_link *link; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5585 | |
| 5586 | read_lock(&css_set_lock); |
Li Zefan | 03c78cb | 2013-06-14 11:17:19 +0800 | [diff] [blame] | 5587 | list_for_each_entry(link, &cgrp->cset_links, cset_link) { |
Tejun Heo | 69d0206 | 2013-06-12 21:04:50 -0700 | [diff] [blame] | 5588 | struct css_set *cset = link->cset; |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5589 | struct task_struct *task; |
| 5590 | int count = 0; |
Tejun Heo | 5abb885 | 2013-06-12 21:04:49 -0700 | [diff] [blame] | 5591 | seq_printf(seq, "css_set %p\n", cset); |
| 5592 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5593 | if (count++ > MAX_TASKS_SHOWN_PER_CSS) { |
| 5594 | seq_puts(seq, " ...\n"); |
| 5595 | break; |
| 5596 | } else { |
| 5597 | seq_printf(seq, " task %d\n", |
| 5598 | task_pid_vnr(task)); |
| 5599 | } |
| 5600 | } |
| 5601 | } |
| 5602 | read_unlock(&css_set_lock); |
| 5603 | return 0; |
| 5604 | } |
| 5605 | |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5606 | static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft) |
| 5607 | { |
| 5608 | return test_bit(CGRP_RELEASABLE, &cgrp->flags); |
| 5609 | } |
| 5610 | |
| 5611 | static struct cftype debug_files[] = { |
| 5612 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5613 | .name = "taskcount", |
| 5614 | .read_u64 = debug_taskcount_read, |
| 5615 | }, |
| 5616 | |
| 5617 | { |
| 5618 | .name = "current_css_set", |
| 5619 | .read_u64 = current_css_set_read, |
| 5620 | }, |
| 5621 | |
| 5622 | { |
| 5623 | .name = "current_css_set_refcount", |
| 5624 | .read_u64 = current_css_set_refcount_read, |
| 5625 | }, |
| 5626 | |
| 5627 | { |
Paul Menage | 7717f7b | 2009-09-23 15:56:22 -0700 | [diff] [blame] | 5628 | .name = "current_css_set_cg_links", |
| 5629 | .read_seq_string = current_css_set_cg_links_read, |
| 5630 | }, |
| 5631 | |
| 5632 | { |
| 5633 | .name = "cgroup_css_links", |
| 5634 | .read_seq_string = cgroup_css_links_read, |
| 5635 | }, |
| 5636 | |
| 5637 | { |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5638 | .name = "releasable", |
| 5639 | .read_u64 = releasable_read, |
| 5640 | }, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5641 | |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5642 | { } /* terminate */ |
| 5643 | }; |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5644 | |
| 5645 | struct cgroup_subsys debug_subsys = { |
| 5646 | .name = "debug", |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 5647 | .css_alloc = debug_css_alloc, |
| 5648 | .css_free = debug_css_free, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5649 | .subsys_id = debug_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5650 | .base_cftypes = debug_files, |
Paul Menage | fe69343 | 2009-09-23 15:56:20 -0700 | [diff] [blame] | 5651 | }; |
| 5652 | #endif /* CONFIG_CGROUP_DEBUG */ |